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>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-26 03:05:38 +03:00
1 parent 352101e657
commit b9c6c5dbc9
5 files changed
+91 -64

No files matched your search

+43
View File
@@ -5,8 +5,10 @@
#include <Types.hpp>
#include <Void/Service/Registry.hpp>
#include <cstdint>
#include <format>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
@@ -35,6 +37,47 @@ namespace Echoion::Core
return "Utility";
}
// Middot separator (U+00B7) used between a track link and its duration.
inline constexpr std::string_view kDot = "\xc2\xb7";
// Escape Markdown punctuation in dynamic text (track titles, etc.) so it
// cannot break an enclosing [text](url) link or apply stray formatting.
inline std::string escapeMarkdown(std::string_view text)
{
std::string out;
out.reserve(text.size() + 8);
for (char c : text) {
switch (c) {
case '\\':
case '`':
case '*':
case '_':
case '[':
case ']':
case '(':
case ')':
case '~':
case '|':
out += '\\';
[[fallthrough]];
default:
out += c;
}
}
return out;
}
// Render `text` as a Markdown link to `url` (escaping the text). When `url`
// is empty/absent the escaped text is returned without a link.
inline std::string markdownLink(std::string_view text,
const std::optional<std::string>& url)
{
if (url.has_value() && !url->empty()) {
return std::format("[{}]({})", escapeMarkdown(text), *url);
}
return escapeMarkdown(text);
}
/**
* @brief Context passed to every command execution.
* Wraps all dependencies required to perform an action.
@@ -44,33 +44,30 @@ namespace Echoion::Core
displayText = std::format("{} by {}", trackInfo.title,
trackInfo.artist);
}
std::string linkText =
trackInfo.shortenedUrl.has_value() &&
!trackInfo.shortenedUrl->empty()
? std::format("[{}]({})", displayText,
*trackInfo.shortenedUrl)
: displayText;
std::string msg =
std::format("\xf0\x9f\x8e\xb5 {}", linkText);
std::string msg = std::format(
"\xf0\x9f\x8e\xb5 {}",
markdownLink(displayText, trackInfo.shortenedUrl));
// Show current position / total duration
// Show current position / total duration after a middot
// (no [] — TeamSpeak parses it as a link reference).
auto playbackState = manager->getPlaybackState();
if (playbackState &&
playbackState->currentTrack.has_value() &&
trackInfo.duration.has_value() &&
trackInfo.duration->count() > 0) {
auto position = playbackState->position;
msg += std::format(" [{}/{}]",
msg += std::format(" {} {}/{}", kDot,
formatDuration(position),
formatDuration(*trackInfo.duration));
} else if (trackInfo.duration.has_value() &&
trackInfo.duration->count() > 0) {
msg += std::format(" [{}]",
msg += std::format(" {} {}", kDot,
formatDuration(*trackInfo.duration));
}
if (!track.addedBy.empty()) {
msg += std::format(" (requested by {})", track.addedBy);
msg += std::format(" (requested by {})",
escapeMarkdown(track.addedBy));
}
ctx.reply(msg);
+12 -19
View File
@@ -98,13 +98,9 @@ namespace Echoion::Core
displayText = std::format("{} by {}", track.title,
track.artist);
}
std::string linkText =
track.shortenedUrl.has_value() &&
!track.shortenedUrl->empty()
? std::format("[{}]({})", displayText,
*track.shortenedUrl)
: displayText;
ctx.reply(std::format("Added to queue: {}", linkText));
ctx.reply(std::format(
"Added to queue: {}",
markdownLink(displayText, track.shortenedUrl)));
return;
}
@@ -112,10 +108,9 @@ namespace Echoion::Core
manager->storePendingSearch(ctx.clientId,
searchResult->tracks);
std::string msg =
std::format("\xf0\x9f\x94\x8d Multiple results "
"found. Use {}select <number>:\n",
ctx.prefix);
std::string msg = std::format(
"Multiple results \xe2\x80\x94 use {}select <number>:\n\n",
ctx.prefix);
for (size_t i = 0; i < searchResult->tracks.size() && i < 5;
++i) {
const auto& track = searchResult->tracks[i];
@@ -124,15 +119,13 @@ namespace Echoion::Core
displayText = std::format("{} by {}", track.title,
track.artist);
}
std::string linkText =
track.shortenedUrl.has_value() &&
!track.shortenedUrl->empty()
? std::format("[{}]({})", displayText,
*track.shortenedUrl)
: displayText;
msg += std::format("{}. {}", i + 1, linkText);
// [escaped title](url) · duration (no [] around the
// duration: TeamSpeak parses [..] as a link reference).
msg += std::format(
"{}. {}", i + 1,
markdownLink(displayText, track.shortenedUrl));
if (track.duration.has_value()) {
msg += std::format(" [{}]",
msg += std::format(" {} {}", kDot,
formatDuration(*track.duration));
}
msg += "\n";
+19 -20
View File
@@ -72,18 +72,19 @@ namespace Echoion::Core
const auto& track = current->value();
const auto& trackInfo = track.track;
// Build time display: [current/total] or [total]
// 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));
}
}
@@ -93,13 +94,10 @@ namespace Echoion::Core
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);
msg << std::format(
"\n**Now Playing:** {}{}\n",
markdownLink(displayText, trackInfo.shortenedUrl),
timeStr);
}
// Queue
@@ -119,17 +117,18 @@ namespace Echoion::Core
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);
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 {})", entry.addedBy);
msg << std::format(
" (by {})", escapeMarkdown(entry.addedBy));
}
msg << "\n";
}
@@ -53,29 +53,24 @@ namespace Echoion::Core
manager->storePendingSearch(ctx.clientId, result->tracks);
std::stringstream msg;
msg << "\xf0\x9f\x94\x8d Search results for \"" << ctx.args
<< "\":\n\n";
msg << "Search results for \"" << ctx.args << "\":\n\n";
for (size_t i = 0; i < result->tracks.size() && i < 5;
++i) {
const auto& track = result->tracks[i];
// Format as [Title by Artist](shortenedUrl) with
// duration
std::string displayText = track.title;
if (!track.artist.empty()) {
displayText = std::format("{} by {}", track.title,
track.artist);
}
std::string linkText =
track.shortenedUrl.has_value() &&
!track.shortenedUrl->empty()
? std::format("[{}]({})", displayText,
*track.shortenedUrl)
: displayText;
msg << std::format("{}. {}", i + 1, linkText);
// [escaped title](url) · duration (no [] around the
// duration: TeamSpeak parses [..] as a link reference).
msg << std::format(
"{}. {}", i + 1,
markdownLink(displayText, track.shortenedUrl));
if (track.duration.has_value()) {
msg << std::format(" [{}]",
formatDuration(*track.duration));
msg << " " << kDot << " "
<< formatDuration(*track.duration);
}
msg << "\n";
}