Delete the echoion Logger adapter (Logger.{hpp,cpp}) and the
ILogger/LoggerRegistry indirection. Core::installLogging() builds the
console + split-file Void::Logging::MultiLogger and installs it as the
process-wide Void::Log sink via Void::Log::make_sink (logSink_ owns it for
the process lifetime). The ~450 ECHOION_LOG_* call sites across EchoionCore
are rewritten to Void::Log::<level>("<Subsystem>", "{}", <expr>) with
per-file module tags (warning->warn, critical->fatal); the pre-formatted
message is passed as the single "{}" argument. Build green.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
484 lines
13 KiB
C++
484 lines
13 KiB
C++
#include "echoion/core/CommandManager.hpp"
|
|
#include <Void/Log/Log.hpp>
|
|
#include "echoion/core/MusicManager.hpp"
|
|
#include "echoion/core/commands/Command.hpp"
|
|
#include <ConfigTypes.hpp>
|
|
#include <algorithm>
|
|
#include <echoion/core/commands/Commands.hpp>
|
|
#include <format>
|
|
#include <sstream>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
|
|
// --- Internal Help Command ---
|
|
class CommandManager::HelpCommand : public Command
|
|
{
|
|
CommandManager& manager_;
|
|
|
|
public:
|
|
|
|
explicit HelpCommand(CommandManager& mgr) : manager_(mgr) {}
|
|
|
|
void execute(const CommandContext& ctx) override
|
|
{
|
|
try {
|
|
auto prefix = manager_.getPrefix();
|
|
|
|
std::string helpMsg = std::format(
|
|
"\xf0\x9f\x8e\xb5 **Echoion Bot Help**\n\n");
|
|
helpMsg += std::format("Prefix: `{}`\n\n", prefix);
|
|
|
|
// Get commands visible to this client
|
|
auto visibleCommands =
|
|
manager_.getVisibleCommands(ctx.clientId);
|
|
|
|
// Group commands by category
|
|
std::map<CommandCategory,
|
|
std::vector<std::pair<std::string, CommandPtr>>>
|
|
grouped;
|
|
|
|
for (const auto& [name, cmd] : visibleCommands) {
|
|
grouped[cmd->getCategory()].push_back({name, cmd});
|
|
}
|
|
|
|
for (const auto& [cat, cmds] : grouped) {
|
|
helpMsg +=
|
|
std::format("**{} Commands**\n", toString(cat));
|
|
|
|
for (const auto& [name, cmd] : cmds) {
|
|
// Format: prefix!command usage
|
|
if (!cmd->getUsage().empty()) {
|
|
helpMsg += std::format("`{}{}` {}\n",
|
|
prefix,
|
|
name,
|
|
cmd->getUsage());
|
|
} else {
|
|
helpMsg +=
|
|
std::format("`{}{}`\n", prefix, name);
|
|
}
|
|
|
|
// Description
|
|
helpMsg +=
|
|
std::format("> {}\n", cmd->getDescription());
|
|
|
|
// Aliases
|
|
auto aliases = cmd->getAliases();
|
|
if (!aliases.empty()) {
|
|
helpMsg += "> Aliases: ";
|
|
for (size_t i = 0; i < aliases.size(); ++i) {
|
|
helpMsg += std::format(
|
|
"`{}{}`", prefix, aliases[i]);
|
|
if (i < aliases.size() - 1) {
|
|
helpMsg += ", ";
|
|
}
|
|
}
|
|
helpMsg += "\n";
|
|
}
|
|
|
|
helpMsg += "\n";
|
|
}
|
|
}
|
|
|
|
ctx.reply(helpMsg);
|
|
} catch (const std::exception& e) {
|
|
Void::Log::error("CommandManager", "{}",
|
|
std::format("Help command exception: {}", e.what()));
|
|
ctx.reply("Couldn't generate help. Please try again.");
|
|
}
|
|
}
|
|
|
|
std::string_view getDescription() const override
|
|
{
|
|
return "Shows this help message with all available commands.";
|
|
}
|
|
|
|
std::string_view getUsage() const override { return "[command]"; }
|
|
|
|
CommandCategory getCategory() const override
|
|
{
|
|
return CommandCategory::Utility;
|
|
}
|
|
|
|
std::vector<std::string_view> getAliases() const override
|
|
{
|
|
return {"h", "?", "commands"};
|
|
}
|
|
};
|
|
|
|
// --- Manager Implementation ---
|
|
|
|
CommandManager::CommandManager()
|
|
{
|
|
registerBuiltinCommands();
|
|
registerHelpCommand();
|
|
startThreadPool();
|
|
}
|
|
|
|
CommandManager::~CommandManager() { shutdown(); }
|
|
|
|
void CommandManager::shutdown()
|
|
{
|
|
shutdown_ = true;
|
|
// ~ThreadPool drains queued commands and joins its workers.
|
|
pool_.reset();
|
|
}
|
|
|
|
void CommandManager::startThreadPool()
|
|
{
|
|
pool_ =
|
|
std::make_unique<Void::Concurrency::ThreadPool>(THREAD_POOL_SIZE);
|
|
Void::Log::info("CommandManager", "{}",
|
|
std::format("CommandManager thread pool started with {} workers",
|
|
THREAD_POOL_SIZE));
|
|
}
|
|
|
|
void CommandManager::executeCommandAsync(CommandTask task)
|
|
{
|
|
try {
|
|
// Re-parse the command (same logic as dispatch)
|
|
std::string_view message = task.message;
|
|
if (!message.starts_with(prefix_)) {
|
|
return;
|
|
}
|
|
|
|
// Skip prefix and trim
|
|
std::string_view input = message.substr(prefix_.length());
|
|
size_t start = input.find_first_not_of(" \t");
|
|
if (start == std::string_view::npos) {
|
|
return;
|
|
}
|
|
input = input.substr(start);
|
|
|
|
// Split command and args
|
|
size_t spacePos = input.find_first_of(" \t");
|
|
std::string_view cmdName = (spacePos == std::string_view::npos)
|
|
? input
|
|
: input.substr(0, spacePos);
|
|
|
|
std::string argsStr;
|
|
if (spacePos != std::string_view::npos) {
|
|
size_t argStart = input.find_first_not_of(" \t", spacePos);
|
|
if (argStart != std::string_view::npos) {
|
|
argsStr = std::string(input.substr(argStart));
|
|
}
|
|
}
|
|
|
|
std::string cmdKey(cmdName);
|
|
std::transform(
|
|
cmdKey.begin(), cmdKey.end(), cmdKey.begin(), ::tolower);
|
|
|
|
CommandPtr command;
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
auto it = commands_.find(cmdKey);
|
|
if (it == commands_.end()) {
|
|
return;
|
|
}
|
|
command = it->second;
|
|
}
|
|
|
|
// Check permissions
|
|
if (!canExecuteCommand(task.clientId, command)) {
|
|
task.replyFunc(
|
|
"You don't have permission to use this command.");
|
|
return;
|
|
}
|
|
|
|
// Execute command
|
|
CommandContext ctx{.clientId = task.clientId,
|
|
.prefix = prefix_,
|
|
.args = std::move(argsStr),
|
|
.invokerName = task.invokerName,
|
|
.musicManager = task.musicManager,
|
|
.services = services_,
|
|
.replyCallback = std::move(task.replyFunc)};
|
|
|
|
command->execute(ctx);
|
|
|
|
} catch (const std::exception& e) {
|
|
Void::Log::error("CommandManager", "{}",
|
|
std::format("Async command execution error: {}", e.what()));
|
|
try {
|
|
task.replyFunc(
|
|
"An error occurred while executing the command.");
|
|
} catch (...) {
|
|
}
|
|
}
|
|
}
|
|
|
|
bool CommandManager::dispatch(uint64_t clientId,
|
|
std::string_view message,
|
|
std::weak_ptr<MusicManager>
|
|
musicManager,
|
|
std::function<void(std::string_view)>
|
|
replyFunc,
|
|
std::string_view invokerName)
|
|
{
|
|
|
|
if (!message.starts_with(prefix_)) {
|
|
return false;
|
|
}
|
|
|
|
// Skip prefix and trim leading whitespace
|
|
std::string_view input = message.substr(prefix_.length());
|
|
size_t start = input.find_first_not_of(" \t");
|
|
if (start == std::string_view::npos) {
|
|
return false;
|
|
}
|
|
input = input.substr(start);
|
|
|
|
// Split command and arguments
|
|
size_t spacePos = input.find_first_of(" \t");
|
|
std::string_view cmdName = (spacePos == std::string_view::npos)
|
|
? input
|
|
: input.substr(0, spacePos);
|
|
|
|
// Convert to lowercase for lookup
|
|
std::string cmdKey(cmdName);
|
|
std::transform(cmdKey.begin(), cmdKey.end(), cmdKey.begin(), ::tolower);
|
|
|
|
// Check if command exists
|
|
CommandPtr command;
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
auto it = commands_.find(cmdKey);
|
|
if (it == commands_.end()) {
|
|
return false;
|
|
}
|
|
command = it->second;
|
|
}
|
|
|
|
// Check permissions synchronously (fast check)
|
|
if (!canExecuteCommand(clientId, command)) {
|
|
replyFunc("You don't have permission to use this command.");
|
|
return true; // Command was found but not allowed
|
|
}
|
|
|
|
// Submit the command for async execution on the worker pool.
|
|
if (shutdown_ || !pool_) {
|
|
return false;
|
|
}
|
|
CommandTask task{.clientId = clientId,
|
|
.message = std::string(message),
|
|
.musicManager = musicManager,
|
|
.replyFunc = std::move(replyFunc),
|
|
.invokerName = std::string(invokerName)};
|
|
|
|
pool_->submit([this, task = std::move(task)]() mutable {
|
|
executeCommandAsync(std::move(task));
|
|
});
|
|
|
|
return true;
|
|
}
|
|
|
|
bool CommandManager::dispatchSync(uint64_t clientId,
|
|
std::string_view message,
|
|
std::weak_ptr<MusicManager>
|
|
musicManager,
|
|
std::function<void(std::string_view)>
|
|
replyFunc,
|
|
std::string_view invokerName)
|
|
{
|
|
|
|
if (!message.starts_with(prefix_)) {
|
|
return false;
|
|
}
|
|
|
|
// Skip prefix and trim leading whitespace
|
|
std::string_view input = message.substr(prefix_.length());
|
|
size_t start = input.find_first_not_of(" \t");
|
|
if (start == std::string_view::npos) {
|
|
return false;
|
|
}
|
|
input = input.substr(start);
|
|
|
|
// Split command and arguments
|
|
size_t spacePos = input.find_first_of(" \t");
|
|
std::string_view cmdName = (spacePos == std::string_view::npos)
|
|
? input
|
|
: input.substr(0, spacePos);
|
|
|
|
std::string argsStr;
|
|
if (spacePos != std::string_view::npos) {
|
|
size_t argStart = input.find_first_not_of(" \t", spacePos);
|
|
if (argStart != std::string_view::npos) {
|
|
argsStr = std::string(input.substr(argStart));
|
|
}
|
|
}
|
|
|
|
// Convert to lowercase for lookup
|
|
std::string cmdKey(cmdName);
|
|
std::transform(cmdKey.begin(), cmdKey.end(), cmdKey.begin(), ::tolower);
|
|
|
|
CommandPtr command;
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
auto it = commands_.find(cmdKey);
|
|
if (it == commands_.end()) {
|
|
return false;
|
|
}
|
|
command = it->second;
|
|
}
|
|
|
|
// Check permissions
|
|
if (!canExecuteCommand(clientId, command)) {
|
|
replyFunc("You don't have permission to use this command.");
|
|
return true;
|
|
}
|
|
|
|
CommandContext ctx{.clientId = clientId,
|
|
.prefix = prefix_,
|
|
.args = std::move(argsStr),
|
|
.invokerName = invokerName,
|
|
.musicManager = musicManager,
|
|
.services = services_,
|
|
.replyCallback = std::move(replyFunc)};
|
|
|
|
try {
|
|
command->execute(ctx);
|
|
} catch (const std::exception& e) {
|
|
Void::Log::error("CommandManager", "{}", std::format(
|
|
"Command execution error [{}]: {}", cmdKey, e.what()));
|
|
ctx.reply(
|
|
"An unexpected error occurred. Our team has been notified.");
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool CommandManager::hasCommand(std::string_view name) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return commands_.contains(std::string(name));
|
|
}
|
|
|
|
std::optional<CommandPtr>
|
|
CommandManager::getCommand(std::string_view name) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
auto it = commands_.find(std::string(name));
|
|
if (it != commands_.end()) {
|
|
return it->second;
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
std::vector<std::pair<std::string, CommandPtr>>
|
|
CommandManager::getAllCommands() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
std::vector<std::pair<std::string, CommandPtr>> result;
|
|
result.reserve(uniqueCommands_.size());
|
|
for (const auto& entry : uniqueCommands_) {
|
|
result.emplace_back(entry.primaryName, entry.instance);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
std::vector<std::pair<std::string, CommandPtr>>
|
|
CommandManager::getVisibleCommands(uint64_t clientId) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
std::vector<std::pair<std::string, CommandPtr>> result;
|
|
|
|
for (const auto& entry : uniqueCommands_) {
|
|
// Only include if client can execute it
|
|
if (canExecuteCommand(clientId, entry.instance)) {
|
|
result.emplace_back(entry.primaryName, entry.instance);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
bool CommandManager::canExecuteCommand(uint64_t clientId,
|
|
const CommandPtr& cmd) const
|
|
{
|
|
if (!cmd->requiresAdmin()) {
|
|
return true; // Non-admin commands are always allowed
|
|
}
|
|
|
|
// If no server config is set, deny admin commands
|
|
if (!serverConfig_.has_value()) {
|
|
return false;
|
|
}
|
|
|
|
// If no admin groups configured, deny everyone
|
|
if (serverConfig_->teamspeak.adminGroups.empty()) {
|
|
return false;
|
|
}
|
|
|
|
// Check permission via callback if set
|
|
if (permissionCheck_ &&
|
|
(cmd->requiresAdmin() ||
|
|
cmd->getCategory() == CommandCategory::Admin)) {
|
|
return permissionCheck_(clientId, cmd->getCategory());
|
|
}
|
|
|
|
// Default: deny admin commands if no checker configured
|
|
return false;
|
|
}
|
|
|
|
void CommandManager::setPrefix(std::string_view prefix)
|
|
{
|
|
prefix_ = prefix;
|
|
}
|
|
|
|
void CommandManager::setPermissionCheckFunc(PermissionCheckFunc func)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
permissionCheck_ = std::move(func);
|
|
}
|
|
|
|
void CommandManager::setServerConfig(const Api::AppConfig& config)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
serverConfig_ = config;
|
|
}
|
|
|
|
void CommandManager::registerBuiltinCommands()
|
|
{
|
|
registerCommand<PlayCommand>("play");
|
|
registerCommand<SelectCommand>("select");
|
|
registerCommand<PauseCommand>("pause");
|
|
registerCommand<ResumeCommand>("resume");
|
|
registerCommand<StopCommand>("stop");
|
|
registerCommand<SkipCommand>("skip");
|
|
// registerCommand<SeekCommand>("seek");
|
|
registerCommand<VolumeCommand>("volume");
|
|
registerCommand<QueueCommand>("queue");
|
|
registerCommand<RemoveCommand>("remove");
|
|
registerCommand<ClearCommand>("clear");
|
|
registerCommand<MoveCommand>("move");
|
|
registerCommand<ShuffleCommand>("shuffle");
|
|
registerCommand<RepeatCommand>("repeat");
|
|
registerCommand<PlaylistCommand>("playlist");
|
|
registerCommand<NowPlayingCommand>("np");
|
|
registerCommand<SearchCommand>("search");
|
|
registerCommand<ReloadCommand>("reload");
|
|
registerCommand<ReloadConfigCommand>("reloadconfig");
|
|
registerCommand<SubscribeCommand>("subscribe");
|
|
registerCommand<UnsubscribeCommand>("unsubscribe");
|
|
registerCommand<SubscribersCommand>("subscribers");
|
|
registerCommand<UnsubscribeIdCommand>("unsubscribeid");
|
|
registerCommand<UnsubscribeAllCommand>("unsubscribeall");
|
|
registerCommand<RpcInfoCommand>("rpc");
|
|
}
|
|
|
|
void CommandManager::registerHelpCommand()
|
|
{
|
|
auto helpCmd = std::make_shared<HelpCommand>(*this);
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
commands_["help"] = helpCmd;
|
|
uniqueCommands_.push_back({"help", helpCmd});
|
|
|
|
for (auto alias : helpCmd->getAliases()) {
|
|
commands_[std::string(alias)] = helpCmd;
|
|
}
|
|
}
|
|
|
|
} // namespace Echoion::Core
|