From 11537215467f5dbeb10a896c0b2b701e83e31630 Mon Sep 17 00:00:00 2001 From: Aleksey Selihov Date: Tue, 27 Jan 2026 23:29:04 +0300 Subject: [PATCH] go into command manager Signed-off-by: Aleksey Selihov --- include/Core/Command/Interface.hpp | 73 ++++++++++++ include/Core/Command/Play.hpp | 81 +++++++++++++ include/Core/Command/Playlist.hpp | 45 +++++++ include/Core/Command/Queue.hpp | 51 ++++++++ include/Core/Command/Select.hpp | 45 +++++++ include/Core/Command/Skip.hpp | 31 +++++ include/Core/Command/Stop.hpp | 28 +++++ include/Core/Command/Subscribe.hpp | 41 +++++++ include/Core/Command/Unsubscribe.hpp | 39 +++++++ include/Core/CommandManager.hpp | 57 +++++++++ include/Core/Plugin.hpp | 9 +- include/Core/Youtube/Client.hpp | 1 - include/Core/ts6bot/Config.hpp | 67 ++++++----- src/Core/CommandManager.cpp | 160 +++++++++++++++++++++++++ src/Core/MusicManager.cpp | 37 +++--- src/Core/Plugin.cpp | 169 +++------------------------ src/Core/Spotify/Client.cpp | 6 +- src/Core/Youtube/Client.cpp | 37 +++--- src/Plugin/Plugin.cpp | 4 +- 19 files changed, 752 insertions(+), 229 deletions(-) create mode 100644 include/Core/Command/Interface.hpp create mode 100644 include/Core/Command/Play.hpp create mode 100644 include/Core/Command/Playlist.hpp create mode 100644 include/Core/Command/Queue.hpp create mode 100644 include/Core/Command/Select.hpp create mode 100644 include/Core/Command/Skip.hpp create mode 100644 include/Core/Command/Stop.hpp create mode 100644 include/Core/Command/Subscribe.hpp create mode 100644 include/Core/Command/Unsubscribe.hpp create mode 100644 include/Core/CommandManager.hpp create mode 100644 src/Core/CommandManager.cpp diff --git a/include/Core/Command/Interface.hpp b/include/Core/Command/Interface.hpp new file mode 100644 index 0000000..e611b4f --- /dev/null +++ b/include/Core/Command/Interface.hpp @@ -0,0 +1,73 @@ +#pragma once + +#include +#include +#include +#include +#include + +enum class CommandCategory { Music, Session, Utility }; + +inline std::string to_string(CommandCategory category) +{ + switch (category) { + + case CommandCategory::Music: + return "Music"; + case CommandCategory::Session: + return "Session"; + case CommandCategory::Utility: + return "Utility"; + } + return "Utility"; +} +/** + * @brief Context passed to every command execution. + * Wraps all dependencies required to perform an action. + */ +struct CommandContext { + std::uint64_t serverConnectionHandlerID; + anyID clientID; + std::string args; // Arguments excluding the command name + std::string_view invokerName; + + // Dependencies + std::shared_ptr ts3; + std::weak_ptr musicManager; // Non-owning reference + + // Helper to reply to the user + void reply(std::string_view message) const; +}; + +/** + * @brief Interface that all commands must implement. + */ +class Command +{ + public: + + virtual ~Command() = default; + + /** + * @brief The logic of the command. + */ + virtual void execute(const CommandContext& ctx) = 0; + + /** + * @brief Returns the description used for .help generation. + */ + virtual std::string_view getDescription() const = 0; + + /** + * @brief Returns usage syntax (e.g., ""). + */ + virtual std::string_view getUsage() const = 0; + + virtual CommandCategory getCategory() const + { + return CommandCategory::Utility; + } + + // Commands can now define their own aliases for the help menu + virtual std::vector getAliases() const { return {}; } +}; diff --git a/include/Core/Command/Play.hpp b/include/Core/Command/Play.hpp new file mode 100644 index 0000000..d6f34b4 --- /dev/null +++ b/include/Core/Command/Play.hpp @@ -0,0 +1,81 @@ +#pragma once +#include +#include +#include +#include + +// --- Play Command --- +class Play : public Command +{ + public: + + void execute(const CommandContext& ctx) override + { + if (ctx.musicManager.expired()) { + LOG_ERROR("MusicManager is expired"); + return; + } + + std::string args(ctx.args); + std::string invoker(ctx.invokerName); + + // 1. Handle Direct URL + if (args.find("http") == 0) { + if (ctx.musicManager.lock()->playUrl(args, invoker)) { + ctx.reply("Added URL to queue."); + } else { + ctx.reply("Failed to add URL."); + } + return; + } + + // 2. Handle Search + auto results = ctx.musicManager.lock()->search(args, 5); + if (results.empty()) { + ctx.reply("No results found."); + return; + } + + if (results.size() == 1) { + if (ctx.musicManager.lock()->playUrl(results[0].url, invoker)) { + ctx.reply(std::format("Added: [{}]({})", results[0].name, + results[0].shortUrl)); + } else { + ctx.reply("Failed to add result."); + } + } else { + for (auto& result : results) + { + LOG_INFO("storing result: {}", result.url); + } + ctx.musicManager.lock()->storePendingSearch(ctx.clientID, + results); + + std::string list = + "Multiple results found. Use .select\\.sel :\n"; + for (size_t i = 0; i < results.size(); ++i) { + list += std::format("{}. [{}]({}) [{}]\n", (i + 1), + results[i].name, results[i].shortUrl, + results[i].length); + } + ctx.reply(list); + } + } + + std::string_view getDescription() const override + { + return "Plays a URL or searches YouTube."; + } + std::string_view getUsage() const override + { + return ""; + } + CommandCategory getCategory() const override + { + return CommandCategory::Music; + } + std::vector getAliases() const override + { + return {"p", "add"}; + } +}; diff --git a/include/Core/Command/Playlist.hpp b/include/Core/Command/Playlist.hpp new file mode 100644 index 0000000..6a7b82b --- /dev/null +++ b/include/Core/Command/Playlist.hpp @@ -0,0 +1,45 @@ +#pragma once +#include +#include +#include +#include + +class Playlist : public Command +{ + public: + + void execute(const CommandContext& ctx) override + { + if (ctx.musicManager.expired()) { + LOG_ERROR("MusicManager is expired"); + return; + } + + std::string args(ctx.args); + std::string invoker(ctx.invokerName); + + if (args.find("http") == 0) { + if (auto plId = + ctx.musicManager.lock()->extractPlaylistId(args)) { + if (ctx.musicManager.lock()->playPlaylist(*plId, invoker)) { + ctx.reply("Added PlayList to queue."); + } else { + ctx.reply("Failed to add playlist into the queue"); + } + } + } + } + CommandCategory getCategory() const override + { + return CommandCategory::Music; + } + std::vector getAliases() const override { return {}; } + std::string_view getDescription() const override + { + return "Plays all songs from URL of YouTube playlist"; + } + std::string_view getUsage() const override + { + return ""; + } +}; diff --git a/include/Core/Command/Queue.hpp b/include/Core/Command/Queue.hpp new file mode 100644 index 0000000..81f2d40 --- /dev/null +++ b/include/Core/Command/Queue.hpp @@ -0,0 +1,51 @@ +#pragma once +#include +#include +#include +#include + +class Queue : public Command +{ + public: + + void execute(const CommandContext& ctx) override + { + if (ctx.musicManager.expired()) { + return; + } + + auto q = ctx.musicManager.lock()->getQueue(); + auto current = ctx.musicManager.lock()->getCurrent(); + + std::string str; + if (current) { + str = + std::format("Now playing: [{}]({}) [{}]\n\n", current->name, + current->shortUrl, current->length); + } + + if (q.empty() && str.empty()) { + ctx.reply("Queue is empty."); + return; + } + + for (size_t i = 0; i < q.size(); ++i) { + str += std::format("{}. [{}]({}) [{}]\n", i + 1, q[i].name, + q[i].shortUrl, q[i].length); + } + ctx.reply(str); + } + std::string_view getDescription() const override + { + return "Shows the current queue."; + } + CommandCategory getCategory() const override + { + return CommandCategory::Music; + } + std::vector getAliases() const override + { + return {"list", "q", "l"}; + } + std::string_view getUsage() const override { return ""; } +}; diff --git a/include/Core/Command/Select.hpp b/include/Core/Command/Select.hpp new file mode 100644 index 0000000..88850f0 --- /dev/null +++ b/include/Core/Command/Select.hpp @@ -0,0 +1,45 @@ +#pragma once +#include +#include +#include + +class Select : public Command +{ + public: + + void execute(const CommandContext& ctx) override + { + if (ctx.musicManager.expired()) { + return; + } + + int idx = 0; + // High-performance string-to-int conversion + auto res = std::from_chars(ctx.args.data(), + ctx.args.data() + ctx.args.size(), idx); + + if (res.ec == std::errc()) { + if (ctx.musicManager.lock()->selectSearchResult( + ctx.clientID, idx, std::string(ctx.invokerName))) { + ctx.reply("Selected song added to queue."); + } else { + ctx.reply("Invalid selection index."); + } + } else { + ctx.reply("Please provide a valid number."); + } + } + CommandCategory getCategory() const override + { + return CommandCategory::Music; + } + std::vector getAliases() const override + { + return {"sel"}; + } + std::string_view getDescription() const override + { + return "Selects a song from search results."; + } + std::string_view getUsage() const override { return ""; } +}; diff --git a/include/Core/Command/Skip.hpp b/include/Core/Command/Skip.hpp new file mode 100644 index 0000000..d8e1dd3 --- /dev/null +++ b/include/Core/Command/Skip.hpp @@ -0,0 +1,31 @@ +#pragma once +#include +#include +#include +#include + +class Skip : public Command +{ + public: + + void execute(const CommandContext& ctx) override + { + if (!ctx.musicManager.expired()) { + ctx.musicManager.lock()->skip(); + ctx.reply("Skipped current track."); + } + } + std::string_view getDescription() const override + { + return "Skips current playing track."; + } + std::string_view getUsage() const override { return ""; } + CommandCategory getCategory() const override + { + return CommandCategory::Music; + } + std::vector getAliases() const override + { + return {"next"}; + } +}; diff --git a/include/Core/Command/Stop.hpp b/include/Core/Command/Stop.hpp new file mode 100644 index 0000000..b693565 --- /dev/null +++ b/include/Core/Command/Stop.hpp @@ -0,0 +1,28 @@ +#pragma once +#include +#include +#include +#include + +class Stop : public Command +{ + public: + + void execute(const CommandContext& ctx) override + { + if (!ctx.musicManager.expired()) { + ctx.musicManager.lock()->stop(); + ctx.reply("Playback stopped and queue cleared."); + } + } + std::string_view getDescription() const override + { + return "Stops playback and clears queue."; + } + CommandCategory getCategory() const override + { + return CommandCategory::Music; + } + std::vector getAliases() const override { return {}; } + std::string_view getUsage() const override { return ""; } +}; diff --git a/include/Core/Command/Subscribe.hpp b/include/Core/Command/Subscribe.hpp new file mode 100644 index 0000000..194dd5c --- /dev/null +++ b/include/Core/Command/Subscribe.hpp @@ -0,0 +1,41 @@ +#pragma once +#include +#include +#include +#include + +// --- Play Command --- +class Subscribe : public Command +{ + public: + + void execute(const CommandContext& ctx) override + { + if (ctx.musicManager.expired()) { + LOG_ERROR("MusicManager is expired"); + return; + } + + std::string args(ctx.args); + std::string invoker(ctx.invokerName); + + if (ctx.musicManager.lock()->subscribe(ctx.clientID)) { + ctx.reply("You are now subscribed to the music bot."); + } else { + ctx.reply("Already subscribed to the music bot."); + } + } + CommandCategory getCategory() const override + { + return CommandCategory::Session; + } + std::vector getAliases() const override + { + return {"sub"}; + } + std::string_view getDescription() const override + { + return "Subscribe on playback from bot."; + } + std::string_view getUsage() const override { return ""; } +}; diff --git a/include/Core/Command/Unsubscribe.hpp b/include/Core/Command/Unsubscribe.hpp new file mode 100644 index 0000000..4d37392 --- /dev/null +++ b/include/Core/Command/Unsubscribe.hpp @@ -0,0 +1,39 @@ +#pragma once +#include +#include +#include +#include + +// --- Play Command --- +class UnSubscribe : public Command +{ + public: + + void execute(const CommandContext& ctx) override + { + if (ctx.musicManager.expired()) { + LOG_ERROR("MusicManager is expired"); + return; + } + + if (ctx.musicManager.lock()->unSubscribe(ctx.clientID)) { + ctx.reply("Unsubscribed."); + } else { + ctx.reply("Already not subscribed."); + } + } + + CommandCategory getCategory() const override + { + return CommandCategory::Session; + } + std::vector getAliases() const override + { + return {"unsub"}; + } + std::string_view getDescription() const override + { + return "UnSubscribe from playback from bot."; + } + std::string_view getUsage() const override { return ""; } +}; diff --git a/include/Core/CommandManager.hpp b/include/Core/CommandManager.hpp new file mode 100644 index 0000000..fa81e9e --- /dev/null +++ b/include/Core/CommandManager.hpp @@ -0,0 +1,57 @@ +#pragma once + +#include +#include +#include +#include +#include + +class CommandManager +{ + public: + + explicit CommandManager(std::shared_ptr ts3); + + /** + * @brief Registers a command class. + * @tparam T The command class (must inherit ICommand). + * @param names List of triggers (e.g., {"play", "p"}). + */ + template + void registerCommand(const std::string& primaryName) + { + auto cmd = std::make_shared(); + + // Register primary + m_commands[primaryName] = cmd; + m_uniqueCommands.push_back({primaryName, cmd}); + + // Register aliases automatically from the class itself + for (auto alias : cmd->getAliases()) { + m_commands[std::string(alias)] = cmd; + } + } + + bool dispatch(uint64 scHandlerID, anyID clientID, + std::string_view invokerName, std::string_view message, + std::weak_ptr musicManager); + + private: + + struct CommandEntry { + std::string primaryName; + std::shared_ptr instance; + }; + void registerAllDefaults(); + + // Internal Help Command (defined inside manager to access registry) + class HelpCommand; + + std::shared_ptr m_ts3; + + // Map for fast lookup: "play" -> PlayCommand* + std::unordered_map> m_commands; + + // List for generating help: {"play", PlayCommand*} + std::vector m_uniqueCommands; +}; diff --git a/include/Core/Plugin.hpp b/include/Core/Plugin.hpp index 8a78895..c2c0142 100644 --- a/include/Core/Plugin.hpp +++ b/include/Core/Plugin.hpp @@ -1,4 +1,5 @@ #pragma once +#include "CommandManager.hpp" #include "PluginInterface.hpp" // #include "Youtube/Client.hpp" #include "ts6bot/AudioDecoder.hpp" @@ -14,13 +15,15 @@ class EchoionCore : public IEchoionPlugin { // Dependencies std::shared_ptr m_ts3; - std::unique_ptr m_musicManager; + std::shared_ptr m_musicManager; + std::unique_ptr m_commandManager; public: explicit EchoionCore(std::shared_ptr funcs) { m_ts3 = std::move(funcs); + m_commandManager = std::make_unique(m_ts3); } void Initialize(bool start = true) override; @@ -633,10 +636,10 @@ class EchoionCore : public IEchoionPlugin }; extern "C" { -IEchoionPlugin *CreatePlugin(const TS3Functions *funcs) + IEchoionPlugin *CreatePlugin(const TS3Functions *funcs) { return new EchoionCore(std::make_shared(*funcs)); } -void DestroyPlugin(const IEchoionPlugin *plugin) { delete plugin; } + void DestroyPlugin(const IEchoionPlugin *plugin) { delete plugin; } } diff --git a/include/Core/Youtube/Client.hpp b/include/Core/Youtube/Client.hpp index f13b011..f9427ae 100644 --- a/include/Core/Youtube/Client.hpp +++ b/include/Core/Youtube/Client.hpp @@ -2,7 +2,6 @@ // Standard Library #include #include -#include #include #include #include diff --git a/include/Core/ts6bot/Config.hpp b/include/Core/ts6bot/Config.hpp index 8f9f2c9..80ad37b 100644 --- a/include/Core/ts6bot/Config.hpp +++ b/include/Core/ts6bot/Config.hpp @@ -49,37 +49,6 @@ namespace ts6bot MusicConfig music{}; ProxyConfiguration proxy{}; }; - - class Configuration : public Singleton - { - friend class Singleton; - - public: - - const Config getConfig() const noexcept { return m_config; } - - Configuration(std::string path) - { - auto config = Config(); - std::string buffer; - auto ec = glz::read_file_json(config, path, buffer); - if (ec) { - std::string descriptive_error = - glz::format_error(ec, buffer); - std::cerr << "Not found configuration in " << path - << " using default" << std::endl; - std::cerr << descriptive_error << std::endl; - std::cerr << ec.ec << std::endl; - m_config = Config(); - } - this->m_config = config; - } - - Config m_config{}; - }; - - // Glaze meta (explicit, stable) - } // namespace ts6bot template <> struct glz::meta { using enum ProxyType; @@ -88,7 +57,7 @@ template <> struct glz::meta { template <> struct glz::meta { using enum ts6bot::LogLevel; static constexpr auto value = - enumerate(Critical, Error, Warning, Info, Trace); + enumerate(Trace, Debug, Info, Warning, Error, Critical, None); }; template <> struct glz::meta { using T = ts6bot::ServerQueryConfig; @@ -124,3 +93,37 @@ template <> struct glz::meta { "password", &T::password, "loggingLevel", &T::loggingLevel, "afk", &T::afk, "music", &T::music, "proxy", &T::proxy); }; +namespace ts6bot +{ + class Configuration : public Singleton + { + friend class Singleton; + + public: + + const Config getConfig() const noexcept { return m_config; } + + Configuration(std::string path) + { + auto config = Config(); + std::string buffer; + auto ec = glz::read_file_json(config, path, buffer); + if (ec) { + std::string descriptive_error = + glz::format_error(ec, buffer); + std::cerr << "Not found configuration in " << path + << " using default" << std::endl; + std::cerr << descriptive_error << std::endl; + std::cerr << ec.ec << std::endl; + m_config = Config(); + } + this->m_config = config; + } + + Config m_config{}; + }; + + // Glaze meta (explicit, stable) + +} // namespace ts6bot + diff --git a/src/Core/CommandManager.cpp b/src/Core/CommandManager.cpp new file mode 100644 index 0000000..e9ae675 --- /dev/null +++ b/src/Core/CommandManager.cpp @@ -0,0 +1,160 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Implement Context Reply Helper +void CommandContext::reply(std::string_view message) const +{ + if (!ts3) { + return; + } + std::string safeMsg(message); + ts3->requestSendPrivateTextMsg(serverConnectionHandlerID, safeMsg.c_str(), + clientID, nullptr); +} + +// --- Dynamic Help Command --- +class CommandManager::HelpCommand : public Command +{ + CommandManager& m_manager; + + public: + + explicit HelpCommand(CommandManager& mgr) : m_manager(mgr) {} + + void execute(const CommandContext& ctx) override + { + // Use # for large headers and ** for emphasis + std::string helpMsg = "# 🎵 Echoion Bot Manual\n\n"; + + // Grouping logic remains the same + std::map< + CommandCategory, + std::vector>>> + grouped; + for (const auto& [name, cmd] : m_manager.m_uniqueCommands) { + grouped[cmd->getCategory()].push_back({name, cmd}); + } + + for (auto const& [cat, cmds] : grouped) { + std::string catName = to_string(cat); + helpMsg += std::format("## {} Commands\n\n", catName); + + for (const auto& [name, cmd] : cmds) { + // Formatting: **.command** `usage` + if (!cmd->getUsage().empty()) { + helpMsg += std::format("** .{} ** ` {} `\n", name, + cmd->getUsage()); + } else { + helpMsg += std::format("** .{} **\n", name); + } + + // Description in a separate line + helpMsg += std::format("{}\n", cmd->getDescription()); + + // Aliases using the standard italic/bold combo + auto aliases = cmd->getAliases(); + if (!aliases.empty()) { + helpMsg += "__Aliases:__ "; + for (size_t i = 0; i < aliases.size(); ++i) { + helpMsg += std::format( + "**{}**{}", aliases[i], + (i == aliases.size() - 1 ? "" : ", ")); + } + helpMsg += "\n"; + } + + // Double newline for spacing between commands + helpMsg += "\n"; + } + } + + ctx.reply(helpMsg); + } + std::string_view getDescription() const override + { + return "Displays this help message."; + } + std::string_view getUsage() const override { return ""; } + std::vector getAliases() const override + { + return {"commands", "h"}; + } +}; + +// --- Manager Implementation --- + +CommandManager::CommandManager(std::shared_ptr ts3) + : m_ts3(std::move(ts3)) +{ + registerAllDefaults(); +} + +void CommandManager::registerAllDefaults() +{ + registerCommand("play"); + registerCommand("playlist"); + registerCommand