85 lines
2.2 KiB
C++
85 lines
2.2 KiB
C++
#pragma once
|
|
|
|
#include "../MusicManager.hpp"
|
|
#include "Command.hpp"
|
|
#include <Void/Log/Log.hpp>
|
|
#include <algorithm>
|
|
#include <format>
|
|
#include <string>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
|
|
// !autoplay [on|off] / !radio — toggle endless related-track playback.
|
|
// With no argument it flips the current state. The choice is persisted so
|
|
// it survives restarts. Related tracks come from the same catalog as the
|
|
// last played track (YouTube radio mix or Yandex similar tracks).
|
|
class AutoplayCommand : 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;
|
|
}
|
|
|
|
bool enable = !manager->autoplayEnabled();
|
|
std::string arg(ctx.args);
|
|
std::transform(arg.begin(), arg.end(), arg.begin(),
|
|
[](unsigned char c) {
|
|
return static_cast<char>(
|
|
std::tolower(c));
|
|
});
|
|
if (arg == "on" || arg == "true" || arg == "1" ||
|
|
arg == "enable") {
|
|
enable = true;
|
|
} else if (arg == "off" || arg == "false" || arg == "0" ||
|
|
arg == "disable") {
|
|
enable = false;
|
|
}
|
|
|
|
manager->setAutoplay(enable);
|
|
|
|
if (enable) {
|
|
ctx.reply(ctx.tr(
|
|
"\xf0\x9f\x93\xbb Autoplay is ON \xe2\x80\x94 I'll "
|
|
"keep the music going with related tracks."));
|
|
} else {
|
|
ctx.reply(ctx.tr("\xf0\x9f\x93\xbb Autoplay is OFF."));
|
|
}
|
|
} catch (const std::exception& e) {
|
|
Void::Log::error(
|
|
"AutoplayCommand", "{}",
|
|
std::format("Autoplay command exception: {}",
|
|
e.what()));
|
|
ctx.reply(
|
|
ctx.tr("Something went wrong. Please try again."));
|
|
}
|
|
}
|
|
|
|
std::string_view getDescription() const override
|
|
{
|
|
return "Toggles radio mode: auto-queues related tracks when "
|
|
"the queue runs out.";
|
|
}
|
|
|
|
std::string_view getUsage() const override { return "[on|off]"; }
|
|
|
|
CommandCategory getCategory() const override
|
|
{
|
|
return CommandCategory::Playback;
|
|
}
|
|
|
|
std::vector<std::string_view> getAliases() const override
|
|
{
|
|
return {"radio"};
|
|
}
|
|
};
|
|
|
|
} // namespace Echoion::Core
|