#pragma once #include "../MusicManager.hpp" #include "Command.hpp" #include #include #include 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(ctx.tr( "The music service is currently unavailable.")); return; } if (ctx.args.empty()) { ctx.reply(ctx.trf("Usage: {} ", 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( ctx.tr("Couldn't connect to search service. " "Please check your connection.")); } else { ctx.reply(ctx.tr( "Search failed. Please try again later.")); } return; } if (result->tracks.empty()) { ctx.reply(ctx.tr( "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 << ctx.tr("Search results for \"") << ctx.args << ctx.tr("\":\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 << ctx.trf( "{}. {}", i + 1, markdownLink(displayText, trackLinkUrl(track))); if (track.duration.has_value()) { msg << " " << kDot << " " << formatDuration(*track.duration); } msg << ctx.tr("\n"); } msg << ctx.trf("\nUse {}select 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( ctx.tr("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 ""; } std::vector 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