UNPKG

discord-together

Version:

Play games or watch YouTube videos together on Discord! More than 23 games available!

53 lines 2.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DiscordTogether = void 0; exports.createDiscordTogether = createDiscordTogether; const config_1 = require("./applications/config"); class DiscordTogether { constructor(client, applications = config_1.DefaultApplicationsConfig) { this.client = client; this.applications = applications; } async createTogetherCode(channelId, application) { const appId = this.applications[application]; try { const invite = await this.fetchApplicationInviteCode(appId, channelId); return { code: invite.code, invite: `https://discord.com/invite/${invite.code}` }; } catch (err) { throw new Error(`Failed to create Together code: ${err}`); } } async fetchApplicationInviteCode(appId, channelId) { if (!this.client.token) { throw new Error('Discord token is not provided in the Client instance'); } const response = await fetch(`https://discord.com/api/v10/channels/${channelId}/invites`, { method: 'POST', body: JSON.stringify({ max_age: 86400, max_uses: 0, target_application_id: appId, target_type: 2, temporary: false, validate: null, }), headers: { Authorization: `Bot ${this.client.token}`, 'Content-Type': 'application/json', }, }); const res = await response.json(); if (res.error || !res.code) throw new Error('An error occurred while retrieving data!'); if (Number(res.code) === 50013) console.warn('Your bot lacks permissions to perform that action'); return res; } } exports.DiscordTogether = DiscordTogether; function createDiscordTogether(client, applications = config_1.DefaultApplicationsConfig) { const instance = new DiscordTogether(client, applications); return instance.createTogetherCode; } //# sourceMappingURL=discord-together.js.map