Files
EchoionCore/include/echoion/core/commands/VolumeCommand.hpp
T

102 lines
2.4 KiB
C++

#pragma once
#include "Command.hpp"
#include "echoion/core/Logger.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(
"The music service is currently unavailable.");
return;
}
if (ctx.args.empty()) {
// Show current volume
auto state = manager->getPlaybackState();
if (state) {
ctx.reply(std::format(
"Current volume: {}%",
static_cast<int>(state->volume * 100)));
} else {
ctx.reply("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("Please provide a valid number (0-100).");
return;
}
if (volumePercent < 0 || volumePercent > 100) {
ctx.reply("Volume must be between 0 and 100.");
return;
}
float volume = volumePercent / 100.0f;
auto volResult = manager->setVolume(volume);
if (!volResult) {
ECHOION_LOG_ERROR(
std::format("Volume command failed: {}",
volResult.error().message));
ctx.reply("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(std::format(
"Volume: {}% {}", volumePercent, indicator));
} catch (const std::exception& e) {
ECHOION_LOG_ERROR(
std::format("Volume command exception: {}", e.what()));
ctx.reply("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