Files
EchoionCore/include/echoion/core/commands/MoveCommand.hpp
T
faumaray 5c036d91fb making WEB
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-11 00:45:17 +03:00

107 lines
2.5 KiB
C++

#pragma once
#include "Command.hpp"
#include <Void/Log/Log.hpp>
#include <charconv>
#include <format>
namespace Echoion::Core
{
class MoveCommand : 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: move <from> <to>\nExample: move 5 1 "
"(moves track 5 to position 1)"));
return;
}
// Parse arguments: from and to positions
uint32_t from = 0, to = 0;
auto spacePos = ctx.args.find(' ');
if (spacePos == std::string::npos) {
ctx.reply(ctx.tr(
"Please provide both from and to positions."));
return;
}
auto result1 = std::from_chars(
ctx.args.data(), ctx.args.data() + spacePos, from);
auto result2 =
std::from_chars(ctx.args.data() + spacePos + 1,
ctx.args.data() + ctx.args.size(),
to);
if (result1.ec != std::errc() ||
result2.ec != std::errc() || from == 0 || to == 0) {
ctx.reply(
ctx.tr("Please provide valid position numbers "
"(starting from 1)."));
return;
}
auto moveResult = manager->moveInQueue(
from - 1, to - 1); // Convert to 0-based
if (!moveResult) {
Void::Log::error(
"MoveCommand", "{}",
std::format("Move command failed: {}",
moveResult.error().message));
if (moveResult.error().code ==
Api::ErrorCode::NotFound) {
ctx.reply(ctx.tr(
"One of those positions doesn't exist in "
"the queue."));
} else {
ctx.reply(ctx.tr(
"Couldn't move that track. Please try again."));
}
return;
}
ctx.reply(ctx.trf("Moved track from position {} to {}.",
from, to));
} catch (const std::exception& e) {
Void::Log::error(
"MoveCommand", "{}",
std::format("Move command exception: {}", e.what()));
ctx.reply(
ctx.tr("Something went wrong. Please try again."));
}
}
std::string_view getDescription() const override
{
return "Moves a track to a different position in the queue.";
}
std::string_view getUsage() const override { return "<from> <to>"; }
CommandCategory getCategory() const override
{
return CommandCategory::Queue;
}
std::vector<std::string_view> getAliases() const override
{
return {"mv"};
}
};
} // namespace Echoion::Core