UNPKG

@ayanaware/bentocord

Version:

Bentocord is a Bento plugin designed to rapidly build fully functional Discord Bots.

99 lines 3.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InteractionContext = void 0; const IsTextableChannel_1 = require("../util/IsTextableChannel"); const BaseContext_1 = require("./BaseContext"); class InteractionContext extends BaseContext_1.BaseContext { constructor(api, interaction) { super(api, interaction.channel, interaction.user ?? interaction.member.user); this.interaction = interaction; } async prepare() { const client = this.discord.client; let channel = client.getChannel(this.channelId); if (!channel) channel = await client.getRESTChannel(this.channelId); if (!(0, IsTextableChannel_1.IsTextableChannel)(channel)) throw new Error('InteractionContext: Channel is not textable.'); this.channel = channel; if (this.interaction.member) { this.member = this.interaction.member; this.user = this.interaction.member.user; } else if (this.interaction.user) { this.user = this.interaction.user; } if (this.interaction.guildID) { // attempt to get guild object const guild = client.guilds.get(this.interaction.guildID); this.guild = guild; // attempt to get selfMember this.selfMember = guild.members.get(this.selfId); } } async defer(flags) { if (this.interaction.acknowledged) return; this.responseId = '@original'; return this.interaction.defer(flags); } async getResponse() { if (this.responseId === '@original') { const original = await this.interaction.getOriginalMessage(); this.responseId = original.id; this.responseMessage = original; return original; } if (this.responseMessage && this.responseMessage.id === this.responseId) return this.responseMessage; const message = await this.discord.client.getMessage(this.channelId, this.responseId); this.responseMessage = message; return message; } async createResponse(response, files) { if (this.interaction.acknowledged) return this.editResponse(response, files); this.responseId = '@original'; await this.interaction.createMessage(response, files); } async editResponse(response, files) { if (!this.interaction.acknowledged) return this.createResponse(response, files); // create a new message if (!this.responseId) { const message = await this.interaction.createFollowup(response, files); this.responseId = message.id; this.responseMessage = message; return; } try { await this.interaction.editMessage(this.responseId, response, files); } catch (e) { // issue editing, create a brand new message const message = await this.interaction.createFollowup(response, files); this.responseId = message.id; this.responseMessage = message; } } async deleteResponse() { await this.deleteMessage(this.responseId); this.responseId = null; this.responseMessage = null; } async createMessage(content, files) { if (this.interaction.acknowledged) return this.interaction.createFollowup(content, files); // not been acknowledged yet await this.interaction.createMessage(content, files); return this.interaction.getOriginalMessage(); } async editMessage(messageId, content, files) { return this.interaction.editMessage(messageId, content, files); } async deleteMessage(messageId) { return this.interaction.deleteMessage(messageId); } } exports.InteractionContext = InteractionContext; //# sourceMappingURL=InteractionContext.js.map