219 lines
5.9 KiB
C++
219 lines
5.9 KiB
C++
#pragma once
|
|
|
|
#include "../MusicManager.hpp"
|
|
#include "Command.hpp"
|
|
#include <Void/Log/Log.hpp>
|
|
#include <format>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
|
|
// Shared play rendering. Concrete commands pick the catalog scope:
|
|
// !play (both), !pyt (YouTube), !pya (Yandex). A bare URL always plays
|
|
// directly regardless of scope; free text searches the chosen catalog(s)
|
|
// and, when there is more than one hit, shows a !select list — the same
|
|
// flow as the search commands.
|
|
class PlayCommandBase : 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. "
|
|
"Please try again later."));
|
|
return;
|
|
}
|
|
|
|
if (ctx.args.empty()) {
|
|
ctx.reply(ctx.trf("Usage: {} <url or search "
|
|
"query>\nExample: {} never "
|
|
"gonna give you up",
|
|
label(), label()));
|
|
return;
|
|
}
|
|
|
|
// Direct URL: hand straight to the player.
|
|
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(
|
|
ctx.tr("Couldn't connect to that URL. "
|
|
"Please check and try again."));
|
|
break;
|
|
case Api::ErrorCode::InvalidUrl:
|
|
ctx.reply(ctx.tr(
|
|
"That doesn't look like a valid URL."));
|
|
break;
|
|
default:
|
|
ctx.reply(
|
|
ctx.tr("Couldn't play that URL. Please "
|
|
"try again."));
|
|
break;
|
|
}
|
|
return;
|
|
}
|
|
|
|
ctx.reply(ctx.trf("Added to queue: {}", *result));
|
|
return;
|
|
}
|
|
|
|
// Otherwise, search the chosen catalog(s).
|
|
auto searchResult = manager->search(ctx.args, scope());
|
|
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(
|
|
ctx.tr("Couldn't connect to search service. "
|
|
"Please check your connection."));
|
|
} else {
|
|
ctx.reply(
|
|
ctx.tr("Search failed. Please try again."));
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (searchResult->tracks.empty()) {
|
|
ctx.reply(ctx.tr(
|
|
"No results found. Try different keywords."));
|
|
return;
|
|
}
|
|
|
|
// Single result - queue 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(ctx.tr(
|
|
"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);
|
|
}
|
|
ctx.reply(ctx.trf(
|
|
"Added to queue: {}",
|
|
markdownLink(displayText, trackLinkUrl(track))));
|
|
return;
|
|
}
|
|
|
|
// Multiple results - store and show list.
|
|
manager->storePendingSearch(ctx.clientId,
|
|
searchResult->tracks);
|
|
|
|
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];
|
|
std::string displayText = track.title;
|
|
if (!track.artist.empty()) {
|
|
displayText = std::format("{} by {}", track.title,
|
|
track.artist);
|
|
}
|
|
msg += std::format(
|
|
"{}. {}", i + 1,
|
|
markdownLink(displayText, trackLinkUrl(track)));
|
|
if (track.duration.has_value()) {
|
|
msg += std::format(" {} {}", kDot,
|
|
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(
|
|
ctx.tr("Something went wrong while trying to play. "
|
|
"Please try again."));
|
|
}
|
|
}
|
|
|
|
CommandCategory getCategory() const override
|
|
{
|
|
return CommandCategory::Play;
|
|
}
|
|
|
|
protected:
|
|
|
|
// The catalog(s) this command searches, and the command name used
|
|
// in usage text.
|
|
[[nodiscard]] virtual MusicManager::SearchScope scope() const = 0;
|
|
[[nodiscard]] virtual std::string_view label() const = 0;
|
|
|
|
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);
|
|
}
|
|
};
|
|
|
|
// !play — both YouTube and Yandex Music.
|
|
class PlayCommand : public PlayCommandBase
|
|
{
|
|
public:
|
|
|
|
std::string_view getDescription() const override
|
|
{
|
|
return "Plays a track from URL, or searches YouTube and Yandex "
|
|
"Music and lets you select.";
|
|
}
|
|
std::string_view getUsage() const override
|
|
{
|
|
return "<url or search query>";
|
|
}
|
|
std::vector<std::string_view> getAliases() const override
|
|
{
|
|
return {"p", "add"};
|
|
}
|
|
|
|
protected:
|
|
|
|
MusicManager::SearchScope scope() const override
|
|
{
|
|
return MusicManager::SearchScope::All;
|
|
}
|
|
std::string_view label() const override { return "play"; }
|
|
};
|
|
|
|
} // namespace Echoion::Core
|