Files
faumaray 5c036d91fb making WEB
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-11 00:45:17 +03:00

154 lines
3.9 KiB
C++

#pragma once
#include "Command.hpp"
#include <Void/Log/Log.hpp>
#include <charconv>
#include <chrono>
#include <format>
namespace Echoion::Core
{
class SeekCommand : 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: seek <+/-seconds>\nExamples:\n seek +30 "
"(skip forward 30s)\n seek -10 (skip back 10s)\n "
"seek 60 (jump to 1 minute mark)"));
return;
}
// Parse the seek argument (supports +30, -10, or absolute
// 60)
std::string_view arg = ctx.args;
bool relative = false;
bool forward = true;
if (arg.starts_with('+')) {
relative = true;
forward = true;
arg = arg.substr(1);
} else if (arg.starts_with('-')) {
relative = true;
forward = false;
arg = arg.substr(1);
}
int seconds = 0;
auto parseResult = std::from_chars(
arg.data(), arg.data() + arg.size(), seconds);
if (parseResult.ec != std::errc() || seconds < 0) {
ctx.reply(ctx.tr(
"Please provide a valid number of "
"seconds.\nExamples: seek +30, seek -10, seek 60"));
return;
}
std::chrono::milliseconds targetPosition;
std::string positionStr;
if (relative) {
// Get current position from playback state
auto state = manager->getPlaybackState();
if (!state) {
ctx.reply(ctx.tr(
"Unable to get current playback position."));
return;
}
auto currentPos = state->position;
if (forward) {
targetPosition =
currentPos + std::chrono::seconds(seconds);
positionStr = std::format("+{}s", seconds);
} else {
// Seek backward
if (currentPos.count() < seconds * 1000) {
targetPosition = std::chrono::milliseconds(0);
} else {
targetPosition =
currentPos - std::chrono::seconds(seconds);
}
positionStr = std::format("-{}s", seconds);
}
} else {
// Absolute seek (legacy behavior)
targetPosition = std::chrono::seconds(seconds);
auto mins = seconds / 60;
auto secs = seconds % 60;
positionStr = std::format("{}:{:02}", mins, secs);
}
auto result = manager->seek(targetPosition);
if (!result) {
Void::Log::error("SeekCommand", "{}",
std::format("Seek command failed: {}",
result.error().message));
if (result.error().code ==
Api::ErrorCode::InvalidArgument) {
ctx.reply(ctx.tr("Nothing is playing right now."));
} else if (result.error().code ==
Api::ErrorCode::NotSupported) {
ctx.reply(
ctx.tr("This track doesn't support seeking."));
} else {
ctx.reply(
ctx.tr("Couldn't seek. Please try again."));
}
return;
}
if (relative) {
ctx.reply(ctx.trf("Seeked {} from current position.",
positionStr));
} else {
ctx.reply(
ctx.trf("Seeked to position {}.", positionStr));
}
} catch (const std::exception& e) {
Void::Log::error(
"SeekCommand", "{}",
std::format("Seek command exception: {}", e.what()));
ctx.reply(
ctx.tr("Something went wrong. Please try again."));
}
}
std::string_view getDescription() const override
{
return "Seeks to a position in the current track. Use +seconds "
"to skip forward, -seconds to skip backward.";
}
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