100 lines
2.3 KiB
C++
100 lines
2.3 KiB
C++
#pragma once
|
|
|
|
#include "Command.hpp"
|
|
#include <Void/Log/Log.hpp>
|
|
#include <charconv>
|
|
#include <format>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
|
|
class SelectCommand : public Command
|
|
{
|
|
public:
|
|
|
|
void execute(const CommandContext& ctx) override
|
|
{
|
|
try {
|
|
auto manager = ctx.musicManager.lock();
|
|
if (!manager) {
|
|
ctx.reply(ctx.tr(
|
|
"The music service is currently unavailable."));
|
|
return;
|
|
}
|
|
|
|
if (ctx.args.empty()) {
|
|
ctx.reply(ctx.tr(
|
|
"Usage: select <number>\nSelect a track from "
|
|
"your last search results."));
|
|
return;
|
|
}
|
|
|
|
// Parse index
|
|
uint32_t index = 0;
|
|
auto result =
|
|
std::from_chars(ctx.args.data(),
|
|
ctx.args.data() + ctx.args.size(),
|
|
index);
|
|
if (result.ec != std::errc() || index == 0) {
|
|
ctx.reply(ctx.tr(
|
|
"Please provide a valid number (1, 2, 3, etc.)."));
|
|
return;
|
|
}
|
|
|
|
std::string invokerName(ctx.invokerName);
|
|
auto selectResult = manager->selectSearchResult(
|
|
ctx.clientId, index, invokerName);
|
|
|
|
if (!selectResult) {
|
|
Void::Log::error(
|
|
"SelectCommand", "{}",
|
|
std::format(
|
|
"Select failed for client {} index {}: {}",
|
|
ctx.clientId, index,
|
|
selectResult.error().message));
|
|
|
|
if (selectResult.error().code ==
|
|
Api::ErrorCode::NotFound) {
|
|
ctx.reply(
|
|
ctx.trf("No search results found. Use {}play "
|
|
"<query> to search first.",
|
|
ctx.prefix));
|
|
} else {
|
|
ctx.reply(
|
|
ctx.tr("Couldn't select that track. Please try "
|
|
"again."));
|
|
}
|
|
return;
|
|
}
|
|
|
|
ctx.reply(*selectResult);
|
|
|
|
} catch (const std::exception& e) {
|
|
Void::Log::error(
|
|
"SelectCommand", "{}",
|
|
std::format("Select command exception: {}", e.what()));
|
|
ctx.reply(
|
|
ctx.tr("Something went wrong. Please try again."));
|
|
}
|
|
}
|
|
|
|
std::string_view getDescription() const override
|
|
{
|
|
return "Selects a track from your search results.";
|
|
}
|
|
|
|
std::string_view getUsage() const override { return "<number>"; }
|
|
|
|
CommandCategory getCategory() const override
|
|
{
|
|
return CommandCategory::Music;
|
|
}
|
|
|
|
std::vector<std::string_view> getAliases() const override
|
|
{
|
|
return {"sel", "pick", "choose"};
|
|
}
|
|
};
|
|
|
|
} // namespace Echoion::Core
|