diff --git a/commands/clear.mjs b/commands/clear.mjs index a2a2e67..de9fc78 100644 --- a/commands/clear.mjs +++ b/commands/clear.mjs @@ -39,13 +39,15 @@ export async function run(msg, data) { const p = await this.getPlayer(msg, false, false, false); if (!p) return; - const fromVal = data?.get?.("from")?.value; - const toVal = data?.get?.("to")?.value; + const rawFrom = data?.get?.("from")?.value; + const rawTo = data?.get?.("to")?.value; + const fromVal = (typeof rawFrom === "number" && Number.isFinite(rawFrom)) ? rawFrom : null; + const toVal = (typeof rawTo === "number" && Number.isFinite(rawTo)) ? rawTo : null; - if (fromVal != null && fromVal !== undefined) { + if (fromVal !== null) { if (fromVal < 1) return msg.replyEmbed("Queue position must be 1 or greater.").catch(() => {}); const startIdx = Math.floor(fromVal) - 1; - const endIdx = (toVal != null && toVal !== undefined && toVal >= 1) ? Math.floor(toVal) - 1 : startIdx; + const endIdx = (toVal !== null && toVal >= 1) ? Math.floor(toVal) - 1 : startIdx; const res = p.removeRange(startIdx, endIdx); return msg.replyEmbed(res).catch(() => {}); } diff --git a/src/commands/CommandHandler.mjs b/src/commands/CommandHandler.mjs index 14b8e1a..473d260 100644 --- a/src/commands/CommandHandler.mjs +++ b/src/commands/CommandHandler.mjs @@ -412,7 +412,10 @@ export class CommandHandler extends EventEmitter { const e = this._optionTypeError(errGuildId2, o, value, previous); return { opts, error: (!external) ? (this.replyHandler(e, msg) ?? true) : e }; } - if (o.empty(value)) value = o.defaultValue; + if (o.empty(value)) { + if (o.defaultValue === undefined) continue; + value = o.defaultValue; + } opts.push({ value: o.formatInput(value, this.client, msg.message), name: o.name, id: o.id, uid: o.uid }); usedOptions.push(o.uid); previous += " " + value; diff --git a/src/commands/Option.mjs b/src/commands/Option.mjs index fd32515..1ded502 100644 --- a/src/commands/Option.mjs +++ b/src/commands/Option.mjs @@ -211,8 +211,11 @@ export class Option { case "text": case "string": return i; - case "number": - return parseFloat(i); + case "number": { + if (i === undefined || i === null || i === "") return null; + const parsed = parseFloat(i); + return Number.isFinite(parsed) ? parsed : null; + } case "boolean": return i?.toLowerCase() === "true" || i === "1"; case "choice": diff --git a/src/music/PlayerManager.mjs b/src/music/PlayerManager.mjs index 6034241..3b20b81 100644 --- a/src/music/PlayerManager.mjs +++ b/src/music/PlayerManager.mjs @@ -151,7 +151,7 @@ export class PlayerManager { * @returns {boolean} True if the player is playing/staying for its channel. */ _isPlayerBusy(player) { - if (!player || player._destroyed) return false; + if (!player || player._destroyed || isPlayerConnectionDead(player)) return false; try { if (player._get247Mode?.() === "on") return true; if (player.queue?.getCurrent?.()) return true; @@ -173,7 +173,7 @@ export class PlayerManager { * @returns {boolean} True if audio is actively playing (or paused). */ _isPlayerActivelyPlaying(player) { - if (!player || player._destroyed) return false; + if (!player || player._destroyed || isPlayerConnectionDead(player)) return false; try { if (player.queue?.getCurrent?.()) return true; if (player.queue && !player.queue.isEmpty()) return true; diff --git a/src/music/player/DisplayMixin.mjs b/src/music/player/DisplayMixin.mjs index 04f4249..1686266 100644 --- a/src/music/player/DisplayMixin.mjs +++ b/src/music/player/DisplayMixin.mjs @@ -263,6 +263,7 @@ const DisplayMixin = { */ announceSong(s) { if (!s) return; + if (this._destroyed || this.leaving || (!this._voiceConn && !this.connection)) return; if (s.type === "radio") { this.emit("message", { embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(this._t("responses.radio.nowPlaying", { diff --git a/src/music/player/Player.mjs b/src/music/player/Player.mjs index e464a89..e18ea45 100644 --- a/src/music/player/Player.mjs +++ b/src/music/player/Player.mjs @@ -720,6 +720,7 @@ class Player extends EventEmitter { this._onLavalinkPlayerDisconnect = null; } } + this.queue?.reset(); this.leaving = true; this._stopInactivityTimer(); this._autoplay = false; @@ -747,6 +748,7 @@ class Player extends EventEmitter { vm.leaveChannel(this._channelId); } catch(e) { /* best effort */ } } + this.removeAllListeners(); } catch (e) { logger.error("[Player] destroy error:", e.message); } diff --git a/src/music/player/Queue.mjs b/src/music/player/Queue.mjs index 184e1be..da01e11 100644 --- a/src/music/player/Queue.mjs +++ b/src/music/player/Queue.mjs @@ -84,10 +84,14 @@ export class Queue extends EventEmitter { */ removeRange(from, to) { if (this.isEmpty()) return "The queue is empty."; + if (typeof from !== "number" || typeof to !== "number" || !Number.isFinite(from) || !Number.isFinite(to)) { + return "Invalid range."; + } const start = Math.max(0, Math.min(from, to)); const end = Math.min(this.data.length - 1, Math.max(from, to)); - if (start >= this.data.length) return "Index out of bounds"; + if (start >= this.data.length || end < start) return "Index out of bounds"; const deleteCount = (end - start) + 1; + if (deleteCount <= 0) return "Invalid range."; const removed = this.data.splice(start, deleteCount); this.emit("queue", { type: "removeRange", data: { start, end, count: removed.length, old: this.data.slice(), removed, new: this.data } }); return { count: removed.length, removed }; diff --git a/src/voice/gateway/GatewayHandler.mjs b/src/voice/gateway/GatewayHandler.mjs index 7884095..7f66c77 100644 --- a/src/voice/gateway/GatewayHandler.mjs +++ b/src/voice/gateway/GatewayHandler.mjs @@ -416,6 +416,48 @@ class GatewayHandler { client.on(Events.VoiceStatesSync, (data) => { this._handleVoiceStatesSync(data); }); + + client.on(Events.ChannelDelete ?? "channelDelete", (channel) => { + this._handleChannelDelete(channel); + }); + } + + _handleChannelDelete(channel) { + const channelId = cleanId(channel?.id ?? channel?._id); + if (!channelId) return; + + const { remix } = this; + const guildId = cleanId(channel?.guildId ?? channel?.guild?.id ?? ""); + + const player = remix?.players?.playerMap?.get?.(channelId) + ?? [...(remix?.players?.playerMap?.values?.() ?? [])].find(p => cleanId(p?._channelId) === channelId || cleanId(p?._home247Channel) === channelId); + + if (player) { + logger.voice(`[ChannelDelete] Voice channel ${channelId} deleted — destroying associated player.`); + remix.players.playerMap.delete(channelId); + const homeCh = cleanId(player._home247Channel ?? ""); + if (homeCh && homeCh !== channelId) remix.players.playerMap.delete(homeCh); + remix.players._unindexPlayer?.(player._guildId, channelId); + + const pendingScrobble = remix.players._pendingScrobbleTimers?.get?.(channelId); + if (pendingScrobble) { + clearTimeout(pendingScrobble.timer); + remix.players._pendingScrobbleTimers.delete(channelId); + } + + try { + player.queue?.reset?.(); + player.destroy(); + } catch (e) { + logger.warn("[ChannelDelete] Player destroy error:", e?.message); + } + } + + try { + if (guildId) { + remix.voiceCache?.removeChannel?.(guildId, channelId); + } + } catch (_) {} } /** diff --git a/src/voice/gateway/GuildSync.mjs b/src/voice/gateway/GuildSync.mjs index 4bf2a1b..3074434 100644 --- a/src/voice/gateway/GuildSync.mjs +++ b/src/voice/gateway/GuildSync.mjs @@ -373,11 +373,35 @@ const GuildSync = { logger.guild( `[GuildDelete] Received GuildDelete for server ${guildId} — ` + - `skipping removal (24/7 removal system disabled). ` + - `Settings and players are preserved. If the server truly removed the bot, ` + - `the GuildCreate handler will re-initialise on re-invite.` + `cleaning up active players while preserving settings for potential re-invite.` ); + const guildChannels = remix.players?._guildPlayerIndex?.get?.(cleanGuildId); + if (guildChannels) { + for (const channelId of [...guildChannels]) { + const p = remix.players.playerMap.get(channelId); + if (p) { + remix.players.playerMap.delete(channelId); + const homeCh = cleanId(p._home247Channel ?? ""); + if (homeCh && homeCh !== channelId) remix.players.playerMap.delete(homeCh); + try { + p.queue?.reset?.(); + p.destroy(); + } catch (_) {} + } + } + remix.players._guildPlayerIndex.delete(cleanGuildId); + } + for (const [key, p] of [...(remix.players?.playerMap?.entries?.() ?? [])]) { + if (cleanId(p?._guildId) === cleanGuildId) { + remix.players.playerMap.delete(key); + try { + p.queue?.reset?.(); + p.destroy(); + } catch (_) {} + } + } + remix.voiceCache.removeGuild(cleanGuildId); for (const [stateKey, info] of [...this._prevVoiceState]) { if (cleanId(info.guildId) === cleanGuildId)