#pragma once #include "Command.hpp" #include "echoion/core/Logger.hpp" #include #include namespace Echoion::Core { class QueueCommand : 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; } auto current = manager->getCurrentTrack(); auto queue = manager->getQueue(); auto state = manager->getPlaybackState(); if (!queue) { ECHOION_LOG_ERROR(std::format( "Queue command failed: {}", queue.error().message)); ctx.reply("Couldn't get queue information."); return; } // Parse page number from args (default to 1) int page = 1; if (!ctx.args.empty()) { try { page = std::stoi(std::string(ctx.args)); if (page < 1) { page = 1; } } catch (...) { page = 1; } } constexpr int ITEMS_PER_PAGE = 10; int totalPages = (static_cast(queue->size()) + ITEMS_PER_PAGE - 1) / ITEMS_PER_PAGE; if (totalPages == 0) { totalPages = 1; } // Clamp page to valid range if (page > totalPages) { page = totalPages; } int startIdx = (page - 1) * ITEMS_PER_PAGE; int endIdx = std::min(startIdx + ITEMS_PER_PAGE, static_cast(queue->size())); std::stringstream msg; msg << "\xf0\x9f\x93\x9c **Queue** (Page " << page << "/" << totalPages << ")\n"; // Current track if (current && current->has_value()) { const auto& track = current->value(); const auto& trackInfo = track.track; // Build time display: [current/total] or [total] std::string timeStr; if (trackInfo.duration.has_value() && trackInfo.duration->count() > 0) { if (state) { timeStr = std::format( " [{}/{}]", formatDuration(state->position), formatDuration(*trackInfo.duration)); } else { timeStr = std::format( " [{}]", formatDuration(*trackInfo.duration)); } } std::string displayText = trackInfo.title; if (!trackInfo.artist.empty()) { displayText = std::format( "{} by {}", trackInfo.title, trackInfo.artist); } std::string linkText = trackInfo.shortenedUrl.has_value() && !trackInfo.shortenedUrl->empty() ? std::format("[{}]({}){}", displayText, *trackInfo.shortenedUrl, timeStr) : displayText + timeStr; msg << std::format("\n**Now Playing:** {}\n", linkText); } // Queue if (queue->empty()) { msg << std::format( "\nThe queue is empty. Add songs with {}play !", ctx.prefix); } else { msg << std::format("\n**Up Next ({} tracks):**\n", queue->size()); for (int i = startIdx; i < endIdx; ++i) { const auto& entry = (*queue)[i]; const auto& trackInfo = entry.track; std::string displayText = trackInfo.title; if (!trackInfo.artist.empty()) { displayText = std::format("{} by {}", trackInfo.title, trackInfo.artist); } std::string linkText = trackInfo.shortenedUrl.has_value() && !trackInfo.shortenedUrl->empty() ? std::format( "[{}]({}) [{}]", displayText, *trackInfo.shortenedUrl, formatDuration(*trackInfo.duration)) : displayText; msg << std::format("{}. {}", i + 1, linkText); if (!entry.addedBy.empty()) { msg << std::format(" (by {})", entry.addedBy); } msg << "\n"; } if (totalPages > 1) { msg << std::format( "\nUse {}queue to see more " "(page {} of {})", ctx.prefix, page, totalPages); } } // State indicators if (state) { msg << "\n\n"; if (state->isPaused) { msg << "\xe2\x8f\xb8 Paused | "; } if (state->shuffle) { msg << "\xf0\x9f\x94\x80 Shuffle | "; } if (state->repeat) { msg << "\xf0\x9f\x94\x81 Repeat | "; } msg << std::format( "Volume: {}%", static_cast(state->volume * 100)); } ctx.reply(msg.str()); } catch (const std::exception& e) { ECHOION_LOG_ERROR( std::format("Queue command exception: {}", e.what())); ctx.reply("Something went wrong. Please try again."); } } std::string_view getDescription() const override { return "Shows the current queue and playback status."; } std::string_view getUsage() const override { return "[page]"; } CommandCategory getCategory() const override { return CommandCategory::Queue; } std::vector getAliases() const override { return {"q", "list"}; } private: static std::string formatDuration(std::chrono::milliseconds duration) { auto totalSeconds = duration.count() / 1000; auto hours = totalSeconds / 3600; auto mins = (totalSeconds % 3600) / 60; auto secs = totalSeconds % 60; if (hours > 0) { return std::format("{}:{:02}:{:02}", hours, mins, secs); } return std::format("{}:{:02}", mins, secs); } }; } // namespace Echoion::Core