1 parent
df99b6ea7f
commit
d146f11f02
63 files changed
+8331
-2709
No files matched your search
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../MusicManager.hpp"
|
||||
#include "Command.hpp"
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <format>
|
||||
@@ -7,7 +8,12 @@
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
class PlayCommand : public Command
|
||||
// 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:
|
||||
|
||||
@@ -22,18 +28,20 @@ namespace Echoion::Core
|
||||
}
|
||||
|
||||
if (ctx.args.empty()) {
|
||||
ctx.reply("Usage: play <url or search query>\nExample: "
|
||||
"play never gonna give you up");
|
||||
ctx.reply(std::format(
|
||||
"Usage: {} <url or search query>\nExample: {} never "
|
||||
"gonna give you up",
|
||||
label(), label()));
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if it's a direct URL
|
||||
// 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));
|
||||
Void::Log::error("PlayCommand", "{}",
|
||||
std::format("Play URL failed '{}': {}", ctx.args,
|
||||
result.error().message));
|
||||
|
||||
switch (result.error().code) {
|
||||
case Api::ErrorCode::NetworkError:
|
||||
@@ -52,15 +60,14 @@ namespace Echoion::Core
|
||||
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);
|
||||
// Otherwise, search the chosen catalog(s).
|
||||
auto searchResult = manager->search(ctx.args, scope());
|
||||
if (!searchResult) {
|
||||
Void::Log::error("PlayCommand", "{}",
|
||||
Void::Log::error("PlayCommand", "{}",
|
||||
std::format("Search failed for '{}': {}", ctx.args,
|
||||
searchResult.error().message));
|
||||
|
||||
@@ -79,13 +86,13 @@ namespace Echoion::Core
|
||||
return;
|
||||
}
|
||||
|
||||
// Single result - play immediately
|
||||
// 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", "{}",
|
||||
Void::Log::error("PlayCommand", "{}",
|
||||
std::format("Queue failed: {}",
|
||||
playResult.error().message));
|
||||
ctx.reply(
|
||||
@@ -100,11 +107,11 @@ namespace Echoion::Core
|
||||
}
|
||||
ctx.reply(std::format(
|
||||
"Added to queue: {}",
|
||||
markdownLink(displayText, track.shortenedUrl)));
|
||||
markdownLink(displayText, trackLinkUrl(track))));
|
||||
return;
|
||||
}
|
||||
|
||||
// Multiple results - store and show list
|
||||
// Multiple results - store and show list.
|
||||
manager->storePendingSearch(ctx.clientId,
|
||||
searchResult->tracks);
|
||||
|
||||
@@ -119,11 +126,9 @@ namespace Echoion::Core
|
||||
displayText = std::format("{} by {}", track.title,
|
||||
track.artist);
|
||||
}
|
||||
// [escaped title](url) · duration (no [] around the
|
||||
// duration: TeamSpeak parses [..] as a link reference).
|
||||
msg += std::format(
|
||||
"{}. {}", i + 1,
|
||||
markdownLink(displayText, track.shortenedUrl));
|
||||
markdownLink(displayText, trackLinkUrl(track)));
|
||||
if (track.duration.has_value()) {
|
||||
msg += std::format(" {} {}", kDot,
|
||||
formatDuration(*track.duration));
|
||||
@@ -134,33 +139,24 @@ namespace Echoion::Core
|
||||
ctx.reply(msg);
|
||||
|
||||
} catch (const std::exception& e) {
|
||||
Void::Log::error("PlayCommand", "{}",
|
||||
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;
|
||||
return CommandCategory::Play;
|
||||
}
|
||||
|
||||
std::vector<std::string_view> getAliases() const override
|
||||
{
|
||||
return {"p", "add"};
|
||||
}
|
||||
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:
|
||||
|
||||
@@ -179,4 +175,32 @@ namespace Echoion::Core
|
||||
}
|
||||
};
|
||||
|
||||
// !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
|
||||
Reference in new issue
Block a user