UNPKG

@confis/discordapiwrapper

Version:

A fast and lightweight discord api wrapper.

504 lines (494 loc) 21.8 kB
var assert = require('assert'); require('axios'); require('ws'); require('console'); /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; /** Text input styles enum */ var TextInputStyles; (function (TextInputStyles) { TextInputStyles[TextInputStyles["SHORT"] = 1] = "SHORT"; TextInputStyles[TextInputStyles["PARAGRAPH"] = 2] = "PARAGRAPH"; })(TextInputStyles || (TextInputStyles = {})); /** Enum for types of webhook */ var WebhookTypes; (function (WebhookTypes) { WebhookTypes[WebhookTypes["INCOMING"] = 1] = "INCOMING"; WebhookTypes[WebhookTypes["CHANNEL_FOLLOWER"] = 2] = "CHANNEL_FOLLOWER"; WebhookTypes[WebhookTypes["APPLICATION"] = 3] = "APPLICATION"; })(WebhookTypes || (WebhookTypes = {})); /** Enum for types of interaction contexts */ var InteractionContextTypes; (function (InteractionContextTypes) { InteractionContextTypes[InteractionContextTypes["GUILD"] = 0] = "GUILD"; InteractionContextTypes[InteractionContextTypes["BOT_DM"] = 1] = "BOT_DM"; InteractionContextTypes[InteractionContextTypes["PRIVATE_CHANNEL"] = 2] = "PRIVATE_CHANNEL"; })(InteractionContextTypes || (InteractionContextTypes = {})); /** Enum for types of interaction integration */ var InteractionIntegrationTypes; (function (InteractionIntegrationTypes) { InteractionIntegrationTypes[InteractionIntegrationTypes["GUILD_INSTALL"] = 0] = "GUILD_INSTALL"; InteractionIntegrationTypes[InteractionIntegrationTypes["USER_INSTALL"] = 1] = "USER_INSTALL"; })(InteractionIntegrationTypes || (InteractionIntegrationTypes = {})); /** Enum for types of message */ var APIMessageTypes; (function (APIMessageTypes) { APIMessageTypes[APIMessageTypes["DEFAULT"] = 0] = "DEFAULT"; APIMessageTypes[APIMessageTypes["RECIPIENT_ADD"] = 1] = "RECIPIENT_ADD"; APIMessageTypes[APIMessageTypes["RECIPIENT_REMOVE"] = 2] = "RECIPIENT_REMOVE"; APIMessageTypes[APIMessageTypes["CALL"] = 3] = "CALL"; APIMessageTypes[APIMessageTypes["CHANNEL_NAME_CHANGE"] = 4] = "CHANNEL_NAME_CHANGE"; APIMessageTypes[APIMessageTypes["CHANNEL_ICON_CHANGE"] = 5] = "CHANNEL_ICON_CHANGE"; APIMessageTypes[APIMessageTypes["CHANNEL_PINNED_MESSAGE"] = 6] = "CHANNEL_PINNED_MESSAGE"; APIMessageTypes[APIMessageTypes["USER_JOIN"] = 7] = "USER_JOIN"; APIMessageTypes[APIMessageTypes["GUILD_BOOST"] = 8] = "GUILD_BOOST"; APIMessageTypes[APIMessageTypes["GUILD_BOOST_TIER_1"] = 9] = "GUILD_BOOST_TIER_1"; APIMessageTypes[APIMessageTypes["GUILD_BOOST_TIER_2"] = 10] = "GUILD_BOOST_TIER_2"; APIMessageTypes[APIMessageTypes["GUILD_BOOST_TIER_3"] = 11] = "GUILD_BOOST_TIER_3"; APIMessageTypes[APIMessageTypes["CHANNEL_FOLLOW_ADD"] = 12] = "CHANNEL_FOLLOW_ADD"; APIMessageTypes[APIMessageTypes["GUILD_DISCOVERY_DISQUALIFIED"] = 14] = "GUILD_DISCOVERY_DISQUALIFIED"; APIMessageTypes[APIMessageTypes["GUILD_DISCOVERY_REQUALIFIED"] = 15] = "GUILD_DISCOVERY_REQUALIFIED"; APIMessageTypes[APIMessageTypes["GUILD_DISCOVERY_GRACE_PERIOD_INITIAL_WARNING"] = 16] = "GUILD_DISCOVERY_GRACE_PERIOD_INITIAL_WARNING"; APIMessageTypes[APIMessageTypes["GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING"] = 17] = "GUILD_DISCOVERY_GRACE_PERIOD_FINAL_WARNING"; APIMessageTypes[APIMessageTypes["THREAD_CREATED"] = 18] = "THREAD_CREATED"; APIMessageTypes[APIMessageTypes["REPLY"] = 19] = "REPLY"; APIMessageTypes[APIMessageTypes["CHAT_INPUT_COMMAND"] = 20] = "CHAT_INPUT_COMMAND"; APIMessageTypes[APIMessageTypes["THREAD_STARTER_MESSAGE"] = 21] = "THREAD_STARTER_MESSAGE"; APIMessageTypes[APIMessageTypes["GUILD_INVITE_REMINDER"] = 22] = "GUILD_INVITE_REMINDER"; APIMessageTypes[APIMessageTypes["CONTEXT_MENU_COMMAND"] = 23] = "CONTEXT_MENU_COMMAND"; APIMessageTypes[APIMessageTypes["AUTO_MODERATION_ACTION"] = 24] = "AUTO_MODERATION_ACTION"; APIMessageTypes[APIMessageTypes["ROLE_SUBSCRIPTION_PURCHASE"] = 25] = "ROLE_SUBSCRIPTION_PURCHASE"; APIMessageTypes[APIMessageTypes["INTERACTION_PREMIUM_UPSELL"] = 26] = "INTERACTION_PREMIUM_UPSELL"; APIMessageTypes[APIMessageTypes["STAGE_START"] = 27] = "STAGE_START"; APIMessageTypes[APIMessageTypes["STAGE_END"] = 28] = "STAGE_END"; APIMessageTypes[APIMessageTypes["STAGE_SPEAKER"] = 29] = "STAGE_SPEAKER"; APIMessageTypes[APIMessageTypes["STAGE_TOPIC"] = 31] = "STAGE_TOPIC"; APIMessageTypes[APIMessageTypes["GUILD_APPLICATION_PREMIUM_SUBSCRIPTION"] = 32] = "GUILD_APPLICATION_PREMIUM_SUBSCRIPTION"; APIMessageTypes[APIMessageTypes["GUILD_INCIDENT_ALERT_MODE_ENABLED"] = 36] = "GUILD_INCIDENT_ALERT_MODE_ENABLED"; APIMessageTypes[APIMessageTypes["GUILD_INCIDENT_ALERT_MODE_DISABLED"] = 37] = "GUILD_INCIDENT_ALERT_MODE_DISABLED"; APIMessageTypes[APIMessageTypes["GUILD_INCIDENT_REPORT_RAID"] = 38] = "GUILD_INCIDENT_REPORT_RAID"; APIMessageTypes[APIMessageTypes["GUILD_INCIDENT_REPORT_FALSE_ALARM"] = 39] = "GUILD_INCIDENT_REPORT_FALSE_ALARM"; APIMessageTypes[APIMessageTypes["PURCHASE_NOTIFICATION"] = 44] = "PURCHASE_NOTIFICATION"; APIMessageTypes[APIMessageTypes["POLL_RESULT"] = 46] = "POLL_RESULT"; })(APIMessageTypes || (APIMessageTypes = {})); /** * Enum for types of application commands */ var ApplicationCommandTypes; (function (ApplicationCommandTypes) { ApplicationCommandTypes[ApplicationCommandTypes["CHAT_INPUT"] = 1] = "CHAT_INPUT"; ApplicationCommandTypes[ApplicationCommandTypes["USER"] = 2] = "USER"; ApplicationCommandTypes[ApplicationCommandTypes["MESSAGE"] = 3] = "MESSAGE"; })(ApplicationCommandTypes || (ApplicationCommandTypes = {})); /** * Enum for types of interactions */ var InteractionTypes; (function (InteractionTypes) { InteractionTypes[InteractionTypes["PING"] = 1] = "PING"; InteractionTypes[InteractionTypes["APPLICATION_COMMAND"] = 2] = "APPLICATION_COMMAND"; InteractionTypes[InteractionTypes["MESSAGE_COMPONENT"] = 3] = "MESSAGE_COMPONENT"; InteractionTypes[InteractionTypes["APPLICATION_COMMAND_AUTOCOMPLETE"] = 4] = "APPLICATION_COMMAND_AUTOCOMPLETE"; InteractionTypes[InteractionTypes["MODAL_SUBMIT"] = 5] = "MODAL_SUBMIT"; })(InteractionTypes || (InteractionTypes = {})); /** * Enum for types of components */ var ComponentTypes; (function (ComponentTypes) { ComponentTypes[ComponentTypes["ACTION_ROW"] = 1] = "ACTION_ROW"; ComponentTypes[ComponentTypes["BUTTON"] = 2] = "BUTTON"; ComponentTypes[ComponentTypes["STRING_SELECT"] = 3] = "STRING_SELECT"; ComponentTypes[ComponentTypes["TEXT_INPUT"] = 4] = "TEXT_INPUT"; })(ComponentTypes || (ComponentTypes = {})); /** * Enum for types of button styles */ var ButtonStyles; (function (ButtonStyles) { ButtonStyles[ButtonStyles["PRIMARY"] = 1] = "PRIMARY"; ButtonStyles[ButtonStyles["SECONDARY"] = 2] = "SECONDARY"; ButtonStyles[ButtonStyles["SUCCESS"] = 3] = "SUCCESS"; ButtonStyles[ButtonStyles["DANGER"] = 4] = "DANGER"; ButtonStyles[ButtonStyles["LINK"] = 5] = "LINK"; })(ButtonStyles || (ButtonStyles = {})); /** * Enum for types of activity */ var ActivityTypes; (function (ActivityTypes) { ActivityTypes[ActivityTypes["GAME"] = 0] = "GAME"; ActivityTypes[ActivityTypes["STREAMING"] = 1] = "STREAMING"; ActivityTypes[ActivityTypes["LISTENING"] = 2] = "LISTENING"; ActivityTypes[ActivityTypes["WATCHING"] = 3] = "WATCHING"; ActivityTypes[ActivityTypes["CUSTOM"] = 4] = "CUSTOM"; ActivityTypes[ActivityTypes["COMPETING"] = 5] = "COMPETING"; })(ActivityTypes || (ActivityTypes = {})); /** * Enum for types of application command options */ var ApplicationCommandOptionTypes; (function (ApplicationCommandOptionTypes) { ApplicationCommandOptionTypes[ApplicationCommandOptionTypes["SUB_COMMAND"] = 1] = "SUB_COMMAND"; ApplicationCommandOptionTypes[ApplicationCommandOptionTypes["SUB_COMMAND_GROUP"] = 2] = "SUB_COMMAND_GROUP"; ApplicationCommandOptionTypes[ApplicationCommandOptionTypes["STRING"] = 3] = "STRING"; ApplicationCommandOptionTypes[ApplicationCommandOptionTypes["INTEGER"] = 4] = "INTEGER"; ApplicationCommandOptionTypes[ApplicationCommandOptionTypes["BOOLEAN"] = 5] = "BOOLEAN"; ApplicationCommandOptionTypes[ApplicationCommandOptionTypes["USER"] = 6] = "USER"; ApplicationCommandOptionTypes[ApplicationCommandOptionTypes["CHANNEL"] = 7] = "CHANNEL"; ApplicationCommandOptionTypes[ApplicationCommandOptionTypes["ROLE"] = 8] = "ROLE"; ApplicationCommandOptionTypes[ApplicationCommandOptionTypes["MENTIONABLE"] = 9] = "MENTIONABLE"; ApplicationCommandOptionTypes[ApplicationCommandOptionTypes["NUMBER"] = 10] = "NUMBER"; ApplicationCommandOptionTypes[ApplicationCommandOptionTypes["ATTACHMENT"] = 11] = "ATTACHMENT"; })(ApplicationCommandOptionTypes || (ApplicationCommandOptionTypes = {})); /** * Enum for types of channels */ var ChannelTypes; (function (ChannelTypes) { ChannelTypes[ChannelTypes["TEXT"] = 0] = "TEXT"; ChannelTypes[ChannelTypes["DM"] = 1] = "DM"; ChannelTypes[ChannelTypes["CATEGORY"] = 4] = "CATEGORY"; })(ChannelTypes || (ChannelTypes = {})); var OverwriteObjectTypes; (function (OverwriteObjectTypes) { OverwriteObjectTypes[OverwriteObjectTypes["ROLE"] = 0] = "ROLE"; OverwriteObjectTypes[OverwriteObjectTypes["MEMBER"] = 1] = "MEMBER"; })(OverwriteObjectTypes || (OverwriteObjectTypes = {})); /** Enum of types of messages */ var MessageTypes; (function (MessageTypes) { MessageTypes[MessageTypes["DEFAULT"] = 0] = "DEFAULT"; MessageTypes[MessageTypes["USER_JOIN"] = 7] = "USER_JOIN"; MessageTypes[MessageTypes["REPLY"] = 19] = "REPLY"; })(MessageTypes || (MessageTypes = {})); /** Bot Intents */ var Intents; (function (Intents) { Intents[Intents["GUILDS"] = 1] = "GUILDS"; Intents[Intents["GUILD_MEMBERS"] = 2] = "GUILD_MEMBERS"; Intents[Intents["GUILD_BANS"] = 4] = "GUILD_BANS"; Intents[Intents["GUILD_EMOJIS_AND_STICKERS"] = 8] = "GUILD_EMOJIS_AND_STICKERS"; Intents[Intents["GUILD_INTEGRATIONS"] = 16] = "GUILD_INTEGRATIONS"; Intents[Intents["GUILD_WEBHOOKS"] = 32] = "GUILD_WEBHOOKS"; Intents[Intents["GUILD_INVITES"] = 64] = "GUILD_INVITES"; Intents[Intents["GUILD_VOICE_STATES"] = 128] = "GUILD_VOICE_STATES"; Intents[Intents["GUILD_PRESENCES"] = 256] = "GUILD_PRESENCES"; Intents[Intents["GUILD_MESSAGES"] = 512] = "GUILD_MESSAGES"; Intents[Intents["GUILD_MESSAGE_REACTIONS"] = 1024] = "GUILD_MESSAGE_REACTIONS"; Intents[Intents["GUILD_MESSAGE_TYPING"] = 2048] = "GUILD_MESSAGE_TYPING"; Intents[Intents["DIRECT_MESSAGES"] = 4096] = "DIRECT_MESSAGES"; Intents[Intents["DIRECT_MESSAGE_REACTIONS"] = 8192] = "DIRECT_MESSAGE_REACTIONS"; Intents[Intents["DIRECT_MESSAGE_TYPING"] = 16384] = "DIRECT_MESSAGE_TYPING"; Intents[Intents["MESSAGE_CONTENT"] = 32768] = "MESSAGE_CONTENT"; Intents[Intents["GUILD_SCHEDULED_EVENTS"] = 65536] = "GUILD_SCHEDULED_EVENTS"; Intents[Intents["ALL"] = 131071] = "ALL"; })(Intents || (Intents = {})); /** Slash command builder */ class SlashCommandBuilder { constructor() { this.options = []; this.type = ApplicationCommandTypes.CHAT_INPUT; this.contexts = [ InteractionContextTypes.GUILD, InteractionContextTypes.BOT_DM ]; this.interactionIntegrationTypes = [ InteractionIntegrationTypes.GUILD_INSTALL, ]; } addSlashCommandOption(type, name, desc, required, choices) { this.options.push({ type: type, name, description: desc, required, choices, }); } /** * Set the name of the slash command * @param name Name of the command * @returns SlashCommandBuilder Object */ setName(name) { this.name = name; return this; } /** * Set to context Interaction * @param type Type of context */ setContextInteraction(type) { assert(type === "MESSAGE" || type === "USER", "Type must be USER or MESSAGE"); if (type === "USER") { this.type = ApplicationCommandTypes.USER; } else { this.type = ApplicationCommandTypes.MESSAGE; } return this; } /** * Sets the contexts in which this slash command can be used. * * @param contexts - Array of interaction context types (GUILD, DM, or GROUP_DM) * @throws {AssertionError} If array is empty or has more than 3 contexts * @returns The current SlashCommandBuilder instance for method chaining * * @example * ```typescript * SlashCommandBuilder().setContexts([ * InteractionContextTypes.GUILD, * InteractionContextTypes.DM * ]); * ``` */ setContexts(contexts) { assert(contexts.length > 0, "At least one context is needed."); assert(contexts.length <= 3, "At most three contexts are needed."); this.contexts = contexts; return this; } /** * Sets the integration types that can use this slash command. * * @param types - Array of interaction integration types (GUILD_INSTALL or USER_INSTALL) * @throws {AssertionError} If array is empty or has more than 2 types * @returns The current SlashCommandBuilder instance for method chaining * * @example * ```typescript * SlashCommandBuilder().setInteractionIntegrationTypes([ * InteractionIntegrationTypes.GUILD_INSTALL * ]); * ``` */ setInteractionIntegrationTypes(types) { assert(types.length > 0, "At least one type is needed."); assert(types.length <= 2, "At most two types are needed."); this.interactionIntegrationTypes = types; return this; } /** * Set the description of the slash comamnd * @param desc Description of the command * @returns SlashCommandBuilder Object */ setDescription(desc) { this.description = desc; return this; } /** * Add an integer option to the slash command * @param name Name of the option * @param description Description of the option * @param required * @param choices If the option is required or not * @returns SlashCommandBuilder Object */ addNumberOption(name, description, required = false, choices) { this.addSlashCommandOption(ApplicationCommandOptionTypes.INTEGER, name, description, required, choices); return this; } /** * Add a string option to the slash command * @param name Name of the option * @param description Description of the option * @param required If the option is required or not * @returns SlashCommandBuilder Object */ addStringOption(name, description, required = false, choices) { this.addSlashCommandOption(ApplicationCommandOptionTypes.STRING, name, description, required, choices); return this; } /** * Add a boolean option to the slash command * @param name Name of the option * @param description Description of the option * @param required If the option is required or not * @returns SlashCommandBuilder Object */ addBooleanOption(name, description, required = false) { this.addSlashCommandOption(ApplicationCommandOptionTypes.BOOLEAN, name, description, required); return this; } /** * Add a user option to the slash command * @param name Name of the option * @param description Description of the option * @param required If the option is required or not * @returns SlashCommandBuilder Object */ addUserOption(name, description, required = false) { this.addSlashCommandOption(ApplicationCommandOptionTypes.USER, name, description, required); return this; } /** * Add an attachment option to the slash command * @param name Name of the option * @param description Description of the option * @param required If the option is required or not * @returns SlashCommandBuilder Object */ addAttachmentOption(name, description, required = false) { this.addSlashCommandOption(ApplicationCommandOptionTypes.ATTACHMENT, name, description, required); return this; } toJson() { const data = { name: this.name, type: this.type, contexts: this.contexts, integration_types: this.interactionIntegrationTypes }; if (this.type === ApplicationCommandTypes.CHAT_INPUT) { data.description = this.description; data.options = this.options; } return data; } addSubCommand(data) { this.options.push(data.toJson()); return this; } /** * Create a sub command group with sub commands * * @param name The name of the sub command group * @param description The description of the sub command group * @param subCommands An array of sub commands * @returns */ addSubCommandGroup(name, description, ...subCommands) { if (!subCommands.length) throw new Error("At least one subcommand is needed."); const newOptions = { name, description, type: 2, options: [], }; for (let i = 0; i < subCommands.length; i++) { newOptions.options.push(subCommands[i].toJson()); } this.options.push(newOptions); return this; } } /** Sub Command builder */ class SubCommandBuilder { constructor() { this.options = []; } addSlashCommandOption(type, name, desc, required, choices) { this.options.push({ type: type, name, description: desc, required, choices, }); } /** * Set the name of the sub command * * @param name Name of the sub command * @returns SubCommandBuilder object */ setName(name) { this.name = name; return this; } /** * Set the description of the sub command * * @param description Description of the sub command * @returns SubCommandBuilder object */ setDescription(description) { this.description = description; return this; } /** * Add an integer option to the slash command * @param name Name of the option * @param description Description of the option * @param required * @param choices If the option is required or not * @returns SlashCommandBuilder Object */ addNumberOption(name, description, required = false, choices) { this.addSlashCommandOption(ApplicationCommandOptionTypes.INTEGER, name, description, required, choices); return this; } /** * Add a string option to the slash command * @param name Name of the option * @param description Description of the option * @param required If the option is required or not * @returns SlashCommandBuilder Object */ addStringOption(name, description, required = false, choices) { this.addSlashCommandOption(ApplicationCommandOptionTypes.STRING, name, description, required, choices); return this; } /** * Add a boolean option to the slash command * @param name Name of the option * @param description Description of the option * @param required If the option is required or not * @returns SlashCommandBuilder Object */ addBooleanOption(name, description, required = false) { this.addSlashCommandOption(ApplicationCommandOptionTypes.BOOLEAN, name, description, required); return this; } /** * Add a user option to the slash command * @param name Name of the option * @param description Description of the option * @param required If the option is required or not * @returns SlashCommandBuilder Object */ addUserOption(name, description, required = false) { this.addSlashCommandOption(ApplicationCommandOptionTypes.USER, name, description, required); return this; } /** * Add an attachment option to the slash command * @param name Name of the option * @param description Description of the option * @param required If the option is required or not * @returns SlashCommandBuilder Object */ addAttachmentOption(name, description, required = false) { this.addSlashCommandOption(ApplicationCommandOptionTypes.ATTACHMENT, name, description, required); return this; } toJson() { return { name: this.name, description: this.description, type: 1, options: this.options, }; } } exports.SlashCommandBuilder = SlashCommandBuilder; exports.SubCommandBuilder = SubCommandBuilder;