go into command manager

Signed-off-by: Aleksey Selihov <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2026-01-27 23:29:04 +03:00
1 parent b94b634a87
commit 1153721546
19 files changed
+752 -229

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
#pragma once
#include <memory>
#include <string_view>
#include <teamspeak/public_definitions.h>
#include <ts3_functions.h>
#include <ts6bot/MusicManager.hpp>
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<TS3Functions> ts3;
std::weak_ptr<MusicManager> 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., "<url>").
*/
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<std::string_view> getAliases() const { return {}; }
};
+81
View File
@@ -0,0 +1,81 @@
#pragma once
#include <Command/Interface.hpp>
#include <Logger.hpp>
#include <format>
#include <ts6bot/MusicManager.hpp>
// --- 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 <number>:\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 "<url | search term>";
}
CommandCategory getCategory() const override
{
return CommandCategory::Music;
}
std::vector<std::string_view> getAliases() const override
{
return {"p", "add"};
}
};
+45
View File
@@ -0,0 +1,45 @@
#pragma once
#include <Command/Interface.hpp>
#include <charconv>
#include <format>
#include <ts6bot/MusicManager.hpp>
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<std::string_view> 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 "<url which contains \"list=\">";
}
};
+51
View File
@@ -0,0 +1,51 @@
#pragma once
#include <Command/Interface.hpp>
#include <charconv>
#include <format>
#include <ts6bot/MusicManager.hpp>
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<std::string_view> getAliases() const override
{
return {"list", "q", "l"};
}
std::string_view getUsage() const override { return ""; }
};
+45
View File
@@ -0,0 +1,45 @@
#pragma once
#include <Command/Interface.hpp>
#include <charconv>
#include <ts6bot/MusicManager.hpp>
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<std::string_view> 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 "<number>"; }
};
+31
View File
@@ -0,0 +1,31 @@
#pragma once
#include <Command/Interface.hpp>
#include <charconv>
#include <format>
#include <ts6bot/MusicManager.hpp>
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<std::string_view> getAliases() const override
{
return {"next"};
}
};
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <Command/Interface.hpp>
#include <charconv>
#include <format>
#include <ts6bot/MusicManager.hpp>
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<std::string_view> getAliases() const override { return {}; }
std::string_view getUsage() const override { return ""; }
};
+41
View File
@@ -0,0 +1,41 @@
#pragma once
#include <Command/Interface.hpp>
#include <Logger.hpp>
#include <format>
#include <ts6bot/MusicManager.hpp>
// --- 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<std::string_view> getAliases() const override
{
return {"sub"};
}
std::string_view getDescription() const override
{
return "Subscribe on playback from bot.";
}
std::string_view getUsage() const override { return ""; }
};
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include <Command/Interface.hpp>
#include <Logger.hpp>
#include <format>
#include <ts6bot/MusicManager.hpp>
// --- 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<std::string_view> getAliases() const override
{
return {"unsub"};
}
std::string_view getDescription() const override
{
return "UnSubscribe from playback from bot.";
}
std::string_view getUsage() const override { return ""; }
};
+57
View File
@@ -0,0 +1,57 @@
#pragma once
#include <Command/Interface.hpp>
#include <PluginInterface.hpp>
#include <memory>
#include <string>
#include <unordered_map>
class CommandManager
{
public:
explicit CommandManager(std::shared_ptr<TS3Functions> ts3);
/**
* @brief Registers a command class.
* @tparam T The command class (must inherit ICommand).
* @param names List of triggers (e.g., {"play", "p"}).
*/
template <typename T>
void registerCommand(const std::string& primaryName)
{
auto cmd = std::make_shared<T>();
// 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> musicManager);
private:
struct CommandEntry {
std::string primaryName;
std::shared_ptr<Command> instance;
};
void registerAllDefaults();
// Internal Help Command (defined inside manager to access registry)
class HelpCommand;
std::shared_ptr<TS3Functions> m_ts3;
// Map for fast lookup: "play" -> PlayCommand*
std::unordered_map<std::string, std::shared_ptr<Command>> m_commands;
// List for generating help: {"play", PlayCommand*}
std::vector<CommandEntry> m_uniqueCommands;
};
+6 -3
View File
@@ -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<TS3Functions> m_ts3;
std::unique_ptr<MusicManager> m_musicManager;
std::shared_ptr<MusicManager> m_musicManager;
std::unique_ptr<CommandManager> m_commandManager;
public:
explicit EchoionCore(std::shared_ptr<TS3Functions> funcs)
{
m_ts3 = std::move(funcs);
m_commandManager = std::make_unique<CommandManager>(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<TS3Functions>(*funcs));
}
void DestroyPlugin(const IEchoionPlugin *plugin) { delete plugin; }
void DestroyPlugin(const IEchoionPlugin *plugin) { delete plugin; }
}
-1
View File
@@ -2,7 +2,6 @@
// Standard Library
#include <expected>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>
+35 -32
View File
@@ -49,37 +49,6 @@ namespace ts6bot
MusicConfig music{};
ProxyConfiguration proxy{};
};
class Configuration : public Singleton<Configuration>
{
friend class Singleton<Configuration>;
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<ProxyType> {
using enum ProxyType;
@@ -88,7 +57,7 @@ template <> struct glz::meta<ProxyType> {
template <> struct glz::meta<ts6bot::LogLevel> {
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<ts6bot::ServerQueryConfig> {
using T = ts6bot::ServerQueryConfig;
@@ -124,3 +93,37 @@ template <> struct glz::meta<ts6bot::Config> {
"password", &T::password, "loggingLevel", &T::loggingLevel, "afk",
&T::afk, "music", &T::music, "proxy", &T::proxy);
};
namespace ts6bot
{
class Configuration : public Singleton<Configuration>
{
friend class Singleton<Configuration>;
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