Files
EchoionCore/include/echoion/core/commands/SeekCommand.hpp
T
2026-02-01 23:04:49 +03:00

85 lines
2.7 KiB
C++

#pragma once
#include "Command.hpp"
#include "echoion/core/Logger.hpp"
#include <format>
#include <chrono>
#include <charconv>
namespace Echoion::Core {
class SeekCommand : 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: seek <seconds>\nExample: seek 60 (jumps to 1 minute)");
return;
}
int seconds = 0;
auto parseResult = std::from_chars(ctx.args.data(),
ctx.args.data() + ctx.args.size(),
seconds);
if (parseResult.ec != std::errc()) {
ctx.reply("Please provide a valid number of seconds.");
return;
}
if (seconds < 0) {
ctx.reply("Time must be a positive number.");
return;
}
auto result = manager->seek(std::chrono::seconds(seconds));
if (!result) {
ECHOION_LOG_ERROR(std::format("Seek command failed: {}", result.error().message));
if (result.error().code == Api::ErrorCode::InvalidArgument) {
ctx.reply("Nothing is playing right now.");
} else if (result.error().code == Api::ErrorCode::NotSupported) {
ctx.reply("This track doesn't support seeking.");
} else {
ctx.reply("Couldn't seek. Please try again.");
}
return;
}
auto mins = seconds / 60;
auto secs = seconds % 60;
if (mins > 0) {
ctx.reply(std::format("Seeked to {}:{:02}.", mins, secs));
} else {
ctx.reply(std::format("Seeked to {} seconds.", seconds));
}
} catch (const std::exception& e) {
ECHOION_LOG_ERROR(std::format("Seek command exception: {}", e.what()));
ctx.reply("Something went wrong. Please try again.");
}
}
std::string_view getDescription() const override {
return "Seeks to a position in the current track.";
}
std::string_view getUsage() const override {
return "<seconds>";
}
CommandCategory getCategory() const override {
return CommandCategory::Playback;
}
std::vector<std::string_view> getAliases() const override {
return {"jump"};
}
};
} // namespace Echoion::Core