mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-23 21:52:45 -05:00
refactor(commands): remove unused imports from various command files refactor(src): clean up imports in multiple source files for better organization (#21)
183 lines
6.6 KiB
JavaScript
183 lines
6.6 KiB
JavaScript
/**
|
|
* @module commands/settings/setters
|
|
* @description Value application (applySet) and
|
|
* shortcut dispatch (handleShortcut) for the settings command. Bodies are
|
|
* verbatim from the original single-file command.
|
|
*/
|
|
|
|
|
|
import { cleanId, getMessageGuildId, getGlobalColor } from "../../src/ui/index.mjs";
|
|
import { EmbedBuilder } from "@fluxerjs/core";
|
|
import runnables from "../../settings/runnables.mjs";
|
|
import {
|
|
parseBool, displayValue, embed, tWrap, prettifySettingLabel,
|
|
BOOL_SETTINGS, VALID_LOCALES, VOLUME_MIN, VOLUME_MAX, PREFIX_MAX,
|
|
VOTE_SKIP_THRESHOLD_MIN, VOTE_SKIP_THRESHOLD_MAX,
|
|
} from "./utils.mjs";
|
|
import {
|
|
save247Channels, disable247, handle247Toggle,
|
|
} from "./channels247.mjs";
|
|
|
|
/**
|
|
* Apply a new value to a setting, with type-specific validation.
|
|
* @param {object} ctx - The bot (Remix) context.
|
|
* @param {object} message - The command message.
|
|
* @param {ServerSettings} set - The guild settings instance.
|
|
* @param {string} key - The setting key.
|
|
* @param {string} rawValue - The raw string value from the command.
|
|
* @returns {Promise<string|null>} Error message string, or null on success.
|
|
*/
|
|
export async function applySet(ctx, message, set, key, rawValue) {
|
|
if (BOOL_SETTINGS.has(key)) {
|
|
const bool = parseBool(rawValue);
|
|
if (bool === null) {
|
|
return ctx.t(message, "responses.settings.mustBeBool", { setting: key });
|
|
}
|
|
set.set(key, bool);
|
|
return null;
|
|
}
|
|
|
|
if (key === "volume") {
|
|
const num = parseInt(rawValue, 10);
|
|
if (isNaN(num) || num < VOLUME_MIN || num > VOLUME_MAX) {
|
|
return ctx.t(message, "responses.settings.volumeRange");
|
|
}
|
|
set.set(key, num);
|
|
return null;
|
|
}
|
|
|
|
if (key === "voteSkipThreshold") {
|
|
const num = parseInt(rawValue, 10);
|
|
if (isNaN(num) || num < VOTE_SKIP_THRESHOLD_MIN || num > VOTE_SKIP_THRESHOLD_MAX) {
|
|
return ctx.t(message, "responses.settings.voteSkipThresholdRange", {
|
|
min: VOTE_SKIP_THRESHOLD_MIN,
|
|
max: VOTE_SKIP_THRESHOLD_MAX,
|
|
});
|
|
}
|
|
set.set(key, num);
|
|
return null;
|
|
}
|
|
|
|
if (key === "locale") {
|
|
if (!VALID_LOCALES.has(rawValue)) {
|
|
return ctx.t(message, "responses.settings.invalidLocale", {
|
|
locale: rawValue,
|
|
locales: [...VALID_LOCALES].map(l => "`" + l + "`").join(", ")
|
|
});
|
|
}
|
|
set.set(key, rawValue);
|
|
const gid = getMessageGuildId(message);
|
|
if (gid) ctx.locale.invalidateCache(gid);
|
|
return null;
|
|
}
|
|
|
|
if (key === "prefix") {
|
|
if (!rawValue || rawValue.length > PREFIX_MAX) {
|
|
return ctx.t(message, "responses.settings.prefixLength");
|
|
}
|
|
if (/\s/.test(rawValue)) {
|
|
return ctx.t(message, "responses.settings.prefixSpaces");
|
|
}
|
|
}
|
|
|
|
if (runnables[key]) {
|
|
const err = runnables[key].call(ctx, rawValue, { msg: message });
|
|
if (err) return "❌ " + err;
|
|
}
|
|
|
|
set.set(key, rawValue);
|
|
return null;
|
|
}
|
|
/**
|
|
* @private
|
|
* @async
|
|
* Handle a settings shortcut command (e.g. `prefix`, `247`).
|
|
* Dispatches to 24/7 toggle, value display, or value set depending on arguments.
|
|
* @param {object} ctx - The bot (Remix) context.
|
|
* @param {object} message - The command message.
|
|
* @param {string} settingKey - The resolved setting key.
|
|
* @param {string[]} valueTokens - Remaining argument tokens after the shortcut alias.
|
|
* @returns {Promise<void>}
|
|
*/
|
|
export async function handleShortcut(ctx, message, settingKey, valueTokens) {
|
|
const set = ctx.getSettings(message);
|
|
const guildId = getMessageGuildId(message);
|
|
const t247 = ctx.locale?.translate?.bind(ctx.locale);
|
|
|
|
if (valueTokens.length === 0) {
|
|
if (settingKey === "stay_247") {
|
|
return handle247Toggle(ctx, message, set, guildId);
|
|
}
|
|
const val = set.get(settingKey);
|
|
const label = prettifySettingLabel(settingKey, t247, guildId);
|
|
const defaultVal = ctx.settingsMgr?.defaults?.[settingKey];
|
|
const shortcutEmbed = new EmbedBuilder()
|
|
.setColor(getGlobalColor())
|
|
.setAuthor({ name: ctx.t(message, "responses.settings.shortcutAuthor") })
|
|
.setTitle(`⚙️ \`${settingKey}\` — ${label}`)
|
|
.addFields(
|
|
{
|
|
name: `📌 ${ctx.t(message, "responses.settings.shortcutValueField")}`,
|
|
value: displayValue(settingKey, val),
|
|
inline: true,
|
|
},
|
|
{
|
|
name: `🏷️ ${ctx.t(message, "responses.settings.shortcutDefaultField")}`,
|
|
value: displayValue(settingKey, defaultVal),
|
|
inline: true,
|
|
}
|
|
)
|
|
.setFooter({
|
|
text: ctx.t(message, "responses.settings.shortcutHintFooter", {
|
|
prefix: ctx.handler?.getPrefix(guildId) ?? "%",
|
|
shortcut: settingKey,
|
|
}),
|
|
});
|
|
return message.reply({ embeds: [shortcutEmbed] });
|
|
}
|
|
|
|
const rawValue = valueTokens.join(" ");
|
|
|
|
if (settingKey === "stay_247") {
|
|
const mode = rawValue.toLowerCase().trim();
|
|
if (mode === "off" || mode === "false" || mode === "disable" || mode === "0") {
|
|
const loc = tWrap(ctx, guildId);
|
|
const panelKey = (k, vars = {}) => loc("responses.settings.247Panel." + k, vars);
|
|
if (!guildId) return message.reply(embed(loc("responses.settings.noServer")));
|
|
const { channelId } = await ctx.players.checkVoiceChannels(message);
|
|
if (channelId) {
|
|
await disable247(ctx, set, guildId, channelId);
|
|
return message.reply(embed(
|
|
panelKey("confirmDisabled", { channel: "<#" + cleanId(channelId) + ">" }) + "\n\n" +
|
|
loc("responses.settings.247Disabled", { channel: cleanId(channelId), prefix: ctx.handler.getPrefix(guildId) }),
|
|
{ title: panelKey("title") }
|
|
));
|
|
}
|
|
for (const [chId, player] of [...ctx.players.playerMap.entries()]) {
|
|
if (cleanId(player?._guildId ?? "") === cleanId(guildId)) {
|
|
const activeId = cleanId(player._channelId ?? chId);
|
|
ctx.markIntentionalLeave?.(activeId);
|
|
ctx.players.playerMap.delete(activeId);
|
|
if (activeId !== chId) ctx.players.playerMap.delete(chId);
|
|
await player.leave().catch(() => {});
|
|
player.destroy();
|
|
}
|
|
}
|
|
save247Channels(set, new Set());
|
|
return message.reply(embed(
|
|
panelKey("confirmDisabledAll") + "\n\n" +
|
|
loc("responses.settings.247DisabledAll"),
|
|
{ title: panelKey("title") }
|
|
));
|
|
}
|
|
return handle247Toggle(ctx, message, set, guildId);
|
|
}
|
|
|
|
const err = await applySet(ctx, message, set, settingKey, rawValue);
|
|
if (err) return message.reply(embed(err));
|
|
|
|
const val = set.get(settingKey);
|
|
const label = prettifySettingLabel(settingKey, t247, guildId);
|
|
return message.reply(embed(ctx.t(message, "responses.settings.setSuccess", { label, value: displayValue(settingKey, val) })));
|
|
}
|