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 <Types.hpp>
#include <Void/Service/Registry.hpp> #include <Void/Service/Registry.hpp>
#include <cstdint> #include <cstdint>
#include <format>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <optional>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <vector> #include <vector>
@@ -35,6 +37,47 @@ namespace Echoion::Core
return "Utility"; 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. * @brief Context passed to every command execution.
* Wraps all dependencies required to perform an action. * Wraps all dependencies required to perform an action.
@@ -44,33 +44,30 @@ namespace Echoion::Core
displayText = std::format("{} by {}", trackInfo.title, displayText = std::format("{} by {}", trackInfo.title,
trackInfo.artist); trackInfo.artist);
} }
std::string linkText = std::string msg = std::format(
trackInfo.shortenedUrl.has_value() && "\xf0\x9f\x8e\xb5 {}",
!trackInfo.shortenedUrl->empty() markdownLink(displayText, trackInfo.shortenedUrl));
? std::format("[{}]({})", displayText,
*trackInfo.shortenedUrl)
: displayText;
std::string msg =
std::format("\xf0\x9f\x8e\xb5 {}", linkText);
// 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(); auto playbackState = manager->getPlaybackState();
if (playbackState && if (playbackState &&
playbackState->currentTrack.has_value() && playbackState->currentTrack.has_value() &&
trackInfo.duration.has_value() && trackInfo.duration.has_value() &&
trackInfo.duration->count() > 0) { trackInfo.duration->count() > 0) {
auto position = playbackState->position; auto position = playbackState->position;
msg += std::format(" [{}/{}]", msg += std::format(" {} {}/{}", kDot,
formatDuration(position), formatDuration(position),
formatDuration(*trackInfo.duration)); formatDuration(*trackInfo.duration));
} else if (trackInfo.duration.has_value() && } else if (trackInfo.duration.has_value() &&
trackInfo.duration->count() > 0) { trackInfo.duration->count() > 0) {
msg += std::format(" [{}]", msg += std::format(" {} {}", kDot,
formatDuration(*trackInfo.duration)); formatDuration(*trackInfo.duration));
} }
if (!track.addedBy.empty()) { if (!track.addedBy.empty()) {
msg += std::format(" (requested by {})", track.addedBy); msg += std::format(" (requested by {})",
escapeMarkdown(track.addedBy));
} }
ctx.reply(msg); ctx.reply(msg);
+12 -19
View File
@@ -98,13 +98,9 @@ namespace Echoion::Core
displayText = std::format("{} by {}", track.title, displayText = std::format("{} by {}", track.title,
track.artist); track.artist);
} }
std::string linkText = ctx.reply(std::format(
track.shortenedUrl.has_value() && "Added to queue: {}",
!track.shortenedUrl->empty() markdownLink(displayText, track.shortenedUrl)));
? std::format("[{}]({})", displayText,
*track.shortenedUrl)
: displayText;
ctx.reply(std::format("Added to queue: {}", linkText));
return; return;
} }
@@ -112,10 +108,9 @@ namespace Echoion::Core
manager->storePendingSearch(ctx.clientId, manager->storePendingSearch(ctx.clientId,
searchResult->tracks); searchResult->tracks);
std::string msg = std::string msg = std::format(
std::format("\xf0\x9f\x94\x8d Multiple results " "Multiple results \xe2\x80\x94 use {}select <number>:\n\n",
"found. Use {}select <number>:\n", ctx.prefix);
ctx.prefix);
for (size_t i = 0; i < searchResult->tracks.size() && i < 5; for (size_t i = 0; i < searchResult->tracks.size() && i < 5;
++i) { ++i) {
const auto& track = searchResult->tracks[i]; const auto& track = searchResult->tracks[i];
@@ -124,15 +119,13 @@ namespace Echoion::Core
displayText = std::format("{} by {}", track.title, displayText = std::format("{} by {}", track.title,
track.artist); track.artist);
} }
std::string linkText = // [escaped title](url) · duration (no [] around the
track.shortenedUrl.has_value() && // duration: TeamSpeak parses [..] as a link reference).
!track.shortenedUrl->empty() msg += std::format(
? std::format("[{}]({})", displayText, "{}. {}", i + 1,
*track.shortenedUrl) markdownLink(displayText, track.shortenedUrl));
: displayText;
msg += std::format("{}. {}", i + 1, linkText);
if (track.duration.has_value()) { if (track.duration.has_value()) {
msg += std::format(" [{}]", msg += std::format(" {} {}", kDot,
formatDuration(*track.duration)); formatDuration(*track.duration));
} }
msg += "\n"; msg += "\n";
+19 -20
View File
@@ -72,18 +72,19 @@ namespace Echoion::Core
const auto& track = current->value(); const auto& track = current->value();
const auto& trackInfo = track.track; 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; std::string timeStr;
if (trackInfo.duration.has_value() && if (trackInfo.duration.has_value() &&
trackInfo.duration->count() > 0) { trackInfo.duration->count() > 0) {
if (state) { if (state) {
timeStr = std::format( timeStr = std::format(
" [{}/{}]", " {} {}/{}", kDot,
formatDuration(state->position), formatDuration(state->position),
formatDuration(*trackInfo.duration)); formatDuration(*trackInfo.duration));
} else { } else {
timeStr = std::format( timeStr = std::format(
" [{}]", " {} {}", kDot,
formatDuration(*trackInfo.duration)); formatDuration(*trackInfo.duration));
} }
} }
@@ -93,13 +94,10 @@ namespace Echoion::Core
displayText = std::format( displayText = std::format(
"{} by {}", trackInfo.title, trackInfo.artist); "{} by {}", trackInfo.title, trackInfo.artist);
} }
std::string linkText = msg << std::format(
trackInfo.shortenedUrl.has_value() && "\n**Now Playing:** {}{}\n",
!trackInfo.shortenedUrl->empty() markdownLink(displayText, trackInfo.shortenedUrl),
? std::format("[{}]({}){}", displayText, timeStr);
*trackInfo.shortenedUrl, timeStr)
: displayText + timeStr;
msg << std::format("\n**Now Playing:** {}\n", linkText);
} }
// Queue // Queue
@@ -119,17 +117,18 @@ namespace Echoion::Core
std::format("{} by {}", trackInfo.title, std::format("{} by {}", trackInfo.title,
trackInfo.artist); trackInfo.artist);
} }
std::string linkText = msg << std::format(
trackInfo.shortenedUrl.has_value() && "{}. {}", i + 1,
!trackInfo.shortenedUrl->empty() markdownLink(displayText,
? std::format( trackInfo.shortenedUrl));
"[{}]({}) [{}]", displayText, if (trackInfo.duration.has_value()) {
*trackInfo.shortenedUrl, msg << std::format(
formatDuration(*trackInfo.duration)) " {} {}", kDot,
: displayText; formatDuration(*trackInfo.duration));
msg << std::format("{}. {}", i + 1, linkText); }
if (!entry.addedBy.empty()) { if (!entry.addedBy.empty()) {
msg << std::format(" (by {})", entry.addedBy); msg << std::format(
" (by {})", escapeMarkdown(entry.addedBy));
} }
msg << "\n"; msg << "\n";
} }
@@ -53,29 +53,24 @@ namespace Echoion::Core
manager->storePendingSearch(ctx.clientId, result->tracks); manager->storePendingSearch(ctx.clientId, result->tracks);
std::stringstream msg; std::stringstream msg;
msg << "\xf0\x9f\x94\x8d Search results for \"" << ctx.args msg << "Search results for \"" << ctx.args << "\":\n\n";
<< "\":\n\n";
for (size_t i = 0; i < result->tracks.size() && i < 5; for (size_t i = 0; i < result->tracks.size() && i < 5;
++i) { ++i) {
const auto& track = result->tracks[i]; const auto& track = result->tracks[i];
// Format as [Title by Artist](shortenedUrl) with
// duration
std::string displayText = track.title; std::string displayText = track.title;
if (!track.artist.empty()) { if (!track.artist.empty()) {
displayText = std::format("{} by {}", track.title, displayText = std::format("{} by {}", track.title,
track.artist); track.artist);
} }
std::string linkText = // [escaped title](url) · duration (no [] around the
track.shortenedUrl.has_value() && // duration: TeamSpeak parses [..] as a link reference).
!track.shortenedUrl->empty() msg << std::format(
? std::format("[{}]({})", displayText, "{}. {}", i + 1,
*track.shortenedUrl) markdownLink(displayText, track.shortenedUrl));
: displayText;
msg << std::format("{}. {}", i + 1, linkText);
if (track.duration.has_value()) { if (track.duration.has_value()) {
msg << std::format(" [{}]", msg << " " << kDot << " "
formatDuration(*track.duration)); << formatDuration(*track.duration);
} }
msg << "\n"; msg << "\n";
} }