126 lines
4.7 KiB
C++
126 lines
4.7 KiB
C++
#pragma once
|
|
|
|
#include "Command.hpp"
|
|
#include "echoion/core/Logger.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) {
|
|
ECHOION_LOG_ERROR(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;
|
|
}
|
|
|
|
ctx.reply(std::format("Added to queue: {}", *result));
|
|
return;
|
|
}
|
|
|
|
// Otherwise, treat as search query
|
|
auto searchResult = manager->search(ctx.args);
|
|
if (!searchResult) {
|
|
ECHOION_LOG_ERROR(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) {
|
|
ECHOION_LOG_ERROR(std::format("Queue failed: {}", playResult.error().message));
|
|
ctx.reply("Couldn't add that track. Please try again.");
|
|
return;
|
|
}
|
|
|
|
ctx.reply(std::format("Added to queue: {}", track.title));
|
|
return;
|
|
}
|
|
|
|
// Multiple results - store and show list
|
|
manager->storePendingSearch(ctx.clientId, searchResult->tracks);
|
|
|
|
std::string msg = "\xf0\x9f\x94\x8d Multiple results found. Use [prefix]select <number>:\n";
|
|
for (size_t i = 0; i < searchResult->tracks.size() && i < 5; ++i) {
|
|
const auto& track = searchResult->tracks[i];
|
|
msg += std::format("{}. {}", i + 1, track.title);
|
|
if (!track.artist.empty()) {
|
|
msg += std::format(" by {}", track.artist);
|
|
}
|
|
if (track.duration.has_value()) {
|
|
auto mins = std::chrono::duration_cast<std::chrono::minutes>(*track.duration).count();
|
|
auto secs = std::chrono::duration_cast<std::chrono::seconds>(*track.duration).count() % 60;
|
|
msg += std::format(" [{}:{:02}]", mins, secs);
|
|
}
|
|
msg += "\n";
|
|
}
|
|
|
|
ctx.reply(msg);
|
|
|
|
} catch (const std::exception& e) {
|
|
ECHOION_LOG_ERROR(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"};
|
|
}
|
|
};
|
|
|
|
} // namespace Echoion::Core
|