UNPKG

hennus-api

Version:

Esta es una libreria para el bot Hennus

127 lines (126 loc) 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BasedInteraction = void 0; const v10_1 = require("discord-api-types/v10"); const data_1 = require("./data"); const guild_1 = require("../guild"); const events_1 = require("../events"); const user_1 = require("../user"); class BasedInteraction extends data_1.BaseData { constructor(data, client) { super(client); this.id = ""; this.token = ""; this.aplicationId = ""; this.id = data.id; this.token = data.token; this.guildID = data.guild_id ?? ""; this.guild = this.client.guilds.resolve(this.guildID) ?? {}; this.channel = this.client.channels.resolve(data.channel?.id ?? "") ?? {}; if (data.message) this.message = new events_1.Message(data.message, client); this.type = data.type; if (data.member) { this.member = new guild_1.GuildMember(data.member, this.guild, client); if (this.member.user) this.user = this.member.user; } ; if (data.user) { this.user = new user_1.User(data.user, client); const member = this.guild.members.resolve(this.user.id); if (member) this.member = member; } ; this.aplicationId = data.application_id; } ; isCommand() { return this.type == v10_1.InteractionType.ApplicationCommand || this.type == v10_1.InteractionType.ApplicationCommandAutocomplete; } ; isComponents() { return this.type == v10_1.InteractionType.MessageComponent; } ; isModal() { return this.type == v10_1.InteractionType.ModalSubmit; } ; async reply(options) { const data = { content: undefined, embeds: undefined, components: undefined, flags: undefined, }; let files = undefined; if (typeof options === 'string') { data.content = options; } else if (typeof options == "object") { if (options.components && Array.isArray(options.components)) data.components = options.components; if (options.embeds && Array.isArray(options.embeds)) data.embeds = options.embeds; if (options.content) data.content = options.content; if (options.ephemeral) data.flags = v10_1.MessageFlags.Ephemeral; if (options.flags) data.flags = data.flags ?? 0 | options.flags; if (options.attachments) { files = []; for (let i = 0; i < options.attachments.length; i++) { const attach = options.attachments[i]; let contentType = ""; let _buffer = undefined; let name = `default${i}.txt`; if (typeof attach.attachment == "string") { const imagen = await this.imagen(attach.attachment); if (imagen) { const buffer = Buffer.from(imagen.data, 'binary'); _buffer = buffer; name = attach.name ?? `default.${imagen.type}`; contentType = imagen.content_type; } ; } else { _buffer = attach.attachment; name = attach.name ?? `default${i}.txt`; } ; if (_buffer) files.push({ data: _buffer, name, contentType }); } ; } ; } ; const msg = await this.client.rest.post("interactionCallback", { body: { type: v10_1.InteractionResponseType.ChannelMessageWithSource, data: data }, files }, this.id, this.token); if (msg instanceof Error) throw msg; if (typeof options == "object" && typeof options.timeout == "number") setTimeout(async () => { try { await this.client.rest.api.delete(v10_1.Routes.webhookMessage(this.client.id, this.token, "@original")); } catch { undefined; } ; }, options.timeout); return msg; } ; async respond(modal) { return await this.client.rest.post("interactionCallback", { body: { type: v10_1.InteractionResponseType.Modal, data: modal } }, this.id, this.token); } ; } exports.BasedInteraction = BasedInteraction; ;