#pragma once #include "Command.hpp" #include "echoion/core/Logger.hpp" #include #include namespace Echoion::Core { class SearchCommand : 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("Usage: search \nExample: search never gonna give you up"); return; } auto result = manager->search(ctx.args); if (!result) { ECHOION_LOG_ERROR(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 << "\xf0\x9f\x94\x8d 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]; 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(*track.duration).count(); auto secs = std::chrono::duration_cast(*track.duration).count() % 60; msg << std::format(" [{}:{:02}]", mins, secs); } msg << "\n"; } msg << "\nUse [prefix]select to play a result."; ctx.reply(msg.str()); } catch (const std::exception& e) { ECHOION_LOG_ERROR(std::format("Search command exception: {}", e.what())); ctx.reply("Something went wrong. Please try again."); } } std::string_view getDescription() const override { return "Searches for tracks and shows results."; } std::string_view getUsage() const override { return ""; } CommandCategory getCategory() const override { return CommandCategory::Music; } std::vector getAliases() const override { return {"find", "s"}; } }; } // namespace Echoion::Core