Files
fluxer/commands/test.mjs
T
NoLogicAlan 092268281a refactor: centralize shared utilities, fix voice intent, and improve error visibility
- extract `cleanId()` to `Utils.mjs` and remove 97+ inline `String(x).replace(/\D/g, "")` duplicates

- add `VoiceStateResolver.mjs` with `iterateVoiceStates`, `hasHumansInChannel`, `getChannelsWithHumans` — eliminates 12+ duplicated voice-state-iteration blocks across `Player`, `PlayerManager`, `GatewayHandler`, `Dashboard`

- add `UI.mjs` for shared color, emoji, and timeout constants

- move `parseInlineProvider` to `providers.mjs`, `getMessageGuildId` to `MessageHandler.mjs`

- fix `VoiceStateCache.removeGuild` and `TrackOptionsManager` cache deletion mutating Maps during iteration

- fix `checkVoiceChannels` now async returning `{channelId, alreadyInVoice, hasHumans}` — all callers updated

- fix `FluxerRevoice` global `console.log` monkey-patch replaced with scoped `withSuppressedLiveKitLogs(fn)`

- fix `Player._get247Mode()` settings resolution chain

- fix `runnables.mjs` validators were no-ops

- promote `Player` magic numbers to named static constants

- replace all silent `catch (_) {}` with `logger.warn()` throughout

- remove unused deps: `express`, `genius-lyrics`, `ws`

- add `@file` JSDoc headers to all modules
2026-06-15 16:56:10 +03:00

89 lines
3.2 KiB
JavaScript

/**
* @file test.mjs — Test/debug command for voice channel user detection
* @module commands.test
*/
import { CommandBuilder } from "../src/CommandHandler.mjs";
import { EmbedBuilder } from "@fluxerjs/core";
import { getGlobalColor } from "../src/MessageHandler.mjs";
export const command = new CommandBuilder()
.setName("test")
.setDescription("Shows how many people are in each voice channel.")
.setRequirement(r => r.setOwnerOnly(true));
/**
* Execute the test command.
* @param {import("../src/MessageHandler.mjs").Message} msg - The incoming message
* @param {Map<string, {value: *}>>} data - Slash-command options map
* @returns {Promise<void>}
*/
export async function run(msg, data) {
const guild = msg.channel?.channel?.guild ?? msg.message?.guild;
if (!guild) {
const embed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(this.t(msg, "responses.test.notInServer"));
return msg.reply({ embeds: [embed] });
}
const channelCounts = new Map();
const cache = this.voiceCache ?? this.observedVoiceUsers;
if (cache) {
if (typeof cache.iterateHumanUsers === "function") {
for (const [, state] of cache.iterateHumanUsers()) {
if (state.guildId !== guild.id) continue;
channelCounts.set(state.channelId, (channelCounts.get(state.channelId) ?? 0) + 1);
}
} else {
for (const [, state] of cache) {
if (state.guildId !== guild.id) continue;
channelCounts.set(state.channelId, (channelCounts.get(state.channelId) ?? 0) + 1);
}
}
if (typeof cache.iterateBotUsers === "function") {
for (const [, state] of cache.iterateBotUsers()) {
if (state.guildId !== guild.id) continue;
channelCounts.set(state.channelId, (channelCounts.get(state.channelId) ?? 0) + 1);
}
}
}
if (channelCounts.size === 0) {
const embed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(this.t(msg, "responses.test.noOneInVoice"));
return msg.reply({ embeds: [embed] });
}
const getChannelName = async (channelId) => {
const cached = this.client.channels.get(channelId);
if (cached?.name) return cached.name;
const guildCached = guild.channels?.get?.(channelId);
if (guildCached?.name) return guildCached.name;
if (guild.channels) {
const all = typeof guild.channels.values === "function"
? [...guild.channels.values()] : Object.values(guild.channels);
const found = all.find(c => (c.id ?? c.channel_id) === channelId);
if (found?.name) return found.name;
}
try {
const fetched = await this.client.channels.fetch(channelId);
if (fetched?.name) return fetched.name;
} catch(e) { }
return `Unknown (${channelId})`;
};
let desc = "";
for (const [channelId, total] of channelCounts) {
const name = await getChannelName(channelId);
const entry = this.t(msg, "responses.test.channelEntry", { name, count: total });
if (desc.length + entry.length > 4080) break;
desc += entry;
}
const embed = new EmbedBuilder()
.setColor(getGlobalColor())
.setTitle(this.t(msg, "responses.test.title"))
.setDescription(desc.trim().slice(0, 4096))
;
msg.reply({ embeds: [embed] }).catch(() => {});
}