110 lines
2.9 KiB
C++
110 lines
2.9 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(ctx.tr(
|
|
"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(
|
|
ctx.tr("Couldn't get current track information."));
|
|
return;
|
|
}
|
|
|
|
if (!current->has_value()) {
|
|
ctx.reply(ctx.trf("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 msg = std::format(
|
|
"\xf0\x9f\x8e\xb5 {}",
|
|
markdownLink(displayText, trackLinkUrl(trackInfo)));
|
|
|
|
// 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(" {} {}/{}", kDot,
|
|
formatDuration(position),
|
|
formatDuration(*trackInfo.duration));
|
|
} else if (trackInfo.duration.has_value() &&
|
|
trackInfo.duration->count() > 0) {
|
|
msg += std::format(" {} {}", kDot,
|
|
formatDuration(*trackInfo.duration));
|
|
}
|
|
|
|
if (!track.addedBy.empty()) {
|
|
msg += std::format(" (requested by {})",
|
|
escapeMarkdown(track.addedBy));
|
|
}
|
|
|
|
// Same at-a-glance status footer as !help and !list.
|
|
if (std::string status = playbackStatusLine(ctx);
|
|
!status.empty()) {
|
|
msg += "\n\n";
|
|
msg += status;
|
|
}
|
|
|
|
ctx.reply(msg);
|
|
} catch (const std::exception& e) {
|
|
Void::Log::error(
|
|
"NowPlayingCommand", "{}",
|
|
std::format("NowPlaying command exception: {}",
|
|
e.what()));
|
|
ctx.reply(
|
|
ctx.tr("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"};
|
|
}
|
|
};
|
|
|
|
} // namespace Echoion::Core
|