#pragma once #include "Command.hpp" #include #include #include #include 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>\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( "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( "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("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; } if (relative) { ctx.reply(std::format( "Seeked {} from current position.", positionStr)); } else { ctx.reply( std::format("Seeked to position {}.", positionStr)); } } catch (const std::exception& e) { Void::Log::error("SeekCommand", "{}", 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. 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 getAliases() const override { return {"jump"}; } }; } // namespace Echoion::Core