Files
EchoionCore/include/echoion/core/commands/QueueCommand.hpp
T
faumaray 5c036d91fb making WEB
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-11 00:45:17 +03:00

182 lines
4.8 KiB
C++

#pragma once
#include "Command.hpp"
#include <Void/Log/Log.hpp>
#include <format>
#include <sstream>
namespace Echoion::Core
{
class QueueCommand : 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;
}
auto current = manager->getCurrentTrack();
auto queue = manager->getQueue();
auto state = manager->getPlaybackState();
if (!queue) {
Void::Log::error("QueueCommand", "{}",
std::format("Queue command failed: {}",
queue.error().message));
ctx.reply(ctx.tr("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 (const std::exception&) {
page = 1;
}
}
constexpr int ITEMS_PER_PAGE = 10;
int totalPages =
(static_cast<int>(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<int>(queue->size()));
std::stringstream msg;
// Same at-a-glance status footer as !help and !np. A
// "Paused" marker is prepended since the queue view is
// where a stopped playhead is most relevant.
if (std::string status = playbackStatusLine(ctx);
!status.empty()) {
if (state && state->isPaused) {
msg << ctx.tr("\xe2\x8f\xb8 Paused\n");
}
msg << status;
}
// Current track
if (current && current->has_value()) {
const auto& track = current->value();
const auto& trackInfo = track.track;
// Build time display: · current/total or · total
// (no [] — TeamSpeak parses it as a link reference).
std::string timeStr;
if (trackInfo.duration.has_value() &&
trackInfo.duration->count() > 0) {
if (state) {
timeStr = std::format(
" {} {}/{}", kDot,
formatDuration(state->position),
formatDuration(*trackInfo.duration));
} else {
timeStr = std::format(
" {} {}", kDot,
formatDuration(*trackInfo.duration));
}
}
std::string displayText = trackInfo.title;
if (!trackInfo.artist.empty()) {
displayText = std::format(
"{} by {}", trackInfo.title, trackInfo.artist);
}
msg << ctx.trf(
"\n**Now Playing:** {}{}\n",
markdownLink(displayText, trackLinkUrl(trackInfo)),
timeStr);
}
// Queue
if (queue->empty()) {
msg << ctx.trf(
"\nThe queue is empty. Add songs with {}play !",
ctx.prefix);
} else {
msg << ctx.trf("\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);
}
msg << ctx.trf(
"{}. {}", i + 1,
markdownLink(displayText,
trackLinkUrl(trackInfo)));
if (trackInfo.duration.has_value()) {
msg << ctx.trf(
" {} {}", kDot,
formatDuration(*trackInfo.duration));
}
if (!entry.addedBy.empty()) {
msg << ctx.trf(" (by {})",
escapeMarkdown(entry.addedBy));
}
msg << ctx.tr("\n");
}
if (totalPages > 1) {
msg << ctx.trf("\nUse {}queue <page> to see more "
"(page {} of {})",
ctx.prefix, page, totalPages);
}
}
msg << ctx.tr("\xf0\x9f\x93\x9c **Queue** (Page ") << page
<< "/" << totalPages << ctx.tr(")\n");
ctx.reply(msg.str());
} catch (const std::exception& e) {
Void::Log::error(
"QueueCommand", "{}",
std::format("Queue command exception: {}", e.what()));
ctx.reply(
ctx.tr("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<std::string_view> getAliases() const override
{
return {"q", "list"};
}
};
} // namespace Echoion::Core