Files
EchoionCore/include/echoion/core/commands/VolumeCommand.hpp
T
faumaray 5c036d91fb making WEB
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-11 00:45:17 +03:00

107 lines
2.5 KiB
C++

#pragma once
#include "Command.hpp"
#include <Void/Log/Log.hpp>
#include <charconv>
#include <format>
namespace Echoion::Core
{
class VolumeCommand : 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()) {
// Show current volume
auto state = manager->getPlaybackState();
if (state) {
ctx.reply(
ctx.trf("Current volume: {}%",
static_cast<int>(state->volume * 100)));
} else {
ctx.reply(ctx.tr("Unable to get current volume."));
}
return;
}
// Parse volume (0-100)
int volumePercent = 0;
auto result =
std::from_chars(ctx.args.data(),
ctx.args.data() + ctx.args.size(),
volumePercent);
if (result.ec != std::errc()) {
ctx.reply(
ctx.tr("Please provide a valid number (0-100)."));
return;
}
if (volumePercent < 0 || volumePercent > 100) {
ctx.reply(ctx.tr("Volume must be between 0 and 100."));
return;
}
float volume = volumePercent / 100.0f;
auto volResult = manager->setVolume(volume);
if (!volResult) {
Void::Log::error(
"VolumeCommand", "{}",
std::format("Volume command failed: {}",
volResult.error().message));
ctx.reply(ctx.tr(
"Couldn't change volume. Please try again."));
return;
}
// Visual volume indicator
std::string indicator;
int bars = volumePercent / 10;
for (int i = 0; i < 10; ++i) {
indicator +=
(i < bars)
? "\xe2\x96\x88"
: "\xe2\x96\x91"; // Full block / Light shade
}
ctx.reply(
ctx.trf("Volume: {}% {}", volumePercent, indicator));
} catch (const std::exception& e) {
Void::Log::error(
"VolumeCommand", "{}",
std::format("Volume command exception: {}", e.what()));
ctx.reply(
ctx.tr("Something went wrong. Please try again."));
}
}
std::string_view getDescription() const override
{
return "Sets or shows the playback volume.";
}
std::string_view getUsage() const override { return "[0-100]"; }
CommandCategory getCategory() const override
{
return CommandCategory::Playback;
}
std::vector<std::string_view> getAliases() const override
{
return {"vol", "v"};
}
};
} // namespace Echoion::Core