100 lines
2.4 KiB
C++
100 lines
2.4 KiB
C++
#pragma once
|
|
|
|
#include "Command.hpp"
|
|
#include "echoion/core/Logger.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(
|
|
"The music service is currently unavailable.");
|
|
return;
|
|
}
|
|
|
|
if (ctx.args.empty()) {
|
|
ctx.reply("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("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("Please provide valid position numbers "
|
|
"(starting from 1).");
|
|
return;
|
|
}
|
|
|
|
auto moveResult = manager->moveInQueue(
|
|
from - 1, to - 1); // Convert to 0-based
|
|
if (!moveResult) {
|
|
ECHOION_LOG_ERROR(
|
|
std::format("Move command failed: {}",
|
|
moveResult.error().message));
|
|
|
|
if (moveResult.error().code ==
|
|
Api::ErrorCode::NotFound) {
|
|
ctx.reply("One of those positions doesn't exist in "
|
|
"the queue.");
|
|
} else {
|
|
ctx.reply(
|
|
"Couldn't move that track. Please try again.");
|
|
}
|
|
return;
|
|
}
|
|
|
|
ctx.reply(std::format(
|
|
"Moved track from position {} to {}.", from, to));
|
|
} catch (const std::exception& e) {
|
|
ECHOION_LOG_ERROR(
|
|
std::format("Move command exception: {}", e.what()));
|
|
ctx.reply("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
|