Files
Echoion/include/Core/Command/Play.hpp
T
faumaray 1153721546 go into command manager
Signed-off-by: Aleksey Selihov <faumaray@gmail.com>
2026-01-27 23:29:04 +03:00

82 lines
1.9 KiB
C++

#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"};
}
};