Files
EchoionCore/include/echoion/core/commands/SearchCommand.hpp
T
faumaray d146f11f02 Release 1.0.0
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-04 01:49:28 +03:00

147 lines
3.8 KiB
C++

#pragma once
#include "../MusicManager.hpp"
#include "Command.hpp"
#include <Void/Log/Log.hpp>
#include <format>
#include <sstream>
namespace Echoion::Core
{
// Shared search rendering. Concrete commands pick the catalog scope:
// !search (both), !youtube/!yb (YouTube), !ya/!yandex (Yandex).
class SearchCommandBase : 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;
}
if (ctx.args.empty()) {
ctx.reply(std::format("Usage: {} <query>", label()));
return;
}
auto result = manager->search(ctx.args, scope());
if (!result) {
Void::Log::error("SearchCommand", "{}",
std::format("Search failed for '{}': {}", ctx.args,
result.error().message));
if (result.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 later.");
}
return;
}
if (result->tracks.empty()) {
ctx.reply("No results found. Try different keywords.");
return;
}
// Store results for this client so they can use select
manager->storePendingSearch(ctx.clientId, result->tracks);
std::stringstream msg;
msg << "Search results for \"" << ctx.args << "\":\n\n";
for (size_t i = 0; i < result->tracks.size() && i < 5;
++i) {
const auto& track = result->tracks[i];
std::string displayText = track.title;
if (!track.artist.empty()) {
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, trackLinkUrl(track)));
if (track.duration.has_value()) {
msg << " " << kDot << " "
<< formatDuration(*track.duration);
}
msg << "\n";
}
msg << std::format(
"\nUse {}select <number> to play a result.",
ctx.prefix);
ctx.reply(msg.str());
} catch (const std::exception& e) {
Void::Log::error("SearchCommand", "{}",
std::format("Search command exception: {}", e.what()));
ctx.reply("Something went wrong. Please try again.");
}
}
CommandCategory getCategory() const override
{
return CommandCategory::Search;
}
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);
}
};
// !search — both YouTube and Yandex Music.
class SearchCommand : public SearchCommandBase
{
public:
std::string_view getDescription() const override
{
return "Search YouTube and Yandex Music for tracks.";
}
std::string_view getUsage() const override { return "<query>"; }
std::vector<std::string_view> getAliases() const override
{
return {"find", "s"};
}
protected:
MusicManager::SearchScope scope() const override
{
return MusicManager::SearchScope::All;
}
std::string_view label() const override { return "search"; }
};
} // namespace Echoion::Core