UNPKG

magmastream

Version:

A user-friendly Lavalink client designed for NodeJS.

73 lines (72 loc) 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DiscordJSManager = void 0; const Manager_1 = require("../structures/Manager"); const module_1 = require("module"); const MagmastreamError_1 = require("../structures/MagmastreamError"); const Enums_1 = require("../structures/Enums"); const GUILD_VOICE_STATES_INTENT = 128; const moduleRequire = (0, module_1.createRequire)(__filename); /** * Discord.js wrapper for Magmastream. */ class DiscordJSManager extends Manager_1.Manager { client; constructor(client, options) { super(options, true); this.client = client; const { version: djsVersion } = moduleRequire("discord.js"); const [major, minor] = djsVersion.split(".").map(Number); if (!this.client.options.intents.has(GUILD_VOICE_STATES_INTENT)) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.INTENT_MISSING, message: "[Custom Wrapper] Your Discord.js client must have the GuildVoiceStates intent enabled.", }); } const attachReadyHandler = () => { let handled = false; const handler = () => { if (handled) return; handled = true; if (!this.options.clientId) this.options.clientId = this.client.user.id; }; if (major > 14 || (major === 14 && minor >= 22)) { this.client.once("clientReady", handler); } else { this.client.once("ready", handler); } }; attachReadyHandler(); client.on("raw", async (data) => { await this.updateVoiceState(data); }); } send(packet) { const guild = this.client.guilds.cache.get(packet.d.guild_id); if (guild) guild.shard.send(packet); } async resolveUser(user) { const id = typeof user === "string" ? user : String(user.id); const cached = this.client.users.cache.get(id); if (cached) return cached; try { const fetched = await this.client.users.fetch(id); return fetched; } catch { return { id, username: typeof user === "string" ? undefined : user.username }; } } resolveGuild(guildId) { const cached = this.client.guilds.cache.get(guildId); if (cached) return cached; return null; } } exports.DiscordJSManager = DiscordJSManager;