This commit is contained in:
NoLogicAlan committed 2026-09-11 01:47:38 +03:00
1 parent f073b78449
commit 85c4bf4e8d
9 files changed
+475 -88

No files matched your search

+125 -37
View File
@@ -1,12 +1,19 @@
/**
* @module commands/settings/channels247
* @description 24/7 channel storage, panel
* rendering and enable/disable/toggle handling for the settings command.
* Bodies are verbatim from the original single-file command.
* @description 24/7 channel storage, live-status panel rendering and
* enable/disable/toggle handling for the settings command.
*
* The panel is localized (falls back to the default locale) and shows the
* LIVE state of every saved channel:
* Connected — a healthy player is attached
* Reconnecting — a bot-level rejoin is armed right now
* Offline — no player; the watchdog sweep will re-arm a rejoin
* plus the auto-recovery line so users can see the watchdog is watching.
*/
import { cleanId } from "../../src/ui/index.mjs";
import { isPlayerConnectionDead } from "../../src/utils/Helpers247.mjs";
import { isValidFluxerId, MAX_247_CHANNELS, embed, tWrap } from "./utils.mjs";
/**
@@ -28,7 +35,8 @@ export function get247Channels(set) {
}
/**
* Save a Set of channel IDs back to the stay_247 setting.
* Writes 'none' if the set is empty.
* Writes 'none' if the set is empty. The settings manager persists
* stay_247 changes immediately (no debounce window) — see Settings.mjs.
* @param {ServerSettings} set - The guild settings instance.
* @param {Set<string>} channels - The channel IDs to save.
*/
@@ -52,42 +60,116 @@ function resolveChannelName(client, channelId) {
}
/**
* Build an embed payload showing the current 24/7 status for the guild.
* @private
* Live status of one saved 24/7 channel.
* @param {object} ctx - The bot (Remix) context.
* @param {string} guildId - The guild ID.
* @param {string} channelId - The saved channel ID.
* @param {object} t - Panel translation function.
* @returns {{line: string, state: "connected"|"reconnecting"|"offline", order: number}}
*/
function channelStatus(ctx, guildId, channelId, t) {
const player = ctx.players?.playerMap?.get(channelId)
?? [...(ctx.players?.playerMap?.values() ?? [])].find(p =>
cleanId(p?._channelId ?? "") === channelId &&
cleanId(p?._guildId ?? "") === cleanId(guildId)
);
const rejoinArmed = ctx._247RejoinTimers?.has?.(cleanId(channelId)) === true;
const name = resolveChannelName(ctx.client, channelId);
if (player && !isPlayerConnectionDead(player)) {
return {
state: "connected", order: 0,
line: t("responses.settings.247Panel.statusConnected") + " " +
(name ? "**" + name + "** " : "") + "<#" + channelId + ">",
};
}
if (rejoinArmed || player?._isJoining) {
return {
state: "reconnecting", order: 1,
line: t("responses.settings.247Panel.statusReconnecting") + " " +
(name ? "**" + name + "** " : "") + "<#" + channelId + ">",
};
}
return {
state: "offline", order: 2,
line: t("responses.settings.247Panel.statusOffline") + " " +
(name ? "**" + name + "** " : "") + "<#" + channelId + ">",
};
}
/**
* @private
* Build the panel description text (localized, live status) without the
* embed wrapper so callers can compose confirmations around it.
* @param {ServerSettings} set - The guild settings instance.
* @param {object} ctx - The bot (Remix) context.
* @param {string} guildId - The guild ID.
* @param {string|null} channelId - The channel ID (for active indicator), or null.
* @returns {object} Embed payload object for message.reply().
* @param {string|null} [justToggledId=null] - Channel just toggled (marked ←).
* @returns {string} Description text for the panel embed.
*/
export function build247Panel(set, ctx, guildId, channelId) {
const channels = [...get247Channels(set)];
function build247PanelDescription(set, ctx, guildId, justToggledId = null) {
const t = tWrap(ctx, guildId);
const prefix = ctx.handler.getPrefix(guildId);
const channels = [...get247Channels(set)];
const panelKey = (k, vars = {}) => t("responses.settings.247Panel." + k, vars);
if (channels.length > 0) {
const lines = channels.map(id => {
const name = resolveChannelName(ctx.client, id);
const isActive = id === channelId;
return (isActive ? "\u25b6 " : "\u2022 ") + (name ? "**" + name + "** " : "") + "<#" + id + ">";
});
return embed(
channels.length === 1 && channelId
? "\u2705 24/7 is now **enabled** in <#" + channelId + ">\n\n" +
"The bot will stay connected and auto-rejoin if disconnected."
: "\u2705 24/7 active in " + channels.length + " channel(s):\n\n" + lines.join("\n"),
{ title: "\u2705 24/7 Mode" }
);
const watchdog = ctx.gatewayHandler;
const watchdogOn = watchdog?.isWatchdog247Running?.() === true;
const sweepSeconds = Math.round((watchdog?._247WatchdogInterval ?? 60_000) / 1000);
const recoveryLine = watchdogOn
? panelKey("autoRecoveryLine", { seconds: sweepSeconds })
: panelKey("autoRecoveryDisabledLine");
if (channels.length === 0) {
return "\u274C 24/7 is **disabled**\n\n" +
panelKey("allChannelsDisabled") + "\n\n" +
panelKey("disabledHint", { prefix }) + "\n\n" +
recoveryLine;
}
const statuses = channels
.map(id => channelStatus(ctx, guildId, id, t))
.sort((a, b) => a.order - b.order);
const lines = statuses.map(s =>
s.line + (justToggledId && s.line.includes("<#" + cleanId(justToggledId) + ">") ? " \u2190" : "")
);
const allConnected = statuses.every(s => s.state === "connected");
return (channels.length === 1 && allConnected
? panelKey("enabledLine") + "\n\n"
: "\u2705 24/7 active in " + channels.length + " channel(s):\n\n") +
lines.join("\n") +
"\n\n" + recoveryLine;
}
/**
* Build a localized, live-status embed showing the current 24/7 state for
* the guild: every saved channel with its connection state, plus the
* auto-recovery line.
* @param {ServerSettings} set - The guild settings instance.
* @param {object} ctx - The bot (Remix) context.
* @param {string} guildId - The guild ID.
* @param {string|null} [justToggledId=null] - Channel that was just toggled
* (surfaced first and marked with ←).
* @returns {object} Embed payload object for message.reply().
*/
export function build247Panel(set, ctx, guildId, justToggledId = null) {
const t = tWrap(ctx, guildId);
const prefix = ctx.handler.getPrefix(guildId);
const panelKey = (k, vars = {}) => t("responses.settings.247Panel." + k, vars);
return embed(
"\u274c 24/7 is **disabled**\n\n" +
"The bot will leave voice channels when idle.\n\n" +
"Join a voice channel and use `" + prefix + "247` to enable.",
{ title: "\u274c 24/7 Mode" }
build247PanelDescription(set, ctx, guildId, justToggledId),
{ title: panelKey("title"), footer: panelKey("footerLine", { prefix }) }
);
}
/**
* Disable 24/7 mode for a specific channel.
* Removes the channel from stay_247, marks intentional leave, and destroys the player.
* Removes the channel from stay_247, marks intentional leave (cancels any
* armed rejoin), and destroys the player.
* @param {object} ctx - The bot (Remix) context.
* @param {ServerSettings} set - The guild settings instance.
* @param {string} guildId - The guild ID.
@@ -99,6 +181,7 @@ export async function disable247(ctx, set, guildId, channelId) {
const channels = get247Channels(set);
channels.delete(id);
save247Channels(set, channels);
ctx.cancel247Rejoin?.(id);
ctx.markIntentionalLeave?.(id);
const player = ctx.players.playerMap.get(id)
?? [...ctx.players.playerMap.values()].find(p =>
@@ -131,6 +214,7 @@ async function enable247(ctx, set, guildId, channelId) {
if (channels.size >= MAX_247_CHANNELS) {
for (const oldId of channels) {
if (oldId !== id) {
ctx.cancel247Rejoin?.(oldId);
ctx.markIntentionalLeave?.(oldId);
const oldPlayer = ctx.players.playerMap.get(oldId)
?? [...ctx.players.playerMap.values()].find(p =>
@@ -161,7 +245,8 @@ async function enable247(ctx, set, guildId, channelId) {
}
/**
* Handle the !247 toggle command. If the user is in a 24/7 channel, disable it;
* otherwise, enable it for the user's current voice channel.
* otherwise, enable it for the user's current voice channel. Replies with an
* explicit confirmation line plus the live-status panel.
* @param {object} ctx - The bot (Remix) context.
* @param {object} message - The command message.
* @param {ServerSettings} set - The guild settings instance.
@@ -169,8 +254,9 @@ async function enable247(ctx, set, guildId, channelId) {
* @returns {Promise<void>}
*/
export async function handle247Toggle(ctx, message, set, guildId) {
const loc = tWrap(ctx, guildId);
if (!guildId) return message.reply(embed(loc("responses.settings.noServer")));
const t = tWrap(ctx, guildId);
const panelKey = (k, vars = {}) => t("responses.settings.247Panel." + k, vars);
if (!guildId) return message.reply(embed(t("responses.settings.noServer")));
const { channelId: userChannelId } = await ctx.players.checkVoiceChannels(message);
if (!userChannelId) {
@@ -182,15 +268,17 @@ export async function handle247Toggle(ctx, message, set, guildId) {
if (channels.has(id)) {
await disable247(ctx, set, guildId, userChannelId);
return message.reply(build247Panel(set, ctx, guildId, id));
}
const result = await enable247(ctx, set, guildId, userChannelId);
if (result?.max) {
return message.reply(embed(
loc("responses.settings.max247Channels", { max: MAX_247_CHANNELS, prefix: ctx.handler.getPrefix(guildId) })
panelKey("confirmDisabled", { channel: id }) + "\n\n" +
build247PanelDescription(set, ctx, guildId, null),
{ title: panelKey("title") }
));
}
return message.reply(build247Panel(set, ctx, guildId, id));
await enable247(ctx, set, guildId, userChannelId);
return message.reply(embed(
panelKey("confirmEnabled", { channel: id }) + "\n\n" +
build247PanelDescription(set, ctx, guildId, id),
{ title: panelKey("title") }
));
}