Files
EchoionCore/include/echoion/core/commands/NowPlayingCommand.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

117 lines
3.1 KiB
C++

#pragma once
#include "Command.hpp"
#include <Void/Log/Log.hpp>
#include <format>
namespace Echoion::Core
{
class NowPlayingCommand : 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();
if (!current) {
Void::Log::error("NowPlayingCommand", "{}",
std::format("NowPlaying command failed: {}",
current.error().message));
ctx.reply("Couldn't get current track information.");
return;
}
if (!current->has_value()) {
ctx.reply(std::format("Nothing is currently playing. "
"Add something with {}play !",
ctx.prefix));
return;
}
const auto& track = current->value();
const auto& trackInfo = track.track;
std::string displayText = trackInfo.title;
if (!trackInfo.artist.empty()) {
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);
// Show current position / total duration
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(" [{}/{}]",
formatDuration(position),
formatDuration(*trackInfo.duration));
} else if (trackInfo.duration.has_value() &&
trackInfo.duration->count() > 0) {
msg += std::format(" [{}]",
formatDuration(*trackInfo.duration));
}
if (!track.addedBy.empty()) {
msg += std::format(" (requested by {})", track.addedBy);
}
ctx.reply(msg);
} catch (const std::exception& e) {
Void::Log::error("NowPlayingCommand", "{}", std::format(
"NowPlaying command exception: {}", e.what()));
ctx.reply("Something went wrong. Please try again.");
}
}
std::string_view getDescription() const override
{
return "Shows the currently playing track.";
}
CommandCategory getCategory() const override
{
return CommandCategory::Playback;
}
std::vector<std::string_view> getAliases() const override
{
return {"np", "current", "playing"};
}
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