UNPKG

magmastream

Version:

A user-friendly Lavalink client designed for NodeJS.

85 lines (84 loc) 2.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SeyfertManager = void 0; const Manager_1 = require("../structures/Manager"); const MagmastreamError_1 = require("../structures/MagmastreamError"); const Enums_1 = require("../structures/Enums"); const GUILD_VOICE_STATES_INTENT = 128; function calculateShardId(guildId, totalShards) { return Number((BigInt(guildId) >> BigInt(22)) % BigInt(totalShards)); } /** * Seyfert wrapper for Magmastream. * * @note This wrapper does require the manual implementation of the "raw" and "ready" events, to call the `updateVoiceState` and `init` methods respectively. * * @example * ```typescript * const client = new Client(); * const manager = new SeyfertManager(client, options); * * client.events.values.RAW = { * data: { name: "raw" }, * run: async (data) => { * await manager.updateVoiceState(data); * } * } * * client.events.values.READY = { * data: { name: "ready" }, * run: async (user, client) => { * await manager.init({ clientId: client.botId }); * } * } * ``` */ class SeyfertManager extends Manager_1.Manager { client; constructor(client, options) { super(options, true); this.client = client; this.client .getRC() .then((rc) => { if (!(rc.intents & GUILD_VOICE_STATES_INTENT)) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.INTENT_MISSING, message: "[Custom Wrapper] Your Seyfert client must have the GuildVoiceStates intent enabled.", }); } }) .catch((error) => { queueMicrotask(() => { throw error; }); }); } send(packet) { if ("gateway" in this.client) { this.client.gateway.send(calculateShardId(packet.d.guild_id, this.client.gateway.totalShards), packet); } else { this.client.shards.get(this.client.calculateShardId(packet.d.guild_id))?.send(true, packet); } } async resolveUser(user) { const id = typeof user === "string" ? user : String(user.id); const cached = this.client.cache.users?.get(id); if (cached) return cached; try { return await this.client.users.fetch(id); } catch { return { id, username: typeof user === "string" ? undefined : user.username }; } } resolveGuild(guildId) { const cached = this.client.cache.guilds?.get(guildId); if (cached) return cached; return null; } } exports.SeyfertManager = SeyfertManager;