refactor: centralize prefix resolution into CommandHandler

Replace all scattered prefix lookups across the codebase with a single
source of truth: CommandHandler.getPrefix(guildId).
This commit is contained in:
NoLogicAlan committed 2026-05-31 15:02:18 +03:00
1 parent 23d0dbc522
commit ba479e4c04
25 files changed
+155 -159

No files matched your search

+11 -20
View File
@@ -59,23 +59,19 @@ export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) {
const guildId = getGuildId(message);
const is247 = (() => {
try {
const raw = this.settingsMgr?.getServer?.(guildId)?.get?.("stay_247");
return raw && raw !== "none";
} catch (_) { return false; }
const raw = this.settingsMgr?.getServer?.(guildId)?.get?.("stay_247");
return raw && raw !== "none";
})();
const mode247 = (() => {
if (!is247) return "off";
try {
const set = this.settingsMgr?.getServer?.(guildId);
const modes = set?.get?.("stay_247_modes");
const matchCh = homeChannelId || activeChannelId;
if (modes && typeof modes === "object" && !Array.isArray(modes) && modes[matchCh]) {
return modes[matchCh];
}
return set?.get?.("stay_247_mode") ?? "off";
} catch (_) { return "off"; }
const set = this.settingsMgr?.getServer?.(guildId);
const modes = set?.get?.("stay_247_modes");
const matchCh = homeChannelId || activeChannelId;
if (modes && typeof modes === "object" && !Array.isArray(modes) && modes[matchCh]) {
return modes[matchCh];
}
return set?.get?.("stay_247_mode") ?? "off";
})();
this.players.playerMap.delete(activeChannelId);
@@ -85,9 +81,7 @@ export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) {
if (is247 && (mode247 === "on" || mode247 === "auto")) {
const rejoinDelay = this.config?.timers?.rejoin247Delay ?? 3_000;
const prefix = (() => {
try { return this._commands?.getPrefix?.(guildId) ?? "%"; } catch (_) { return "%"; }
})();
const prefix = this._commands.getPrefix(guildId);
const embed = new EmbedBuilder().setColor(getGlobalColor())
.setDescription(this.t(message, "responses.join.autoLeaveInactive247", { channel: activeChannelId, prefix }));
message.channel.send({ embeds: [embed] }).catch(() => {});
@@ -101,9 +95,6 @@ export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) {
}
}, rejoinDelay);
} else {
const prefix = (() => {
try { return this._commands?.getPrefix?.(guildId) ?? "%"; } catch (_) { return "%"; }
})();
const embed2 = new EmbedBuilder().setColor(getGlobalColor())
.setDescription(this.t(message, "responses.join.autoLeaveInactive", { channel: activeChannelId }));
message.channel.send({ embeds: [embed2] }).catch(() => {});
@@ -190,7 +181,7 @@ export async function run(message, data) {
const cid = this.players.checkVoiceChannels(message);
if (!cid) {
const prefix = this._commands?.getPrefix?.(getGuildId(message)) ?? "%";
const prefix = this._commands.getPrefix(getGuildId(message));
const embed = new EmbedBuilder().setColor(getGlobalColor())
.setDescription(this.t(message, "responses.join.noVoiceChannel", { prefix }));
return message.reply({ embeds: [embed] });
+3 -3
View File
@@ -139,7 +139,7 @@ function extractPeriod(data, msg) {
/**
* Resolve a Last.fm category (loved/top/recent/playlist) into playable tracks.
* Shared between `%lastfm play <cat>` and `%play lastfm:<cat>`.
* Shared between `$prefixlastfm play <cat>` and `$prefixplay lastfm:<cat>`.
*
* @param {object} ctx - The command `this` context (has .lastfm, .getPlayer, .handler, .t)
* @param {object} msg - The message object
@@ -153,7 +153,7 @@ function extractPeriod(data, msg) {
*/
export async function playLastFmCategory(ctx, msg, userId, category, options = {}) {
const lastfm = ctx.lastfm;
const prefix = ctx.handler?.getPrefix?.(msg.message?.guildId) ?? "%";
const prefix = ctx.handler.getPrefix(msg.message?.guildId);
const resolveProvider = options.resolveProvider || "yt";
if (!lastfm || !lastfm.enabled) return msg.reply(notConfigured(ctx, msg));
@@ -332,7 +332,7 @@ export async function run(msg, data) {
const lastfm = this.lastfm;
if (!lastfm || !lastfm.enabled) return msg.reply(notConfigured(this, msg));
const prefix = this.handler?.getPrefix?.(msg.message?.guildId) ?? "%";
const prefix = this.handler.getPrefix(msg.message?.guildId);
const action = data.get("action")?.value ?? "profile";
const userId = msg.message?.author?.id ?? msg.author?.id;
const targetUserId = data.get("user")?.value ?? null;
+3 -3
View File
@@ -76,7 +76,7 @@ export async function run(msg, data) {
return msg.reply(embed(
this.t(msg, "responses.leave.specifyChannel", {
channels: channelList.map(c => `• ${c}`).join("\n"),
prefix: this.getSettings(msg)?.get("prefix") ?? "%"
prefix: this._commands.getPrefix(msg.message?.guildId ?? msg.channel?.guild?.id)
})
));
}
@@ -135,7 +135,7 @@ export async function run(msg, data) {
player.destroy();
if (mode === "auto") {
msg.reply(embed(this.t(msg, "responses.leave.leftRejoin247", { channel: targetChannelId, prefix: set?.get("prefix") ?? "%" })));
msg.reply(embed(this.t(msg, "responses.leave.leftRejoin247", { channel: targetChannelId, prefix: this._commands.getPrefix(msg.message?.guildId ?? msg.channel?.guild?.id) })));
const leave247Delay = this.config?.timers?.leave247RejoinDelay ?? 5000;
setTimeout(() => {
if (this._spawnPlayer) {
@@ -171,7 +171,7 @@ export async function run(msg, data) {
}
}
} else {
msg.reply(embed(this.t(msg, "responses.leave.left247On", { prefix: set?.get("prefix") ?? "%" })));
msg.reply(embed(this.t(msg, "responses.leave.left247On", { prefix: this._commands.getPrefix(msg.message?.guildId ?? msg.channel?.guild?.id) })));
if (other247AutoChannels.length > 0) {
const rejoinDelay = this.config?.timers?.leave247RejoinDelay ?? 5000;
+1 -1
View File
@@ -24,7 +24,7 @@ export async function run(message, data) {
const totalTracks = p.queue.size();
if (!current && totalTracks === 0) {
const embed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(this.t(message, "responses.list.empty", { prefix: "%" }));
const embed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(this.t(message, "responses.list.empty", { prefix: this._commands.getPrefix(message.message?.guildId ?? message.channel?.guildId) }));
return message.reply({ embeds: [embed] });
}
+1 -1
View File
@@ -16,7 +16,7 @@ function parseInlineProvider(raw) {
export const command = new CommandBuilder()
.setName("playnext")
.setId("playnext")
.setDescription("Same as %play but adds the result to the top of the queue.", "commands.playnext")
.setDescription("Same as $prefixplay but adds the result to the top of the queue.", "commands.playnext")
.setCategory("music")
.addExamples(
"$prefixplaynext blinding lights",
+1 -1
View File
@@ -137,7 +137,7 @@ async function playStation(ctx, msg, radio, editTarget = null) {
if (current?.type === "radio" || hasRadioQueued) await p.switchRadio(radio);
else p.playRadio(radio);
const prefix = (() => { try { return ctx.handler?.getPrefix?.(msg?.channel?.channel?.guildId ?? msg?.message?.guildId) ?? "%"; } catch (_) { return "%"; } })();
const prefix = ctx.handler.getPrefix(msg?.channel?.channel?.guildId ?? msg?.message?.guildId);
const embed = new EmbedBuilder()
.setColor(getGlobalColor())
.setTitle(`📻 ${radio.detailedName}`)
+24 -32
View File
@@ -207,9 +207,7 @@ function resolveChannelName(client, channelId) {
*/
function build247StatusPanel(set, ctx, message, t, guildId) {
const channels = [...get247Channels(set)];
const prefix = (() => {
try { return set.get("prefix") ?? "%"; } catch (_) { return "%"; }
})();
const prefix = ctx.handler.getPrefix(guildId);
const locale = (key, data = {}) => t ? t(guildId, key, { ...data, prefix }) : key;
if (channels.length === 0) {
@@ -255,7 +253,7 @@ function build247StatusPanel(set, ctx, message, t, guildId) {
}
/**
* Build a simple embed for the %247 on/auto confirmation.
* Build a simple embed for the $prefix247 on/auto confirmation.
* @param {object} set - ServerSettings
* @param {string} channelId - Clean channel ID
* @param {string} mode - "on" | "auto"
@@ -267,9 +265,7 @@ function build247StatusPanel(set, ctx, message, t, guildId) {
function build247EnabledPanel(set, channelId, mode, joined, ctx, guildId, t) {
const channels = [...get247Channels(set)];
const modeStr = modeLabel(mode, t, guildId);
const prefix = (() => {
try { return set.get("prefix") ?? "%"; } catch (_) { return "%"; }
})();
const prefix = ctx.handler.getPrefix(guildId);
const locale = (key, data = {}) => t ? t(guildId, key, { ...data, prefix }) : key;
const chName = resolveChannelName(ctx.client, channelId);
const label = chName ? `**${chName}** <#${channelId}>` : `<#${channelId}>`;
@@ -302,17 +298,15 @@ function build247EnabledPanel(set, channelId, mode, joined, ctx, guildId, t) {
}
/**
* Build a simple embed for the %247 off confirmation.
* Build a simple embed for the $prefix247 off confirmation.
* @param {object} set - ServerSettings
* @param {string} channelId - Clean channel ID
* @param {string} guildId - Guild ID
* @param {Function} [t] - Locale translate function
*/
function build247DisabledPanel(set, channelId, guildId, t) {
function build247DisabledPanel(set, channelId, guildId, t, ctx) {
const channels = [...get247Channels(set)];
const prefix = (() => {
try { return set.get("prefix") ?? "%"; } catch (_) { return "%"; }
})();
const prefix = ctx.handler.getPrefix(guildId);
const locale = (key, data = {}) => t ? t(guildId, key, { ...data, prefix }) : key;
if (channels.length === 0) {
@@ -362,11 +356,9 @@ function format247Status(set, t, guildId) {
return `${channels.size} channels: ${parts.join(", ")}`;
}
function format247Summary(set, t, guildId) {
function format247Summary(set, t, guildId, ctx) {
const channels = [...get247Channels(set)];
const prefix = (() => {
try { return set.get("prefix") ?? "%"; } catch (_) { return "%"; }
})();
const prefix = ctx.handler.getPrefix(guildId);
const locale = (key, data = {}) => t ? t(guildId, key, { ...data, prefix }) : key;
if (channels.length === 0) {
@@ -421,7 +413,7 @@ async function handle247(ctx, message, value) {
if (!["off", "on", "auto"].includes(resolved)) {
return message.reply(embed(
ctx.t(message, "responses.settings.invalid247", { value, prefix: set.get("prefix") ?? "%" })
ctx.t(message, "responses.settings.invalid247", { value, prefix: ctx.handler.getPrefix(guildId) })
));
}
@@ -450,7 +442,7 @@ async function handle247(ctx, message, value) {
player.destroy();
}
const t247 = ctx.locale?.translate?.bind(ctx.locale);
return message.reply(build247DisabledPanel(set, id, guildId, t247));
return message.reply(build247DisabledPanel(set, id, guildId, t247, ctx));
}
save247Channels(set, new Set());
@@ -466,9 +458,7 @@ async function handle247(ctx, message, value) {
player.destroy();
}
}
const prefix = (() => {
try { return set.get("prefix") ?? "%"; } catch (_) { return "%"; }
})();
const prefix = ctx.handler.getPrefix(guildId);
const t247 = ctx.locale?.translate?.bind(ctx.locale);
const loc = (key, data = {}) => t247 ? t247(guildId, key, { ...data, prefix }) : key;
return message.reply(richEmbed([{
@@ -487,7 +477,7 @@ async function handle247(ctx, message, value) {
const userChannelId = ctx.players.checkVoiceChannels(message);
if (!userChannelId) {
return message.reply(embed(
ctx.t(message, "responses.settings.noVoice247", { mode: resolved, prefix: set.get("prefix") ?? "%" })
ctx.t(message, "responses.settings.noVoice247", { mode: resolved, prefix: ctx.handler.getPrefix(guildId) })
));
}
@@ -495,7 +485,7 @@ async function handle247(ctx, message, value) {
const channels = get247Channels(set);
if (!channels.has(id) && channels.size >= MAX_247_CHANNELS) {
return message.reply(embed(
ctx.t(message, "responses.settings.max247Channels", { max: MAX_247_CHANNELS, prefix: set.get("prefix") ?? "%" })
ctx.t(message, "responses.settings.max247Channels", { max: MAX_247_CHANNELS, prefix: ctx.handler.getPrefix(guildId) })
));
}
channels.add(id);
@@ -518,9 +508,7 @@ async function handle247(ctx, message, value) {
const t247 = ctx.locale?.translate?.bind(ctx.locale);
return message.reply(build247EnabledPanel(set, id, resolved, true, ctx, guildId, t247));
} catch (e) {
const prefix = (() => {
try { return set.get("prefix") ?? "%"; } catch (_) { return "%"; }
})();
const prefix = ctx.handler.getPrefix(guildId);
const t247 = ctx.locale?.translate?.bind(ctx.locale);
const loc = (key, data = {}) => t247 ? t247(guildId, key, { ...data, prefix }) : key;
return message.reply(embed(
@@ -620,7 +608,7 @@ export const command = function() {
for (const [alias, settingKey] of Object.entries(SHORTCUTS)) {
const builder = new CommandBuilder()
.setName(alias)
.setDescription(`Shortcut for \`${settingKey}\`. Usage: %${alias} [value]`)
.setDescription(`Shortcut for \`${settingKey}\`. Usage: $prefix${alias} [value]`)
.setId(`shortcut_${alias}`)
.setCategory("util")
.setRequirement(e => e.addPermission("ManageGuild"))
@@ -710,19 +698,20 @@ export const command = function() {
export async function run(message, data) {
const set = this.getSettings(message);
const cmd = data.commandId || "getSettings";
const guildId = message.channel?.guildId ?? message.message?.guildId;
if (cmd?.startsWith("shortcut_")) {
const alias = cmd.replace("shortcut_", "");
const settingKey = SHORTCUTS[alias];
const raw = (message.content ?? message.message?.content ?? "").trim();
const prefix = set.get("prefix") ?? "%";
const prefix = this.handler.getPrefix(guildId);
const body = raw.startsWith(prefix) ? raw.slice(prefix.length).trim() : raw;
const tokens = body.split(/\s+/).slice(1);
return handleShortcut(this, message, settingKey, tokens);
}
const raw = (message.content ?? message.message?.content ?? "").trim();
const prefix = set.get("prefix") ?? "%";
const prefix = this.handler.getPrefix(guildId);
const body = raw.startsWith(prefix) ? raw.slice(prefix.length).trim() : raw;
const args = body.split(/\s+/);
const inlineShortcut = SHORTCUTS[args[1]?.toLowerCase()];
@@ -759,8 +748,10 @@ export async function run(message, data) {
}
const val = set.get(settingKey);
const description = this.settingsMgr.descriptions?.[settingKey];
const prefix = ctx.handler.getPrefix(guildId);
const resolvedDesc = description ? description.replace(/\$prefix/gi, prefix) : null;
let reply = `**${prettifySettingLabel(settingKey)}**\nValue: ${displayValue(settingKey, val)}`;
if (description) reply += `\n\n*${description}*`;
if (resolvedDesc) reply += `\n\n*${resolvedDesc}*`;
return message.reply(embed(reply));
}
@@ -803,7 +794,7 @@ export async function run(message, data) {
}
if (cmd === "helpSettings") {
const pfx = set.get("prefix") ?? "%";
const pfx = this.handler.getPrefix(guildId);
if (!settingKey) {
const keys = Object.keys(this.settingsMgr.defaults);
@@ -817,7 +808,8 @@ export async function run(message, data) {
));
}
const description = this.settingsMgr.descriptions?.[settingKey] ?? this.t(message, "responses.settings.noDescription");
const rawDescription = this.settingsMgr.descriptions?.[settingKey] ?? this.t(message, "responses.settings.noDescription");
const description = rawDescription.replace(/\$prefix/gi, pfx);
const currentVal = set.get(settingKey);
const defaultVal = this.settingsMgr.defaults?.[settingKey];
+1 -2
View File
@@ -1,7 +1,6 @@
import { CommandBuilder } from "../src/CommandHandler.mjs";
import { EmbedBuilder } from "@fluxerjs/core";
import { getGlobalColor } from "../src/MessageHandler.mjs";
import { logger } from "../src/constants/Logger.mjs";
const DEFAULT_ALIAS = "default";
@@ -112,7 +111,7 @@ export async function run(msg, data) {
return msg.reply({ embeds: [new EmbedBuilder().setColor("#ff0000").setDescription(this.t(msg, "responses._common.noVoiceChannel"))] });
}
const prefix = this.handler?.getPrefix?.(msg.message?.guildId) ?? "%";
const prefix = this.handler.getPrefix(msg.message?.guildId);
if (subCommand === "set") {
const timesRaw = data.get("times")?.value;
+1 -1
View File
@@ -38,7 +38,7 @@ export async function run(message, data) {
if (!volOption || raw === null || raw === undefined || isNaN(Number(raw))) {
const currentDefault = set?.get("volume") ?? 100;
const currentPlayerVol = Math.round((p.preferredVolume ?? 1) * 100);
const pfx = set?.get("prefix") ?? "%";
const pfx = this.handler.getPrefix(message.channel?.guildId ?? message.message?.guildId);
embed
.setTitle(this.t(message, "responses.volumedefault.title"))
.setDescription(
+1 -1
View File
@@ -96,7 +96,7 @@ function buildVotersEmbed(voters, total, page, limit, type, resourceId, expired
export async function run(msg, data) {
const fluxerlist = this.fluxerlist;
const prefix = this.handler?.getPrefix?.(msg.message?.guildId) ?? "%";
const prefix = this.handler.getPrefix(msg.message?.guildId);
const action = data.get("action")?.value ?? "info";
const type = data.get("type")?.value ?? "bot";
const resourceId = data.get("id")?.value || null;