many different fixes

This commit is contained in:
faumaray committed 2026-02-03 05:07:15 +03:00
1 parent a583c17916
commit 53801f2b53
36 files changed
+5149 -1291

No files matched your search

+205 -15
View File
@@ -1,11 +1,12 @@
#include "echoion/core/CommandManager.hpp"
#include "echoion/api/ConfigTypes.hpp"
#include "echoion/core/MusicManager.hpp"
#include "echoion/core/CommandManager.hpp"
#include "echoion/core/Logger.hpp"
#include <echoion/core/commands/Commands.hpp>
#include "echoion/core/MusicManager.hpp"
#include "echoion/core/commands/Command.hpp"
#include <algorithm>
#include <sstream>
#include <echoion/core/commands/Commands.hpp>
#include <format>
#include <sstream>
namespace Echoion::Core {
@@ -89,6 +90,122 @@ public:
CommandManager::CommandManager() {
registerBuiltinCommands();
registerHelpCommand();
startThreadPool();
}
CommandManager::~CommandManager() {
shutdown();
}
void CommandManager::shutdown() {
{
std::lock_guard<std::mutex> lock(queueMutex_);
shutdown_ = true;
}
queueCv_.notify_all();
for (auto& thread : workerThreads_) {
if (thread.joinable()) {
thread.join();
}
}
workerThreads_.clear();
}
void CommandManager::startThreadPool() {
for (size_t i = 0; i < THREAD_POOL_SIZE; ++i) {
workerThreads_.emplace_back([this]() {
processCommandQueue();
});
}
ECHOION_LOG_INFO(std::format("CommandManager thread pool started with {} workers", THREAD_POOL_SIZE));
}
void CommandManager::processCommandQueue() {
while (true) {
CommandTask task;
{
std::unique_lock<std::mutex> lock(queueMutex_);
queueCv_.wait(lock, [this] { return shutdown_ || !commandQueue_.empty(); });
if (shutdown_ && commandQueue_.empty()) {
return;
}
task = std::move(commandQueue_.front());
commandQueue_.pop();
}
// Execute the command
executeCommandAsync(std::move(task));
}
}
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,
.replyCallback = std::move(task.replyFunc)
};
command->execute(ctx);
} catch (const std::exception& e) {
ECHOION_LOG_ERROR(std::format("Async command execution error: {}", e.what()));
try {
task.replyFunc("An error occurred while executing the command.");
} catch (...) {}
}
}
bool CommandManager::dispatch(
@@ -110,6 +227,73 @@ bool CommandManager::dispatch(
}
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
}
// Queue the command for async execution
CommandTask task{
.clientId = clientId,
.message = std::string(message),
.musicManager = musicManager,
.replyFunc = std::move(replyFunc),
.invokerName = std::string(invokerName)
};
{
std::lock_guard<std::mutex> lock(queueMutex_);
if (shutdown_) {
return false;
}
commandQueue_.push(std::move(task));
}
queueCv_.notify_one();
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)
@@ -141,11 +325,12 @@ bool CommandManager::dispatch(
// Check permissions
if (!canExecuteCommand(clientId, command)) {
replyFunc("You don't have permission to use this command.");
return true; // Command was found but not allowed
return true;
}
CommandContext ctx{
.clientId = clientId,
.prefix = prefix_,
.args = std::move(argsStr),
.invokerName = invokerName,
.musicManager = musicManager,
@@ -155,7 +340,8 @@ bool CommandManager::dispatch(
try {
command->execute(ctx);
} catch (const std::exception& e) {
ECHOION_LOG_ERROR(std::format("Command execution error [{}]: {}", cmdKey, e.what()));
ECHOION_LOG_ERROR(
std::format("Command execution error [{}]: {}", cmdKey, e.what()));
ctx.reply("An unexpected error occurred. Our team has been notified.");
}
@@ -216,12 +402,13 @@ bool CommandManager::canExecuteCommand(uint64_t clientId, const CommandPtr& cmd)
}
// Check permission via callback if set
if (permissionCheck_) {
return permissionCheck_(clientId, cmd->getCategory());
}
// Default: deny admin commands if no checker configured
return false;
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) {
@@ -245,9 +432,9 @@ void CommandManager::registerBuiltinCommands() {
registerCommand<ResumeCommand>("resume");
registerCommand<StopCommand>("stop");
registerCommand<SkipCommand>("skip");
registerCommand<SeekCommand>("seek");
registerCommand<VolumeCommand>("volume");
registerCommand<QueueCommand>("queue");
// registerCommand<SeekCommand>("seek");
registerCommand<VolumeCommand>("volume");
registerCommand<QueueCommand>("queue");
registerCommand<RemoveCommand>("remove");
registerCommand<ClearCommand>("clear");
registerCommand<MoveCommand>("move");
@@ -260,6 +447,9 @@ void CommandManager::registerBuiltinCommands() {
registerCommand<ReloadConfigCommand>("reloadconfig");
registerCommand<SubscribeCommand>("subscribe");
registerCommand<UnsubscribeCommand>("unsubscribe");
registerCommand<SubscribersCommand>("subscribers");
registerCommand<UnsubscribeIdCommand>("unsubscribeid");
registerCommand<UnsubscribeAllCommand>("unsubscribeall");
}
void CommandManager::registerHelpCommand() {