diff --git a/commands/forceleave.mjs b/commands/forceleave.mjs index c3ff423..3a7e5b2 100644 --- a/commands/forceleave.mjs +++ b/commands/forceleave.mjs @@ -18,13 +18,18 @@ function embed(desc) { return { embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(desc).toJSON()] }; } +function cleanId(value) { + return String(value ?? "").replace(/\D/g, ""); +} + export async function run(msg, data) { - const cid = data.get("channelId").value; + const cid = cleanId(data.get("channelId").value); const targetChannel = this.client.channels.cache.get(cid); if (!targetChannel) return msg.replyEmbed(embed("❌ Channel not found.")); - if (msg.message.guildId !== targetChannel.guildId) + if (cleanId(msg.message.guildId) !== cleanId(targetChannel.guildId)) return msg.replyEmbed(embed("❌ This command has to be run in the same server as the voice channel.")); - const p = this.players.playerMap.get(cid); + const p = this.players.playerMap.get(cid) + ?? [...this.players.playerMap.values()].find((player) => cleanId(player?._channelId) === cid); if (!p) return msg.replyEmbed(embed("❌ Player not found.")); if (!p.connection) return msg.replyEmbed(embed("❌ Player not initialized.")); await this.players.leave(msg, cid); diff --git a/commands/join.mjs b/commands/join.mjs index 26285e6..5fbbc27 100644 --- a/commands/join.mjs +++ b/commands/join.mjs @@ -3,6 +3,20 @@ import { EmbedBuilder } from "@fluxerjs/core"; import { getGlobalColor } from "../src/MessageHandler.mjs"; import Player from "../src/Player.mjs"; +function getGuildId(message) { + return message?.channel?.guildId + ?? message?.channel?.guild?.id + ?? message?.message?.guildId + ?? message?.message?.guild?.id + ?? message?.channel?.server_id + ?? message?.channel?.serverId + ?? null; +} + +function cleanId(value) { + return String(value ?? "").replace(/\D/g, ""); +} + export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) { if (!this.client.channels.cache.has(cid)) { ecb(); @@ -11,8 +25,11 @@ export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) { .toJSON(); return message.replyEmbed({ embeds: [embed] }); } - if (this.players.playerMap.has(cid)) { - cb(this.players.playerMap.get(cid)); + const cleanChannelId = cleanId(cid); + const existing = this.players.playerMap.get(cleanChannelId) + ?? [...this.players.playerMap.values()].find((player) => cleanId(player?._channelId) === cleanChannelId); + if (existing) { + cb(existing); const embed = new EmbedBuilder().setColor(getGlobalColor()) .setDescription("Already joined <#" + cid + ">.") .toJSON(); @@ -41,11 +58,7 @@ export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) { p.on("autoleave", () => { const activeChannelId = String(p._channelId ?? cid).replace(/\D/g, "") || cid; const homeChannelId = String(p._home247Channel ?? activeChannelId).replace(/\D/g, "") || activeChannelId; - const guildId = message.channel?.guildId - ?? message.channel?.guild?.id - ?? message.message?.guildId - ?? message.channel?.server_id - ?? message.channel?.serverId; + const guildId = getGuildId(message); const is247 = (() => { try { const raw = this.settingsMgr?.getServer?.(guildId)?.get?.("stay_247"); @@ -54,7 +67,7 @@ export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) { })(); const prefix = (() => { try { - return this._commands?.getPrefix?.(message.channel?.guild?.id) ?? "%"; + return this._commands?.getPrefix?.(guildId) ?? "%"; } catch (_) { return "%"; } })(); const desc = is247 @@ -71,7 +84,7 @@ export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) { }); p.on("message", m => { - const guildId = message.channel?.guild?.id ?? message.channel?.server_id ?? message.channel?.serverId; + const guildId = getGuildId(message); const raw = this.settingsMgr?.getServer?.(guildId)?.get("songAnnouncements"); const disabled = raw === false || raw === 0 || ["false","0","no","off","disable"].includes(String(raw).toLowerCase().trim()); @@ -80,17 +93,17 @@ export async function joinChannel(message, cid, cb = () => {}, ecb = () => {}) { message.channel.sendEmbed({ embeds: [embed] }); }); - this.players.playerMap.set(cid, p); + this.players.playerMap.set(cleanChannelId, p); const joiningEmbed = new EmbedBuilder().setColor(getGlobalColor()).setDescription("⏳ Joining Channel...").toJSON(); const statusMsg = await message.replyEmbed({ embeds: [joiningEmbed] }); try { - await p.join(cid); + await p.join(cleanChannelId); const okEmbed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(`✅ Successfully joined <#${cid}>`).toJSON(); await statusMsg.editEmbed({ embeds: [okEmbed] }); cb(p); } catch (e) { - this.players.playerMap.delete(cid); + this.players.playerMap.delete(cleanChannelId); p.destroy(); const errEmbed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(`❌ Failed to join: ${e.message}`).toJSON(); await statusMsg.editEmbed({ embeds: [errEmbed] }); @@ -125,7 +138,7 @@ export function run(message, data) { resolvedId = idMatch[1]; } else { // Try to look up by name - const serverId = message.channel?.server_id ?? message.channel?.serverId; + const guildId = cleanId(getGuildId(message)); const allChannels = [ ...(this._commands?.client?.channels?.values?.() ?? this._commands?.client?.channels?.cache?.values?.() ?? @@ -133,9 +146,9 @@ export function run(message, data) { this.client?.channels?.cache?.values?.() ?? []) ]; const match = allChannels.find(c => { - const cServerId = c.server_id ?? c.serverId ?? c.guildId; + const cServerId = cleanId(c.guildId ?? c.guild?.id ?? c.server_id ?? c.serverId); const isVoice = c.channel_type === "VoiceChannel" || c.type === "VoiceChannel" || c.type === 2; - return isVoice && cServerId === serverId && + return isVoice && cServerId === guildId && (c.name?.toLowerCase() === rawArg.toLowerCase()); }); if (match) resolvedId = match._id ?? match.id; @@ -155,7 +168,7 @@ export function run(message, data) { const cid = this.players.checkVoiceChannels(message); if (!cid) { - const prefix = this._commands?.getPrefix?.(message.channel?.guild?.id ?? message.channel?.server_id ?? message.channel?.serverId) ?? "%"; + const prefix = this._commands?.getPrefix?.(getGuildId(message)) ?? "%"; const embed = new EmbedBuilder().setColor(getGlobalColor()) .setDescription(`❌ You're not in a voice channel. Please join one first, or specify a channel: \`${prefix}join <#channel>\``) .toJSON(); diff --git a/commands/settings.mjs b/commands/settings.mjs index ebb683a..d9af642 100644 --- a/commands/settings.mjs +++ b/commands/settings.mjs @@ -56,7 +56,13 @@ function displayValue(key, value) { /** Resolve guild ID from a message context */ function getGuildId(message) { - return message.message?.guild?.id ?? message.channel?.guild?.id ?? null; + return message.message?.guildId + ?? message.message?.guild?.id + ?? message.channel?.guildId + ?? message.channel?.guild?.id + ?? message.channel?.server_id + ?? message.channel?.serverId + ?? null; } /** Resolve guild name from a message context */ diff --git a/src/PlayerManager.mjs b/src/PlayerManager.mjs index 75b17c1..1841e92 100644 --- a/src/PlayerManager.mjs +++ b/src/PlayerManager.mjs @@ -12,6 +12,7 @@ import { SettingsManager } from "./Settings.mjs"; import { Utils } from "./Utils.mjs"; import { logger } from "./constants/Logger.mjs"; import { EmbedBuilder } from "@fluxerjs/core"; +import { getVoiceManager } from "@fluxerjs/voice"; import { getGlobalColor } from "./MessageHandler.mjs"; /** Helper — build a plain embed payload from a description string */ @@ -19,6 +20,36 @@ function mkEmbed(desc) { return { embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(desc).toJSON()] }; } +function cleanId(value) { + return String(value ?? "").replace(/\D/g, ""); +} + +function getMessageGuildId(message) { + return message?.channel?.guildId ?? + message?.channel?.guild?.id ?? + message?.message?.guildId ?? + message?.message?.guild?.id ?? + message?.channel?.server_id ?? + message?.channel?.serverId ?? + message?.message?.server_id ?? + message?.message?.serverId ?? + null; +} + +function getPlayerGuildId(player, fallbackChannel = null) { + return cleanId( + player?._guildId ?? + fallbackChannel?.guildId ?? + fallbackChannel?.guild?.id ?? + fallbackChannel?.server_id ?? + fallbackChannel?.serverId + ); +} + +function getPlayerChannelId(player, fallbackChannelId = null) { + return cleanId(player?._channelId ?? player?._home247Channel ?? fallbackChannelId); +} + export class PlayerManager { /** @type {SettingsManager} */ settings; @@ -61,30 +92,85 @@ export class PlayerManager { checkVoiceChannels(message) { if (!message) return null; - const userId = message.author?.id; - const serverId = - message.channel?.server_id ?? - message.channel?.serverId ?? - message.channel?.guild?.id ?? - message.channel?.guildId ?? - message.message?.server_id ?? - message.message?.serverId ?? - message.message?.guildId; + const userId = message.author?.id ?? message.message?.author?.id; + const guildId = getMessageGuildId(message); if (!userId) return null; + if (!guildId) return null; - const cleanServer = serverId ? String(serverId).replace(/\D/g, "") : null; + const cleanGuild = cleanId(guildId); + const seedObserved = (channelId) => { + const cleanChannelId = cleanId(channelId); + if (!cleanChannelId) return null; + if (!this.observedVoiceUsers?.has(userId)) { + this.observedVoiceUsers?.set(userId, { channelId: cleanChannelId, guildId: cleanGuild }); + } + return cleanId(this.observedVoiceUsers?.get(userId)?.channelId) || cleanChannelId; + }; - // Fallback: find any existing player in this server - if (cleanServer) { - for (const [channelId] of this.playerMap) { - const channel = this.commands.client?.channels?.get(channelId) ?? - this.commands.client?.channels?.cache?.get(channelId); - const chGuild = String(channel?.guildId ?? channel?.server_id ?? channel?.serverId ?? "").replace(/\D/g, ""); - if (chGuild === cleanServer) { - return channelId; + const observed = this.observedVoiceUsers?.get?.(userId); + if (cleanId(observed?.guildId) === cleanGuild) { + const observedChannelId = cleanId(observed?.channelId); + if (observedChannelId) return observedChannelId; + } + + try { + const vm = getVoiceManager(this.commands.client); + const voiceChannelId = vm?.getVoiceChannelId?.(guildId, userId); + const seeded = seedObserved(voiceChannelId); + if (seeded) return seeded; + } catch (_) {} + + const memberVoiceChannelId = + message?.member?.voice?.channelId ?? + message?.message?.member?.voice?.channelId ?? + null; + { + const seeded = seedObserved(memberVoiceChannelId); + if (seeded) return seeded; + } + + try { + const guild = + this.commands.client?.guilds?.cache?.get?.(guildId) ?? + this.commands.client?.guilds?.cache?.get?.(cleanGuild); + const voiceStates = guild?.voice_states ?? guild?.voiceStates?.cache ?? guild?.voiceStates ?? null; + if (voiceStates) { + if (!Array.isArray(voiceStates) && typeof voiceStates === "object") { + const direct = voiceStates[userId]; + const directChannelId = + typeof direct === "string" + ? direct + : direct?.channelId ?? direct?.channel_id ?? null; + const seeded = seedObserved(directChannelId); + if (seeded) return seeded; + } + + const entries = Array.isArray(voiceStates) + ? voiceStates + : typeof voiceStates.values === "function" + ? voiceStates.values() + : Object.values(voiceStates); + + for (const state of entries) { + const stateUserId = state?.userId ?? state?.user_id ?? state?.id; + const stateGuildId = cleanId(state?.guildId ?? state?.guild_id ?? guildId); + if (stateUserId !== userId || stateGuildId !== cleanGuild) continue; + const seeded = seedObserved(state?.channelId ?? state?.channel_id); + if (seeded) return seeded; } } + } catch (_) {} + + const liveGuildPlayers = [...this.playerMap.entries()].filter(([channelId, player]) => { + const fallbackChannel = + this.commands.client?.channels?.get?.(channelId) ?? + this.commands.client?.channels?.cache?.get?.(channelId) ?? + null; + return getPlayerGuildId(player, fallbackChannel) === cleanGuild; + }); + if (liveGuildPlayers.length === 1) { + return getPlayerChannelId(liveGuildPlayers[0][1], liveGuildPlayers[0][0]) || cleanId(liveGuildPlayers[0][0]); } return null; } @@ -102,24 +188,16 @@ export class PlayerManager { * @returns {Promise} */ async getPlayer(message, promptJoin = true, verifyUser = true, shouldJoin = false) { - const serverId = - message.channel?.server_id ?? - message.channel?.serverId ?? - message.channel?.guild?.id ?? - message.channel?.guildId ?? - message.message?.server_id ?? - message.message?.serverId ?? - message.message?.guildId; - - const cleanServerId = serverId ? String(serverId).replace(/\D/g, "") : null; + const guildId = getMessageGuildId(message); + const cleanGuildId = cleanId(guildId); const userChannelId = this.checkVoiceChannels(message); - const cleanUserChannelId = userChannelId ? String(userChannelId).replace(/\D/g, "") : null; + const cleanUserChannelId = cleanId(userChannelId); if (cleanUserChannelId) { const player = this.playerMap.get(cleanUserChannelId) ?? [...this.playerMap.values()].find(p => - String(p?._channelId ?? "").replace(/\D/g, "") === cleanUserChannelId + getPlayerChannelId(p) === cleanUserChannelId ); if (player) { player.textChannel = message.channel; @@ -127,10 +205,9 @@ export class PlayerManager { } } - const serverPlayers = cleanServerId + const serverPlayers = cleanGuildId ? [...this.playerMap.entries()].filter(([, player]) => { - const playerGuildId = String(player?._guildId ?? "").replace(/\D/g, ""); - return playerGuildId === cleanServerId; + return getPlayerGuildId(player) === cleanGuildId; }) : []; @@ -166,7 +243,7 @@ export class PlayerManager { const prefix = (() => { try { - return this.settings.getServer(serverId)?.get("prefix") ?? "%"; + return this.settings.getServer(guildId)?.get("prefix") ?? "%"; } catch (_) { return "%"; } })(); message.replyEmbed(mkEmbed(`⚠️ I'm already playing in ${channelList}. Join that channel or use \`${prefix}play\` to start music in your channel.`)); @@ -206,13 +283,15 @@ export class PlayerManager { return new Promise(resolve => this.initPlayer(msg, autoDetected, (p) => resolve(p))); } - const serverId = msg.channel?.server_id ?? msg.channel?.serverId; - const allChannels = serverId + const guildId = getMessageGuildId(msg); + const cleanGuildId = cleanId(guildId); + const allChannels = cleanGuildId ? [...(this.commands.client?.channels?.values?.() ?? this.commands.client?.channels?.cache?.values?.() ?? [])] .filter(c => { - const cServerId = c.server_id ?? c.serverId; - return cServerId === serverId && c.channel_type === "VoiceChannel"; + const channelGuildId = cleanId(c.guildId ?? c.guild?.id ?? c.server_id ?? c.serverId); + const isVoice = c.channel_type === "VoiceChannel" || c.type === "VoiceChannel" || c.type === 2; + return channelGuildId === cleanGuildId && isVoice; }) : []; @@ -342,8 +421,10 @@ export class PlayerManager { return message.replyEmbed(mkEmbed("❌ Please join a **voice channel** first before using this command!")); } - if (this.playerMap.has(cid)) { - const existing = this.playerMap.get(cid); + const cleanChannelId = cleanId(cid); + const existing = this.playerMap.get(cleanChannelId) + ?? [...this.playerMap.values()].find((entry) => getPlayerChannelId(entry) === cleanChannelId); + if (existing) { existing.textChannel = message.channel; cb(existing); return message.replyEmbed(mkEmbed(`Already joined <#${cid}>.`)); @@ -362,19 +443,19 @@ export class PlayerManager { player.textChannel = message.channel; player.on("autoleave", () => { - const activeChannelId = String(player._channelId ?? cid).replace(/\D/g, "") || cid; - const homeChannelId = String(player._home247Channel ?? activeChannelId).replace(/\D/g, "") || activeChannelId; + const activeChannelId = getPlayerChannelId(player, cleanChannelId) || cleanChannelId; + const homeChannelId = cleanId(player._home247Channel) || activeChannelId; const ch = player.textChannel; - const serverId = ch?.server_id ?? ch?.serverId ?? ch?.guild?.id; + const guildId = cleanId(player._guildId ?? ch?.guildId ?? ch?.guild?.id ?? getMessageGuildId({ channel: ch })); const is247 = (() => { try { - const raw = this.settings.getServer(serverId)?.get("stay_247"); + const raw = this.settings.getServer(guildId)?.get("stay_247"); return raw && raw !== "none"; } catch (_) { return false; } })(); const prefix = (() => { try { - return this.settings.getServer(serverId)?.get("prefix") ?? "%"; + return this.settings.getServer(guildId)?.get("prefix") ?? "%"; } catch (_) { return "%"; } })(); const desc = is247 @@ -382,7 +463,7 @@ export class PlayerManager { : `Left channel <#${activeChannelId}> because of inactivity.\nIf you want me to stay in voice, use \`${prefix}247 on/auto\``; ch?.sendEmbed(mkEmbed(desc)); this.playerMap.delete(activeChannelId); - if (activeChannelId !== cid) this.playerMap.delete(cid); + if (activeChannelId !== cleanChannelId) this.playerMap.delete(cleanChannelId); if (homeChannelId !== activeChannelId) this.playerMap.delete(homeChannelId); player.destroy(); }); @@ -391,15 +472,15 @@ export class PlayerManager { player.on("message", (m) => { const ch = player.textChannel; - const serverId = ch?.server_id ?? ch?.serverId ?? ch?.guild?.id; - const raw = this.settings.getServer(serverId)?.get("songAnnouncements"); + const guildId = cleanId(player._guildId ?? ch?.guildId ?? ch?.guild?.id ?? getMessageGuildId({ channel: ch })); + const raw = this.settings.getServer(guildId)?.get("songAnnouncements"); const disabled = raw === false || raw === 0 || ["false","0","no","off","disable"].includes(String(raw).toLowerCase().trim()); if (disabled) return; ch?.sendEmbed(typeof m === "object" && Array.isArray(m.embeds) ? m : mkEmbed(m)); }); - this.playerMap.set(cid, player); + this.playerMap.set(cleanChannelId, player); (async () => { const statusMsg = await message.replyEmbed(mkEmbed("⏳ Joining Channel...")); @@ -407,9 +488,9 @@ export class PlayerManager { await player.join(cid); await statusMsg.editEmbed(mkEmbed(`✅ Successfully joined <#${cid}>`)); - const serverId = message.channel?.server_id ?? message.channel?.serverId; - if (serverId) { - const savedVol = this.settings.getServer(serverId)?.get("volume"); + const guildId = cleanId(channel.guildId ?? getMessageGuildId(message)); + if (guildId) { + const savedVol = this.settings.getServer(guildId)?.get("volume"); if (savedVol !== undefined && savedVol !== null) { const vol = Number(savedVol); if (!isNaN(vol)) player.setVolume(vol / 100); @@ -419,9 +500,35 @@ export class PlayerManager { cb(player); } catch (err) { await statusMsg.editEmbed(mkEmbed(`❌ Failed to join: ${err.message}`)).catch(() => {}); - this.playerMap.delete(cid); + this.playerMap.delete(cleanChannelId); player.destroy(); } })(); } -} \ No newline at end of file + + async leave(msg, cid) { + if (!cid) { + const guildId = getMessageGuildId(msg); + if (guildId) { + const matchedEntry = [...this.playerMap.entries()].find(([, player]) => + getPlayerGuildId(player) === cleanId(guildId) + ); + cid = getPlayerChannelId(matchedEntry?.[1], matchedEntry?.[0]) || matchedEntry?.[0] || null; + } + } + + const cleanChannelId = cleanId(cid); + const player = cleanChannelId + ? this.playerMap.get(cleanChannelId) ?? + [...this.playerMap.values()].find((entry) => getPlayerChannelId(entry) === cleanChannelId) + : null; + if (!player) return msg.replyEmbed(mkEmbed("I'm not in a voice channel.")); + + const activeChannelId = getPlayerChannelId(player, cleanChannelId) || cleanChannelId; + this.playerMap.delete(activeChannelId); + if (activeChannelId !== cleanChannelId) this.playerMap.delete(cleanChannelId); + await msg.replyEmbed(mkEmbed("✅ Successfully Left")); + await player.leave(); + player.destroy(); + } +}