Files
EchoionCore/include/echoion/core/commands/PlayCommand.hpp
T
faumarayandClaude Opus 4.8 2d7651c308 Route all logging through the Void::Log facade
Delete the echoion Logger adapter (Logger.{hpp,cpp}) and the
ILogger/LoggerRegistry indirection. Core::installLogging() builds the
console + split-file Void::Logging::MultiLogger and installs it as the
process-wide Void::Log sink via Void::Log::make_sink (logSink_ owns it for
the process lifetime). The ~450 ECHOION_LOG_* call sites across EchoionCore
are rewritten to Void::Log::<level>("<Subsystem>", "{}", <expr>) with
per-file module tags (warning->warn, critical->fatal); the pre-formatted
message is passed as the single "{}" argument. Build green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-23 14:10:06 +03:00

190 lines
5.1 KiB
C++

#pragma once
#include "Command.hpp"
#include <Void/Log/Log.hpp>
#include <format>
namespace Echoion::Core
{
class PlayCommand : 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. "
"Please try again later.");
return;
}
if (ctx.args.empty()) {
ctx.reply("Usage: play <url or search query>\nExample: "
"play never gonna give you up");
return;
}
// Check if it's a direct URL
if (ctx.args.find("http") == 0) {
auto result = manager->play(ctx.args, ctx.clientId);
if (!result) {
Void::Log::error("PlayCommand", "{}",
std::format("Play URL failed '{}': {}",
ctx.args, result.error().message));
switch (result.error().code) {
case Api::ErrorCode::NetworkError:
ctx.reply("Couldn't connect to that URL. "
"Please check and try again.");
break;
case Api::ErrorCode::InvalidUrl:
ctx.reply(
"That doesn't look like a valid URL.");
break;
default:
ctx.reply("Couldn't play that URL. Please "
"try again.");
break;
}
return;
}
// result contains the track title
ctx.reply(std::format("Added to queue: {}", *result));
return;
}
// Otherwise, treat as search query
auto searchResult = manager->search(ctx.args);
if (!searchResult) {
Void::Log::error("PlayCommand", "{}",
std::format("Search failed for '{}': {}", ctx.args,
searchResult.error().message));
if (searchResult.error().code ==
Api::ErrorCode::NetworkError) {
ctx.reply("Couldn't connect to search service. "
"Please check your connection.");
} else {
ctx.reply("Search failed. Please try again.");
}
return;
}
if (searchResult->tracks.empty()) {
ctx.reply("No results found. Try different keywords.");
return;
}
// Single result - play immediately
if (searchResult->tracks.size() == 1) {
const auto& track = searchResult->tracks[0];
auto playResult =
manager->queueTrack(track, ctx.clientId);
if (!playResult) {
Void::Log::error("PlayCommand", "{}",
std::format("Queue failed: {}",
playResult.error().message));
ctx.reply(
"Couldn't add that track. Please try again.");
return;
}
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;
ctx.reply(std::format("Added to queue: {}", linkText));
return;
}
// Multiple results - store and show list
manager->storePendingSearch(ctx.clientId,
searchResult->tracks);
std::string msg =
std::format("\xf0\x9f\x94\x8d Multiple results "
"found. Use {}select <number>:\n",
ctx.prefix);
for (size_t i = 0; i < searchResult->tracks.size() && i < 5;
++i) {
const auto& track = searchResult->tracks[i];
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);
if (track.duration.has_value()) {
msg += std::format(" [{}]",
formatDuration(*track.duration));
}
msg += "\n";
}
ctx.reply(msg);
} catch (const std::exception& e) {
Void::Log::error("PlayCommand", "{}",
std::format("Play command exception: {}", e.what()));
ctx.reply("Something went wrong while trying to play. "
"Please try again.");
}
}
std::string_view getDescription() const override
{
return "Plays a track from URL or searches and lets you "
"select.";
}
std::string_view getUsage() const override
{
return "<url or search query>";
}
CommandCategory getCategory() const override
{
return CommandCategory::Music;
}
std::vector<std::string_view> getAliases() const override
{
return {"p", "add"};
}
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