Files
EchoionCore/include/echoion/core/commands/RoomCommand.hpp
T
faumaray 01362d2e51 Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-12 02:56:29 +03:00

219 lines
6.7 KiB
C++

#pragma once
#include "Command.hpp"
#include "echoion/core/ListeningRoomManager.hpp"
#include <algorithm>
#include <charconv>
#include <cctype>
#include <format>
#include <sstream>
#include <string>
#include <string_view>
namespace Echoion::Core
{
class RoomCommand final : public Command
{
public:
void execute(const CommandContext& ctx) override
{
auto rooms = ctx.service<ListeningRoomManager>();
if (!rooms) {
ctx.reply(ctx.tr("Listening rooms are currently unavailable."));
return;
}
auto [action, argument] = split(ctx.args);
if (action.empty() || action == "list") {
list(ctx, *rooms);
return;
}
if (action == "help") {
help(ctx);
return;
}
if (action == "create") {
reply(ctx, rooms->create(argument, ctx.clientId),
ctx.trf("Created and joined room '{}'.", argument));
return;
}
if (action == "join") {
reply(ctx, rooms->join(argument, ctx.clientId),
ctx.trf("Joined room '{}'.", argument));
return;
}
if (action == "leave") {
reply(ctx, rooms->leave(ctx.clientId),
ctx.tr("Left the listening room."));
return;
}
const auto current = rooms->roomForClient(ctx.clientId);
if (!current) {
ctx.reply(ctx.trf(
"Join a room first with {}room join <name>.", ctx.prefix));
return;
}
if (action == "delete") {
reply(ctx, rooms->remove(*current, ctx.clientId),
ctx.trf("Deleted room '{}'.", *current));
} else if (action == "play") {
if (argument.empty()) {
help(ctx);
return;
}
auto result = rooms->play(*current, argument, ctx.clientId);
if (!result) {
ctx.reply(ctx.trf("Room action failed: {}",
result.error().message));
} else {
ctx.reply(ctx.trf("Added to '{}': {}", *current, *result));
}
} else if (action == "pause") {
reply(ctx, rooms->pause(*current, ctx.clientId),
ctx.trf("Paused room '{}'.", *current));
} else if (action == "resume") {
reply(ctx, rooms->resume(*current, ctx.clientId),
ctx.trf("Resumed room '{}'.", *current));
} else if (action == "stop") {
reply(ctx, rooms->stop(*current, ctx.clientId),
ctx.trf("Stopped room '{}'.", *current));
} else if (action == "skip") {
reply(ctx, rooms->skip(*current, ctx.clientId),
ctx.trf("Skipped in room '{}'.", *current));
} else if (action == "clear") {
reply(ctx, rooms->clearQueue(*current, ctx.clientId),
ctx.trf("Cleared queue for '{}'.", *current));
} else if (action == "remove") {
const auto queueId = number(argument);
if (!queueId) {
help(ctx);
return;
}
reply(ctx, rooms->removeQueueEntry(
*current, *queueId, ctx.clientId),
ctx.trf("Removed queue entry {}.", *queueId));
} else if (action == "move") {
auto [queueIdText, positionText] = split(argument);
const auto queueId = number(queueIdText);
const auto position = number(positionText);
if (!queueId || !position || *position == 0) {
help(ctx);
return;
}
reply(ctx, rooms->moveQueueEntry(*current, *queueId,
static_cast<uint32_t>(*position - 1), ctx.clientId),
ctx.trf("Moved queue entry {} to position {}.",
*queueId, *position));
} else if (action == "queue") {
queue(ctx, *rooms, *current);
} else {
help(ctx);
}
}
[[nodiscard]] std::string_view getDescription() const override
{
return "Creates, joins and controls independent listening rooms.";
}
[[nodiscard]] std::string_view getUsage() const override
{
return "<list|create|join|leave|delete|play|pause|resume|stop|skip|queue|remove|move|clear>";
}
[[nodiscard]] CommandCategory getCategory() const override
{
return CommandCategory::Music;
}
private:
static std::pair<std::string, std::string> split(std::string_view input)
{
const auto first = input.find_first_not_of(" \t");
if (first == std::string_view::npos) return {};
const auto separator = input.find_first_of(" \t", first);
std::string action(input.substr(first, separator - first));
std::ranges::transform(action, action.begin(),
[](unsigned char value) { return std::tolower(value); });
if (separator == std::string_view::npos) return {action, {}};
const auto argumentStart = input.find_first_not_of(" \t", separator);
return {action, argumentStart == std::string_view::npos
? std::string{} : std::string(input.substr(argumentStart))};
}
static std::optional<uint64_t> number(std::string_view text)
{
uint64_t value = 0;
const auto [end, error] = std::from_chars(
text.data(), text.data() + text.size(), value);
return error == std::errc{} && end == text.data() + text.size()
? std::optional<uint64_t>{value} : std::nullopt;
}
static void list(const CommandContext& ctx, ListeningRoomManager& rooms)
{
const auto all = rooms.list();
if (all.empty()) {
ctx.reply(ctx.trf("No listening rooms. Create one with {}room create <name>.",
ctx.prefix));
return;
}
const auto current = rooms.roomForClient(ctx.clientId);
std::stringstream output;
output << ctx.tr("Listening rooms:") << '\n';
for (const auto& room : all) {
const bool selected = current == room.name;
output << (selected ? "**> " : "- ")
<< escapeMarkdown(room.name) << (selected ? "** " : " ")
<< ctx.trf("({} listeners, {} queued{})", room.members.size(),
room.queueSize, room.isPlaying ? ", playing" : "") << '\n';
}
ctx.reply(output.str());
}
static void queue(const CommandContext& ctx, ListeningRoomManager& rooms,
const std::string& name)
{
auto entries = rooms.queue(name);
if (!entries) {
ctx.reply(ctx.trf("Room action failed: {}", entries.error().message));
return;
}
std::stringstream output;
output << ctx.trf("Queue for '{}' ({} tracks):", name, entries->size());
for (std::size_t index = 0; index < entries->size(); ++index) {
output << '\n' << index + 1 << ". "
<< trackLink((*entries)[index].track) << " `#"
<< (*entries)[index].queueId << '`';
}
ctx.reply(output.str());
}
static void reply(const CommandContext& ctx, const Api::Result<void>& result,
std::string_view success)
{
if (result) {
ctx.reply(success);
} else {
ctx.reply(ctx.trf("Room action failed: {}", result.error().message));
}
}
static void help(const CommandContext& ctx)
{
ctx.reply(ctx.trf(
"Room commands:\n"
"`{}room list`\n`{}room create <name>`\n"
"`{}room join <name>`\n`{}room leave`\n"
"`{}room play <song>`\n`{}room pause|resume|stop|skip`\n"
"`{}room queue`\n`{}room remove <queue-id>`\n"
"`{}room move <queue-id> <position>`\n"
"`{}room clear`\n`{}room delete`",
ctx.prefix, ctx.prefix, ctx.prefix, ctx.prefix, ctx.prefix,
ctx.prefix, ctx.prefix, ctx.prefix, ctx.prefix, ctx.prefix,
ctx.prefix));
}
};
}