Files
fluxer/commands/leave.mjs
T
NoLogicAlan 73c290e59c fix(voice): resolve voice channel detection after bot reboot
- Move voice state population outside settings conditional in GuildCreate
  (now runs on every gateway resume, not just new guild joins)
- Make checkVoiceChannels() async with Fluxer API fallback
  (fetches member voice state directly when cache is empty after reboot)
- Fix PlayerManager client reference passing
- Update all callers to properly await checkVoiceChannels()
- Fix leave command to work without requiring user in voice channel

Fixes issue where bot couldn't detect users already in voice channels
immediately after a reboot due to empty voice state cache.
2026-04-16 23:41:25 +03:00

80 lines
2.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { CommandBuilder } from "../src/CommandHandler.mjs";
import { logger } from "../src/constants/Logger.mjs";
import { EmbedBuilder } from "@fluxerjs/core";
import { getGlobalColor } from "../src/MessageHandler.mjs";
export const command = new CommandBuilder()
.setName("leave")
.setDescription("Make the bot leave your current voice channel", "commands.leave")
.addAliases("l", "stop")
.setCategory("music");
function embed(desc) {
return { embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(desc).toJSON()] };
}
export async function run(msg) {
const guildId = msg.channel?.guild?.id ?? msg.message?.guildId;
// Get all players in this guild
const guildPlayers = [...this.players.playerMap.entries()]
.filter(([chId, p]) => p._guildId === guildId);
if (guildPlayers.length === 0) {
return msg.replyEmbed(embed("❌ I'm not in a voice channel in this server."));
}
let cid;
// Try to find the channel the user is in (optional, for convenience)
const userChannelId = await this.players.checkVoiceChannels(msg);
if (userChannelId && guildPlayers.some(([chId]) => chId === userChannelId)) {
// User is in a channel where the bot is playing - use that one
cid = userChannelId;
} else if (guildPlayers.length === 1) {
// Only one player in guild - use that one
cid = guildPlayers[0][0];
} else {
// Multiple channels - use the first one found
// Or you could implement a selection menu here
cid = guildPlayers[0][0];
}
cid = String(cid).replace(/\D/g, "");
const player = this.players.playerMap.get(cid);
if (!player?.connection) return msg.replyEmbed(embed("❌ Player not initialized."));
const set = this.getSettings(msg);
const raw = set?.get("stay_247");
const ch247 = (!raw || raw === "none")
? new Set()
: Array.isArray(raw)
? new Set(raw.map(id => String(id).replace(/\D/g, "")).filter(Boolean))
: new Set([String(raw).replace(/\D/g, "")]);
if (ch247.has(cid)) {
const mode = set?.get("stay_247_mode") ?? "auto";
this.markIntentionalLeave(cid);
this.players.playerMap.delete(cid);
await player.leave().catch(() => {});
player.destroy();
if (mode === "auto") {
msg.replyEmbed(embed(`✅ Successfully Left — rejoining <#${cid}> in 5 seconds.\nTo disable 24/7 mode permanently, use \`%247 off\`.`));
const leave247Delay = this.config?.timers?.leave247RejoinDelay ?? 5000;
setTimeout(() => {
if (this._spawnPlayer) {
this._spawnPlayer(guildId, cid).catch(e =>
logger.warn("[leave] 247 rejoin failed for", cid, e.message)
);
}
}, leave247Delay);
} else {
msg.replyEmbed(embed(`✅ Successfully Left.\nℹ️ 24/7 mode is **on** — bot won't rejoin automatically. Use \`%play\` to bring it back, or \`%247 off\` to fully disable.`));
}
} else {
await this.leaveChannel(cid, guildId, msg);
}
}