go into command manager
Signed-off-by: Aleksey Selihov <faumaray@gmail.com>
This commit is contained in:
1 parent
b94b634a87
commit
1153721546
19 files changed
+752
-229
No files matched your search
@@ -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 {}; }
|
||||
};
|
||||
@@ -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"};
|
||||
}
|
||||
};
|
||||
@@ -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=\">";
|
||||
}
|
||||
};
|
||||
@@ -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 ""; }
|
||||
};
|
||||
@@ -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>"; }
|
||||
};
|
||||
@@ -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"};
|
||||
}
|
||||
};
|
||||
@@ -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 ""; }
|
||||
};
|
||||
@@ -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 ""; }
|
||||
};
|
||||
@@ -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 ""; }
|
||||
};
|
||||
Reference in new issue
Block a user