Files
EchoionCore/include/echoion/core/commands/SearchCommand.hpp
T
2026-02-01 23:04:49 +03:00

91 lines
3.0 KiB
C++

#pragma once
#include "Command.hpp"
#include "echoion/core/Logger.hpp"
#include <format>
#include <sstream>
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 <query>\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<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";
}
msg << "\nUse [prefix]select <number> 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 "<query>";
}
CommandCategory getCategory() const override {
return CommandCategory::Music;
}
std::vector<std::string_view> getAliases() const override {
return {"find", "s"};
}
};
} // namespace Echoion::Core