Files
fluxer/src/PlayerManager.mjs
T
2026-05-25 16:51:22 +03:00

1184 lines
42 KiB
JavaScript

/**
* PlayerManager.mjs — Manages voice channel players across servers
*
* Updated for moonlink.js: passes the MoonlinkManager instance into every
* new Player so it can resolve tracks and retrieve session IDs.
*/
import Player from "./Player.mjs";
import { CommandHandler } from "./CommandHandler.mjs";
import { Message } from "./MessageHandler.mjs";
import { SettingsManager } from "./Settings.mjs";
import { Utils } from "./Utils.mjs";
import { logger } from "./constants/Logger.mjs";
import { get247ChannelMode } from "./constants/Helpers247.mjs";
import { EmbedBuilder } from "@fluxerjs/core";
import { getVoiceManager } from "@fluxerjs/voice";
import { getGlobalColor } from "./MessageHandler.mjs";
import { Dashboard } from "./dashboard/Dashboard.mjs";
/** Helper — build a plain embed payload from a description string */
function mkEmbed(desc) {
return { embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(desc)] };
}
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);
}
/**
* Check whether the bot actually has the Connect permission on a specific
* voice channel. This uses @fluxerjs/core's PermissionFlags to test the
* bitfield directly, rather than relying on gateway error messages which
* conflate real permission denials with stale-session 401s.
*
* @param {import('@fluxerjs/core').Client} client
* @param {string} channelId
* @returns {boolean} true if the bot has Connect (and Speak) on the channel
*/
function botHasVoicePermissions(client, channelId) {
try {
const channel = client?.channels?.get?.(channelId);
if (!channel) return true;
const me = channel.guild?.members?.me;
if (!me) return true;
const perms = me.permissionsIn?.(channel);
if (!perms) return true;
if (perms.has(0x8)) return true;
return perms.has(0x400) && perms.has(0x100000) && perms.has(0x200000);
} catch (_) {
return true;
}
}
/**
* Sanitize raw voice-join error messages into user-friendly text.
*
* IMPORTANT: A 401/"permission" error from the gateway does NOT always mean
* the bot lacks the Connect permission. The Fluxer gateway can also return 401
* when the bot's previous voice session hasn't been cleaned up yet (stale
* session race). In that case the bot DOES have Connect — it's a transient
* error, not a real permission denial.
*
* To tell the difference, we check the actual channel permissions using
* botHasVoicePermissions(). If the bot HAS Connect but got 401, it's a
* stale session ("SESSION_RACE"), not a real permission error ("PERMISSION").
*/
/**
* Check if a user might be in a voice channel that the bot cannot see
* (e.g. private channel where the bot lacks View Channel permission).
*
* On Fluxer, if the guild has voice states but none match the user,
* AND the user is a known member, they are likely in a hidden channel.
* The gateway may still send partial voice state data through the member
* object even when the bot can't see the channel itself.
*
* @param {Object} client - Fluxer client
* @param {string} userId - User ID to check
* @param {string} guildId - Guild ID
* @returns {boolean} true if the user appears to be in an invisible voice channel
*/
function userMightBeInHiddenVoice(client, userId, guildId) {
if (!client || !userId || !guildId) return false;
const cleanGuild = cleanId(guildId);
try {
const guild = client.guilds?.get?.(cleanGuild) ?? client.guilds?.get?.(guildId);
if (!guild) return false;
const member = guild.members?.get?.(userId);
if (!member) return false;
const memberVoiceChannelId =
member.voice?.channelId ??
member.voice_channel_id ??
member.voiceChannelId ??
null;
if (memberVoiceChannelId) return true;
} catch (_) {}
return false;
}
function sanitizeJoinError(err, client = null, channelId = null) {
const msg = String(err?.message ?? err ?? "");
if (msg.includes("401") || msg.includes("Unauthorized")) {
if (client && channelId && !botHasVoicePermissions(client, channelId)) {
return "PERMISSION";
}
return "SESSION_RACE";
}
if (msg.includes("permission") || msg.includes("Permission")) {
if (client && channelId && !botHasVoicePermissions(client, channelId)) {
return "PERMISSION";
}
return "SESSION_RACE";
}
if (msg.includes("not found") || msg.includes("Unknown channel")) {
return "NOT_FOUND";
}
if (msg.includes("timeout") || msg.includes("timed out")) {
return "TIMEOUT";
}
return null;
}
export class PlayerManager {
/** @type {SettingsManager} */
settings;
/** @type {CommandHandler} */
commands;
/** @type {Map<string, Player>} Keyed by cleaned channel ID */
playerMap = new Map();
/** @type {Map<string, Set<string>>} Reverse index: guildId → Set<channelId> for O(1) guild lookups */
_guildPlayerIndex = new Map();
/** @type {Set<string>} Channel IDs currently being joined (not yet in playerMap) */
_pendingJoins = new Set();
/** @type {Map<string, {timer, songUrl, startedAtMs}>} Pending scrobble timers keyed by channel ID.
* Prevents duplicate scrobbles when startplay fires multiple times. */
_pendingScrobbleTimers = new Map();
/** @type {Object} */
config;
/** @type {Object} */
playerConfig;
/** @type {import("./constants/Locale.mjs").Locale|null} */
locale = null;
/** @type {import("./dashboard/Dashboard.mjs").Dashboard|null} */
dashboard = null;
/**
* @param {SettingsManager} settings
* @param {CommandHandler} commands
* @param {Object} config
* @param {Object} config.config - Parsed config.json
* @param {Object} config.player - Config data passed to new Player instances
*/
constructor(settings, commands, config) {
this.commands = commands;
this.settings = settings;
this.config = config.config;
this.playerConfig = config.player;
this.dashboard = config.dashboard ?? null;
this.locale = config.locale ?? null;
this.timers = config.timers ?? {};
this._lastfm = null;
this.trackOptions = config.trackOptions ?? null;
}
/**
* Add a channel to the guild→player reverse index.
* Called after a successful player join.
* @param {string} guildId
* @param {string} channelId
*/
_indexPlayer(guildId, channelId) {
const gId = cleanId(guildId);
const cId = cleanId(channelId);
if (!gId || !cId) return;
let set = this._guildPlayerIndex.get(gId);
if (!set) { set = new Set(); this._guildPlayerIndex.set(gId, set); }
set.add(cId);
}
/**
* Remove a channel from the guild→player reverse index.
* Called on player leave/destroy.
* @param {string} guildId
* @param {string} channelId
*/
_unindexPlayer(guildId, channelId) {
const gId = cleanId(guildId);
const cId = cleanId(channelId);
if (!gId) return;
const set = this._guildPlayerIndex.get(gId);
if (set) {
set.delete(cId);
if (set.size === 0) this._guildPlayerIndex.delete(gId);
}
}
/**
* Get all [channelId, Player] entries for a guild.
* Uses the reverse index for O(1) guild lookup instead of O(n) scan.
* @param {string} guildId
* @returns {Array<[string, Player]>}
*/
getGuildPlayers(guildId) {
const gId = cleanId(guildId);
const set = this._guildPlayerIndex.get(gId);
if (!set) return [];
const result = [];
for (const channelId of set) {
const player = this.playerMap.get(channelId);
if (!player || player._destroyed) {
set.delete(channelId);
continue;
}
result.push([channelId, player]);
}
return result;
}
/**
* Find a player by guildId and channelId using the reverse index.
* @param {string} guildId
* @param {string} channelId
* @returns {Player|null}
*/
getPlayerByGuildAndChannel(guildId, channelId) {
const cId = cleanId(channelId);
const players = this.getGuildPlayers(guildId);
for (const [mapChannelId, player] of players) {
if (getPlayerChannelId(player, mapChannelId) === cId) return player;
}
return null;
}
/**
* Find a player by channelId alone (scans guild index first, then fallback).
* @param {string} channelId
* @returns {Player|null}
*/
getPlayerByChannelId(channelId) {
const cId = cleanId(channelId);
for (const [, channelSet] of this._guildPlayerIndex) {
for (const mapChannelId of channelSet) {
const player = this.playerMap.get(mapChannelId);
if (player && getPlayerChannelId(player, mapChannelId) === cId) return player;
}
}
return null;
}
/**
* Forward player lifecycle/state events to the dashboard pub/sub channels.
*
* IMPORTANT: Events are sent in standard { type, data } format so
* the backend (backend-master) PlayerManager can parse them correctly.
*
* Two channels are used:
* {platform}:players — global, for init/close lifecycle events
* {platform}:player_{id} — per-player, for playback/queue/volume events
*
* @param {Player} player
* @param {Object} [context]
* @param {string|null} [context.channelId]
* @param {string|null} [context.guildId]
* @returns {Player}
*/
setupEvents(player, context = {}) {
if (!player || player._dashboardEventsBound) return player;
Object.defineProperty(player, "_dashboardEventsBound", {
value: true,
configurable: true,
enumerable: false,
writable: true,
});
const emit = (type, data) => {
if (!this.dashboard?.enabled) return;
this.dashboard.updatePlayer({ type, data }, player);
};
const emitGlobal = (type) => {
if (!this.dashboard?.enabled) return;
this.dashboard.playerUpdate({ type }, player);
};
const sendUserUpdates = (eventType) => {
if (!this.dashboard?.enabled) return;
const channelId = getPlayerChannelId(player, context.channelId);
const channel = player.client?.channels?.get(channelId);
const guild = player.client?.guilds?.get(cleanId(player._guildId ?? context.guildId));
if (!guild) return;
const voiceStates = guild.voice_states ?? guild.voiceStates ?? null;
if (!voiceStates) return;
const entries = Array.isArray(voiceStates)
? voiceStates
: typeof voiceStates.values === "function"
? [...voiceStates.values()]
: Object.values(voiceStates);
for (const state of entries) {
if (!state?.channelId && !state?.channel_id) continue;
const stateChannelId = cleanId(state.channelId ?? state.channel_id);
if (stateChannelId !== channelId) continue;
const member = guild.members?.get?.(state.userId ?? state.user_id);
if (!member?.user || member.user?.bot) continue;
emit(eventType, member.user.id);
this.dashboard.userUpdate({
type: eventType,
guildId: cleanId(player._guildId ?? context.guildId),
channelId,
}, member.user);
}
};
player.on("roomfetched", () => {
emitGlobal("init");
sendUserUpdates("join");
});
player.on("startplay", (song) => {
emit("startplay", Dashboard.convertVideo(song ?? player.queue?.current));
emit("streamStartPlay", Date.now());
this.dashboard.playerUpdate({ type: "startplay" }, player);
if (this._lastfm?.enabled && song) {
this._handleLastFmStartPlay(player, song);
}
});
player.on("stopplay", () => {
emit("stopplay", null);
this.dashboard.playerUpdate({ type: "stopplay" }, player);
});
player.on("playback", (playing) => {
const elapsedMs = player._pausedAt
? (player._pausedAt.getTime?.() ?? Number(player._pausedAt)) -
(player.startedPlaying?.getTime?.() ?? Number(player.startedPlaying ?? 0))
: Date.now() - (player.startedPlaying?.getTime?.() ?? Number(player.startedPlaying ?? 0));
const type = playing ? "resume" : "pause";
emit(type, { elapsedTime: Math.max(0, elapsedMs) });
this.dashboard.playerUpdate({ type }, player);
});
player.on("volume", (volume) => {
emit("volume", volume);
this.dashboard.playerUpdate({ type: "volume" }, player);
});
player.on("filter", (filter) => {
emit("filter", filter);
});
player.on("update", (scope) => {
this.dashboard.playerUpdate({ type: "update" }, player);
});
player.on("message", (message) => {
});
player.on("autoleave", () => {
sendUserUpdates("leave");
emitGlobal("close");
emit("stopplay", null);
});
player.on("leave", () => {
sendUserUpdates("leave");
emitGlobal("close");
emit("stopplay", null);
});
player.queue?.on("queue", (queueEvent) => {
const serialised = { type: queueEvent.type };
switch (queueEvent.type) {
case "add":
serialised.data = {
append: queueEvent.data?.append,
data: Dashboard.convertVideo(queueEvent.data?.data),
};
break;
case "addMany":
serialised.data = {
append: queueEvent.data?.append,
tracks: (queueEvent.data?.tracks ?? []).map(v => Dashboard.convertVideo(v)),
};
break;
case "remove":
serialised.data = {
index: queueEvent.data?.index,
removed: Dashboard.convertVideo(queueEvent.data?.removed),
old: (queueEvent.data?.old ?? []).map(v => Dashboard.convertVideo(v)),
new: (queueEvent.data?.new ?? []).map(v => Dashboard.convertVideo(v)),
};
break;
case "move":
serialised.data = {
from: queueEvent.data?.from,
to: queueEvent.data?.to,
track: Dashboard.convertVideo(queueEvent.data?.track),
};
break;
case "shuffle":
serialised.data = (queueEvent.data ?? []).map(v => Dashboard.convertVideo(v));
break;
case "update":
serialised.data = {
current: Dashboard.convertVideo(queueEvent.data?.current),
old: Dashboard.convertVideo(queueEvent.data?.old),
loop: queueEvent.data?.loop,
};
break;
default:
serialised.data = queueEvent.data;
break;
}
emit("queue", serialised);
this.dashboard.playerUpdate({ type: "queue" }, player);
});
return player;
}
/**
* Translate a locale key using the message's guild locale.
* @param {Object} message
* @param {string} key
* @param {Object} [replacements={}]
* @returns {string}
*/
_t(message, key, replacements = {}) {
if (!this.locale) return key;
const guildId = getMessageGuildId(message);
return this.locale.translate(guildId, key, replacements);
}
/**
* Attempt to detect the voice channel a user is currently in.
* @param {Message} message
* @returns {string|null} Voice channel ID or null
*/
checkVoiceChannels(message) {
if (!message) return null;
const userId = message.author?.id ?? message.message?.author?.id;
const guildId = getMessageGuildId(message);
if (!userId) return null;
if (!guildId) return null;
const cleanGuild = cleanId(guildId);
const seedObserved = (channelId) => {
const cleanChannelId = cleanId(channelId);
if (!cleanChannelId) return null;
const cache = this.voiceCache ?? this.observedVoiceUsers;
if (cache && !cache.has(userId, cleanGuild)) {
cache.set(userId, { channelId: cleanChannelId, guildId: cleanGuild });
}
const loc = cache?.get(userId, cleanGuild);
return cleanId(loc?.channelId) || cleanChannelId;
};
const cache = this.voiceCache ?? this.observedVoiceUsers;
const observed = cache?.get?.(userId, cleanGuild);
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 (_) {}
try {
const vm = getVoiceManager(this.commands.client);
if (vm?.voiceStates) {
const guildVoiceMap = vm.voiceStates.get(cleanGuild) ?? vm.voiceStates.get(guildId);
if (guildVoiceMap && typeof guildVoiceMap.get === "function") {
const vmChannelId = guildVoiceMap.get(userId);
const seeded = seedObserved(vmChannelId);
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?.get?.(guildId) ??
this.commands.client?.guilds?.get?.(cleanGuild);
const voiceStates = guild?.voice_states ?? 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) ??
null;
return getPlayerGuildId(player, fallbackChannel) === cleanGuild && !player?._destroyed;
});
if (liveGuildPlayers.length === 1) {
return getPlayerChannelId(liveGuildPlayers[0][1], liveGuildPlayers[0][0]) || cleanId(liveGuildPlayers[0][0]);
}
if (liveGuildPlayers.length > 1) {
for (const [mapKey, player] of liveGuildPlayers) {
try {
const room = player?.connection?.room;
if (!room?.isConnected || !room.remoteParticipants) continue;
for (const [, participant] of room.remoteParticipants) {
const pId = participant?.identity ?? participant?.sid;
if (pId === userId) {
const found = getPlayerChannelId(player, mapKey) || cleanId(mapKey);
seedObserved(found);
return found;
}
}
} catch (_) {}
}
}
return null;
}
/**
* Get or create a player for the user's voice channel.
* @param {Message} message
* @param {boolean} [promptJoin=true]
* @param {boolean} [verifyUser=true]
* @param {boolean} [shouldJoin=false]
* @returns {Promise<Player|null>}
*/
async getPlayer(message, promptJoin = true, verifyUser = true, shouldJoin = false) {
const guildId = getMessageGuildId(message);
const cleanGuildId = cleanId(guildId);
const userChannelId = this.checkVoiceChannels(message);
const cleanUserChannelId = cleanId(userChannelId);
if (cleanUserChannelId) {
const player = this.playerMap.get(cleanUserChannelId)
?? this.getPlayerByGuildAndChannel(cleanGuildId, cleanUserChannelId);
if (player) {
player.textChannel = message.channel;
try {
const textChannelId = message?.channel?.id ?? message?.channel?.channel?.id ?? null;
if (guildId && textChannelId) {
this.settings.getServer(guildId)?.set("announcementChannelId", textChannelId);
}
} catch (_) {}
return player;
}
if (this._pendingJoins.has(cleanUserChannelId)) {
return null;
}
}
const serverPlayers = cleanGuildId
? this.getGuildPlayers(cleanGuildId)
: [];
if (serverPlayers.length > 0) {
const channelList = serverPlayers.map(([chId]) => `<#${chId}>`).join(" or ");
if (!userChannelId) {
if (!verifyUser) {
const first = serverPlayers[0];
first[1].textChannel = message.channel;
return first[1];
}
const msgUserId = message?.author?.id ?? message?.message?.author?.id;
if (msgUserId && userMightBeInHiddenVoice(this.commands?.client, msgUserId, guildId)) {
message.reply(mkEmbed(this._t(message, "responses._common.hiddenVoiceChannel")));
} else {
message.reply(mkEmbed(this._t(message, "responses._common.noVoiceStrict")));
}
return null;
}
const match = serverPlayers.find(([, player]) =>
getPlayerChannelId(player) === cleanUserChannelId
);
if (match) {
match[1].textChannel = message.channel;
try {
const textChannelId = message?.channel?.id ?? message?.channel?.channel?.id ?? null;
if (cleanGuildId && textChannelId) {
this.settings.getServer(cleanGuildId)?.set("announcementChannelId", textChannelId);
}
} catch (_) {}
return match[1];
}
if (shouldJoin) {
return new Promise((resolve) => {
this.initPlayer(message, userChannelId, (p) => resolve(p));
});
}
const prefix = (() => {
try {
return this.settings.getServer(guildId)?.get("prefix") ?? "%";
} catch (_) { return "%"; }
})();
message.reply(mkEmbed(this._t(message, "responses._common.alreadyInChannel", { channels: channelList, prefix })));
return null;
}
if (!userChannelId) {
if (shouldJoin) {
return this.promptVC(message);
}
const msgUserId = message?.author?.id ?? message?.message?.author?.id;
if (msgUserId && userMightBeInHiddenVoice(this.commands?.client, msgUserId, guildId)) {
message.reply(mkEmbed(this._t(message, "responses._common.hiddenVoiceChannel")));
} else {
message.reply(mkEmbed(this._t(message, "responses._common.noVoiceChannel")));
}
return null;
}
if (shouldJoin) {
return new Promise((resolve) => {
this.initPlayer(message, userChannelId, (p) => resolve(p));
});
}
return null;
}
/**
* Prompt the user to select a voice channel.
* @param {Message} msg
* @returns {Promise<string|false>}
*/
async promptVC(msg) {
const autoDetected = this.checkVoiceChannels(msg);
if (autoDetected) {
return new Promise(resolve => this.initPlayer(msg, autoDetected, (p) => resolve(p)));
}
const guildId = getMessageGuildId(msg);
const cleanGuildId = cleanId(guildId);
const allChannels = cleanGuildId
? [...(this.commands.client?.channels?.values?.() ?? [])]
.filter(c => {
const channelGuildId = cleanId(c.guildId ?? c.guild?.id ?? c.server_id ?? c.serverId);
const isVoice = c.type === 2;
return channelGuildId === cleanGuildId && isVoice;
})
: [];
const reactions = ["1️⃣","2️⃣","3️⃣","4️⃣","5️⃣","6️⃣","7️⃣","8️⃣","9️⃣"];
const channelArr = allChannels.slice(0, 9);
let channelSelection = "";
if (channelArr.length > 0) {
channelSelection = this._t(msg, "responses._common.voiceSelectionPrompt") + "\n\n";
channelArr.forEach((c, i) => { channelSelection += `${i + 1}. <#${c._id ?? c.id}>\n`; });
}
const hint = this._t(msg, "responses._common.voiceSelectionHint");
const selectionMsg = await msg.reply(mkEmbed(
(channelSelection ? channelSelection + "\n**..or** " + hint : "Please " + hint)
));
return new Promise(resolve => {
let unsubscribeReactions;
let unsubscribeMessages;
const promptUser = msg.author ?? msg.message?.author ?? null;
const cleanup = () => {
unsubscribeMessages?.();
unsubscribeReactions?.();
};
const timeout = setTimeout(() => {
cleanup();
msg.reply(mkEmbed(this._t(msg, "responses._common.voiceSelectionTimedOut")));
resolve(false);
}, 30_000);
if (typeof selectionMsg?.onReaction === "function" && channelArr.length > 0) {
unsubscribeReactions = selectionMsg.onReaction(
reactions.slice(0, channelArr.length),
(e) => {
const idx = reactions.indexOf(e.emoji_id ?? e.emoji?.id ?? e.emoji);
const channel = channelArr[idx];
if (!channel) return;
clearTimeout(timeout);
cleanup();
const cid = channel._id ?? channel.id;
this.initPlayer(msg, cid, (p) => resolve(p));
},
promptUser
);
}
unsubscribeMessages = msg.channel.onMessageUser((m) => {
const content = m.content?.toLowerCase() ?? "";
if (content === "x") {
clearTimeout(timeout);
cleanup();
m.reply(mkEmbed(this._t(m, "voice.join.cancelled")));
resolve(false);
return;
}
if (!this.commands.validateInput("voiceChannel", m.content, m)) {
m.reply(mkEmbed(this._t(m, "responses._common.voiceSelectionInvalid")));
return;
}
const channel = this.commands.formatInput("voiceChannel", m.content, m);
clearTimeout(timeout);
cleanup();
this.initPlayer(m, channel, (p) => resolve(p));
}, promptUser);
});
}
/**
* Make the player leave its current voice channel.
* @param {Message} msg
* @param {string} [cid]
*/
async leave(msg, cid) {
if (!cid) {
const guildId = getMessageGuildId(msg);
if (guildId) {
const guildPlayers = this.getGuildPlayers(cleanId(guildId));
if (guildPlayers.length > 0) {
const [, firstPlayer] = guildPlayers[0];
cid = getPlayerChannelId(firstPlayer, guildPlayers[0][0]) || guildPlayers[0][0];
}
}
}
const cleanChannelId = cleanId(cid);
const player = cleanChannelId
? this.playerMap.get(cleanChannelId) ??
this.getPlayerByChannelId(cleanChannelId)
: null;
if (!player) return msg.reply(mkEmbed(this._t(msg, "responses._common.notInVoice")));
const activeChannelId = getPlayerChannelId(player, cleanChannelId) || cleanChannelId;
this.playerMap.delete(activeChannelId);
this._unindexPlayer(player._guildId, activeChannelId);
const pendingScrobble = this._pendingScrobbleTimers.get(activeChannelId);
if (pendingScrobble) { clearTimeout(pendingScrobble.timer); this._pendingScrobbleTimers.delete(activeChannelId); }
if (activeChannelId !== cleanChannelId) this.playerMap.delete(cleanChannelId);
try {
await player.leave();
} catch (e) {
logger.warn("[PlayerManager] leave() error (non-fatal):", e.message);
}
player.destroy();
await msg.reply(mkEmbed(this._t(msg, "responses._common.successfullyLeft")));
}
/**
* Create and register a new Player for the given voice channel.
* @param {Message} message
* @param {string} cid - Voice channel ID
* @param {Function} [cb]
*/
initPlayer(message, cid, cb = () => {}) {
const channel = this.commands.client?.channels?.get(cid);
if (!channel) {
return message.reply(mkEmbed(
this._t(message, "responses.join.channelNotFound", { channel: cid })
));
}
const isVoice = channel.type === 2;
if (!isVoice) {
return message.reply(mkEmbed(this._t(message, "responses._common.voiceChannelRequired")));
}
if (!botHasVoicePermissions(this.commands?.client, cid)) {
return message.reply(mkEmbed(
this._t(message, "responses.join.joinFailedPerms", { channel: `<#${cleanId(cid)}>` })
));
}
const cleanChannelId = cleanId(cid);
const existing = this.playerMap.get(cleanChannelId)
?? this.getPlayerByChannelId(cleanChannelId);
if (existing) {
existing.textChannel = message.channel;
cb(existing);
return message.reply(mkEmbed(this._t(message, "responses.join.alreadyJoined", { channel: cid })));
}
if (this._pendingJoins.has(cleanChannelId)) {
return message.reply(mkEmbed(this._t(message, "responses.join.joining")));
}
this._pendingJoins.add(cleanChannelId);
const player = new Player(this.config.token, {
...this.playerConfig,
client: this.commands.client,
config: this.config,
nodelink: this.config.nodelink,
moonlink: this.playerConfig?.moonlink ?? null,
revoice: this.playerConfig?.revoice ?? null,
settingsMgr: this.settings,
observedVoiceUsers: this.observedVoiceUsers ?? null,
voiceCache: this.voiceCache ?? null,
locale: this.locale ?? null,
trackOptions: this.trackOptions ?? null,
});
player.textChannel = message.channel;
this.setupEvents(player, {
channelId: cleanChannelId,
guildId: cleanId(channel.guildId ?? getMessageGuildId(message)),
});
player.on("autoleave", () => {
const activeChannelId = getPlayerChannelId(player, cleanChannelId) || cleanChannelId;
const homeChannelId = cleanId(player._home247Channel) || activeChannelId;
const ch = player.textChannel;
const guildId = cleanId(player._guildId ?? ch?.guildId ?? ch?.guild?.id ?? getMessageGuildId({ channel: ch }));
const raw247 = (() => {
try { return this.settings.getServer(guildId)?.get("stay_247"); } catch (_) { return null; }
})();
const isIn247List = (() => {
if (!raw247 || raw247 === "none") return false;
const channels = Array.isArray(raw247)
? raw247.map(id => String(id).replace(/\D/g, "")).filter(Boolean)
: [String(raw247).replace(/\D/g, "")].filter(Boolean);
return channels.includes(homeChannelId) || channels.includes(activeChannelId);
})();
const matchChannel = isIn247List
? (channels247list => channels247list.includes(homeChannelId) ? homeChannelId : activeChannelId)(
Array.isArray(raw247) ? raw247.map(id => String(id).replace(/\D/g, "")) : [String(raw247).replace(/\D/g, "")]
)
: null;
const mode247 = matchChannel
? get247ChannelMode(this.settings.getServer(guildId), matchChannel)
: "off";
if (mode247 === "auto" || mode247 === "on") {
logger.inactivity(`[PlayerManager] autoleave suppressed for 24/7 ${mode247} channel ${activeChannelId} (guild ${guildId})`);
return;
}
if (player._hasHumansInChannel()) {
logger.inactivity(`[PlayerManager] autoleave suppressed — humans still in channel ${activeChannelId} (guild ${guildId})`);
return;
}
if (player.queue?.getCurrent() || !player.queue?.isEmpty()) {
logger.inactivity(`[PlayerManager] autoleave suppressed — queue has songs in channel ${activeChannelId} (guild ${guildId})`);
return;
}
this.playerMap.delete(activeChannelId);
this._unindexPlayer(player._guildId, activeChannelId);
const pendingScrobble = this._pendingScrobbleTimers.get(activeChannelId);
if (pendingScrobble) { clearTimeout(pendingScrobble.timer); this._pendingScrobbleTimers.delete(activeChannelId); }
if (activeChannelId !== cleanChannelId) this.playerMap.delete(cleanChannelId);
if (homeChannelId !== activeChannelId) this.playerMap.delete(homeChannelId);
player.destroy();
const prefix = (() => {
try { return this.settings.getServer(guildId)?.get("prefix") ?? "%"; } catch (_) { return "%"; }
})();
let desc;
if (mode247 === "on") {
desc = this.locale?.translate(guildId, "responses.join.autoLeave247On", { channel: `<#${activeChannelId}>` })
?? `Left channel <#${activeChannelId}> because of inactivity.\nI'll rejoin automatically when the bot restarts (%247 on mode).`;
} else if (mode247 === "auto") {
desc = this.locale?.translate(guildId, "responses.join.autoLeave247Auto", { channel: `<#${activeChannelId}>` })
?? `Left channel <#${activeChannelId}> — reconnecting automatically (%247 auto mode)...`;
} else {
desc = this.locale?.translate(guildId, "responses.join.autoLeaveInactive247", { channel: `<#${activeChannelId}>`, prefix })
?? `Left channel <#${activeChannelId}> because of inactivity.\nIf you want me to stay in voice, use \`${prefix}247 on/auto\``;
}
if (typeof ch?.send === "function") {
ch.send(mkEmbed(desc)).catch(err => {
if (err.code === 'MISSING_PERMISSIONS' || err.statusCode === 403) {
logger.warn(`[PlayerManager] Cannot send autoleave message in channel ${ch.id} — missing permissions`);
}
});
}
});
player.on("leave", () => {});
player.on("message", (m) => {
let ch = player.textChannel;
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;
if (!ch || typeof ch.send !== "function") {
try {
const serverSettings = this.settings.getServer(guildId);
const savedAnnChId = serverSettings?.get?.("announcementChannelId");
if (savedAnnChId) {
ch = this.commands?.client?.channels?.get?.(String(savedAnnChId).replace(/\D/g, "")) ?? null;
}
} catch (_) {}
}
if (!ch || typeof ch.send !== "function") {
try {
const guild = this.commands?.client?.guilds?.get?.(guildId);
if (guild?.systemChannelId) {
ch = guild.channels?.get?.(guild.systemChannelId) ?? null;
}
} catch (_) {}
}
if (!ch || typeof ch.send !== "function") {
try {
const guild = this.commands?.client?.guilds?.get?.(guildId);
if (guild?.channels) {
for (const c of (guild.channels.values?.() ?? [])) {
if (c.isTextBased?.() || c.type === 0 || c.type === "GUILD_TEXT") {
ch = c;
break;
}
}
}
} catch (_) {}
}
if (!ch || typeof ch.send !== "function") return;
if (!player.textChannel) player.textChannel = ch;
ch.send(typeof m === "object" && Array.isArray(m.embeds) ? m : mkEmbed(m)).catch(err => {
if (err.code === 'MISSING_PERMISSIONS' || err.statusCode === 403) {
logger.warn(`[PlayerManager] Cannot send player message in channel ${ch.id} — missing permissions`);
}
});
});
(async () => {
const statusMsg = await message.reply(mkEmbed(this._t(message, "responses.join.joining")));
try {
await player.join(cid);
this.playerMap.set(cleanChannelId, player);
this._indexPlayer(channel.guildId ?? getMessageGuildId(message), cleanChannelId);
this._pendingJoins.delete(cleanChannelId);
await statusMsg.edit(mkEmbed(this._t(message, "responses.join.joined", { channel: cid })));
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);
}
}
cb(player);
} catch (err) {
this._pendingJoins.delete(cleanChannelId);
const errCode = sanitizeJoinError(err, this.commands?.client, cleanChannelId);
let errorMsg;
if (errCode === "SESSION_RACE") {
logger.warn(`[PlayerManager] Stale voice session detected for channel ${cleanChannelId}, retrying in 2s...`);
try {
await new Promise(r => setTimeout(r, 2_000));
if (player._revoice && player._channelId) {
try { player._revoice._leaveGateway(player._channelId, player._guildId ?? player._resolveGuildId()); } catch (_) {}
try { player._revoice.deleteConnection(player._channelId); } catch (_) {}
}
await player.join(cid);
this.playerMap.set(cleanChannelId, player);
this._indexPlayer(channel.guildId ?? getMessageGuildId(message), cleanChannelId);
await statusMsg.edit(mkEmbed(this._t(message, "responses.join.joined", { channel: cid })));
const retryGuildId = cleanId(channel.guildId ?? getMessageGuildId(message));
if (retryGuildId) {
const savedVol = this.settings.getServer(retryGuildId)?.get("volume");
if (savedVol !== undefined && savedVol !== null) {
const vol = Number(savedVol);
if (!isNaN(vol)) player.setVolume(vol / 100);
}
}
cb(player);
return;
} catch (retryErr) {
logger.warn(`[PlayerManager] Retry also failed for channel ${cleanChannelId}: ${retryErr.message}`);
errorMsg = this._t(message, "responses.join.joinFailedGeneric");
}
} else if (errCode === "PERMISSION") {
errorMsg = this._t(message, "responses.join.joinFailedPerms", { channel: `<#${cleanChannelId}>` });
} else if (errCode === "NOT_FOUND") {
errorMsg = this._t(message, "responses.join.joinFailedNotFound");
} else if (errCode === "TIMEOUT") {
errorMsg = this._t(message, "responses.join.joinFailed");
} else {
errorMsg = this._t(message, "responses.join.joinFailed", { error: err.message });
}
await statusMsg.edit(mkEmbed(errorMsg)).catch(() => {});
this.playerMap.delete(cleanChannelId);
this._unindexPlayer(channel.guildId ?? getMessageGuildId(message), cleanChannelId);
player.destroy();
cb(null);
}
})();
}
/**
* Handle the "startplay" event for Last.fm: send now-playing notification
* to all linked users in the voice channel, and schedule a deferred scrobble
* after the track has played for long enough (50% duration or 4 min).
*
* @param {Player} player
* @param {Object} song - Track object
*/
_handleLastFmStartPlay(player, song) {
const lastfm = this._lastfm;
if (!lastfm?.enabled) return;
const guildId = cleanId(player._guildId);
if (!guildId) return;
const channelId = getPlayerChannelId(player);
const humanUserIds = [];
if (this.voiceCache) {
const users = this.voiceCache.getHumansInChannel(guildId, channelId);
humanUserIds.push(...users);
} else if (this.observedVoiceUsers) {
for (const [uid, info] of this.observedVoiceUsers) {
if (cleanId(info.guildId) === guildId && cleanId(info.channelId) === channelId) {
humanUserIds.push(uid);
}
}
}
const guild = player.client?.guilds?.get(guildId);
if (guild) {
const voiceStates = guild.voice_states ?? guild.voiceStates ?? null;
if (voiceStates) {
const entries = Array.isArray(voiceStates)
? voiceStates
: typeof voiceStates.values === "function"
? [...voiceStates.values()]
: Object.values(voiceStates ?? {});
for (const state of entries) {
const uid = state?.userId ?? state?.user_id;
const chId = cleanId(state?.channelId ?? state?.channel_id);
if (uid && chId === channelId) {
const member = guild.members?.get?.(uid);
if (member?.user?.bot) continue;
if (!humanUserIds.includes(uid)) humanUserIds.push(uid);
}
}
}
}
const startedAtMs = player.startedPlaying;
const pendingKey = channelId;
const existing = this._pendingScrobbleTimers.get(pendingKey);
if (existing) {
clearTimeout(existing.timer);
this._pendingScrobbleTimers.delete(pendingKey);
}
for (const userId of humanUserIds) {
lastfm.updateNowPlaying(userId, song).catch(() => {});
}
const durationMs = (() => {
const d = song.duration;
if (!d) return null;
if (typeof d === "object" && d.seconds) return d.seconds * 1000;
if (typeof d === "number") return d;
return null;
})();
if (durationMs && durationMs >= 30_000) {
const thresholdMs = Math.min(
durationMs * lastfm.scrobbleThreshold,
lastfm.scrobbleMinMs
);
if (thresholdMs <= 600_000) {
const timer = setTimeout(() => {
this._pendingScrobbleTimers.delete(pendingKey);
const current = player.queue?.getCurrent();
if (!current || player._destroyed || player.leaving) return;
if (current.title !== song.title || current.url !== song.url) return;
if (player._paused) return;
const playedMs = Date.now() - (player.startedPlaying ?? startedAtMs ?? Date.now());
if (lastfm.shouldScrobble(song, playedMs)) {
for (const userId of humanUserIds) {
lastfm.scrobble(userId, song, startedAtMs).catch(() => {});
}
}
}, thresholdMs);
this._pendingScrobbleTimers.set(pendingKey, { timer, songUrl: song.url, startedAtMs });
}
}
}
}