Files
EchoionCore/include/echoion/core/commands/QueueCommand.hpp
T
faumarayandClaude Opus 4.8 b9c6c5dbc9 commands: fix result/queue formatting for TeamSpeak markdown
The reply lists rendered badly: durations written as [0:00] were parsed as
Markdown link references (brackets stripped), and titles containing Markdown
punctuation broke the surrounding [title](url) link so it showed raw.

Add shared escapeMarkdown()/markdownLink() helpers (Command.hpp) and reformat
search, play, nowplaying and queue replies to:
  N. [escaped title](shortUrl) · duration
Titles/nicknames are Markdown-escaped so '[', '*', '_', etc. can't break the
link; the duration follows a middot separator instead of [..]. Also guard a
possible nullopt duration deref in the queue list.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-26 03:05:38 +03:00

204 lines
5.2 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(
"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("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<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;
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
// (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 << std::format(
"\n**Now Playing:** {}{}\n",
markdownLink(displayText, trackInfo.shortenedUrl),
timeStr);
}
// 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);
}
msg << std::format(
"{}. {}", i + 1,
markdownLink(displayText,
trackInfo.shortenedUrl));
if (trackInfo.duration.has_value()) {
msg << std::format(
" {} {}", kDot,
formatDuration(*trackInfo.duration));
}
if (!entry.addedBy.empty()) {
msg << std::format(
" (by {})", escapeMarkdown(entry.addedBy));
}
msg << "\n";
}
if (totalPages > 1) {
msg << std::format(
"\nUse {}queue <page> 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<int>(state->volume * 100));
}
ctx.reply(msg.str());
} catch (const std::exception& e) {
Void::Log::error("QueueCommand", "{}",
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<std::string_view> 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