125 lines
3.9 KiB
C++
125 lines
3.9 KiB
C++
#pragma once
|
|
|
|
#include <echoion/api.hpp>
|
|
#include "commands/Command.hpp"
|
|
#include "echoion/api/ConfigTypes.hpp"
|
|
#include <map>
|
|
#include <vector>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <string>
|
|
#include <functional>
|
|
|
|
namespace Echoion::Core {
|
|
|
|
class MusicManager;
|
|
|
|
// Function to check if client has permission (set by Core)
|
|
using PermissionCheckFunc = std::function<bool(uint64_t clientId, CommandCategory category)>;
|
|
|
|
/**
|
|
* @brief Manages command registration and dispatch.
|
|
*
|
|
* Usage:
|
|
* CommandManager mgr;
|
|
* mgr.registerCommand<PlayCommand>("play");
|
|
* mgr.dispatch(clientId, "play song name", musicManager, replyFunc);
|
|
*/
|
|
class CommandManager {
|
|
public:
|
|
CommandManager();
|
|
~CommandManager() = default;
|
|
|
|
// Disable copy
|
|
CommandManager(const CommandManager&) = delete;
|
|
CommandManager& operator=(const CommandManager&) = delete;
|
|
|
|
// Enable move
|
|
CommandManager(CommandManager&&) = default;
|
|
CommandManager& operator=(CommandManager&&) = default;
|
|
|
|
/**
|
|
* @brief Registers a command class.
|
|
* @tparam T The command class (must inherit Command).
|
|
* @param primaryName The primary name/trigger (e.g., "play").
|
|
*/
|
|
template <typename T>
|
|
void registerCommand(const std::string& primaryName) {
|
|
auto cmd = std::make_shared<T>();
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
// Register primary name
|
|
commands_[primaryName] = cmd;
|
|
uniqueCommands_.push_back({primaryName, cmd});
|
|
|
|
// Register aliases automatically from the class itself
|
|
for (auto alias : cmd->getAliases()) {
|
|
commands_[std::string(alias)] = cmd;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Dispatches a command message to the appropriate handler.
|
|
* @param clientId The client ID who invoked the command.
|
|
* @param message The full message (including prefix, e.g., "!play song").
|
|
* @param musicManager Weak pointer to the MusicManager.
|
|
* @param replyFunc Function to call to send a reply.
|
|
* @param invokerName Name of the invoking user.
|
|
* @return true if a command was found and executed, false otherwise.
|
|
*/
|
|
[[nodiscard]] bool dispatch(
|
|
uint64_t clientId,
|
|
std::string_view message,
|
|
std::weak_ptr<MusicManager> musicManager,
|
|
std::function<void(std::string_view)> replyFunc,
|
|
std::string_view invokerName = ""
|
|
);
|
|
|
|
[[nodiscard]] bool hasCommand(std::string_view name) const;
|
|
[[nodiscard]] std::optional<CommandPtr> getCommand(std::string_view name) const;
|
|
[[nodiscard]] std::vector<std::pair<std::string, CommandPtr>> getAllCommands() const;
|
|
|
|
// Get commands visible to a specific client (for help)
|
|
[[nodiscard]] std::vector<std::pair<std::string, CommandPtr>> getVisibleCommands(uint64_t clientId) const;
|
|
|
|
// Check if client can execute a command
|
|
[[nodiscard]] bool canExecuteCommand(uint64_t clientId, const CommandPtr& cmd) const;
|
|
|
|
void setPrefix(std::string_view prefix);
|
|
[[nodiscard]] std::string_view getPrefix() const { return prefix_; }
|
|
|
|
// Set permission check function
|
|
void setPermissionCheckFunc(PermissionCheckFunc func);
|
|
// Set server config for permission checks
|
|
void setServerConfig(const Api::AppConfig& config);
|
|
|
|
private:
|
|
struct CommandEntry {
|
|
std::string primaryName;
|
|
CommandPtr instance;
|
|
};
|
|
|
|
void registerBuiltinCommands();
|
|
|
|
// Internal Help Command (defined in .cpp to access registry)
|
|
class HelpCommand;
|
|
void registerHelpCommand();
|
|
|
|
std::string prefix_ = "!";
|
|
|
|
// Map for fast lookup: "play" -> PlayCommand
|
|
std::map<std::string, CommandPtr, std::less<>> commands_;
|
|
|
|
// List for generating help: {"play", PlayCommand}
|
|
std::vector<CommandEntry> uniqueCommands_;
|
|
|
|
mutable std::mutex mutex_;
|
|
|
|
// Permission checking
|
|
PermissionCheckFunc permissionCheck_;
|
|
std::optional<Api::AppConfig> serverConfig_;
|
|
};
|
|
|
|
} // namespace Echoion::Core
|