Skip to content

Commit

Permalink
Support deferred interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Feb 6, 2022
1 parent 06dda8c commit 582f909
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/bot/command/GameCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import TextMessagingTunnel from '@bot/messaging/TextMessagingTunnel';
import GameStateManager from '@bot/state/GameStateManager';
import CommandConfig from '@config/CommandConfig';
import localize from '@i18n/localize';
import { CommandInteraction, GuildMember, Interaction, Message, TextChannel } from 'discord.js';
import { GuildMember, Interaction, Message } from 'discord.js';

/**
* Command to start a duel with someone else.
Expand Down Expand Up @@ -44,7 +44,7 @@ export default class GameCommand {
if (
message.member &&
!message.author.bot &&
message.channel instanceof TextChannel &&
message.channel.isText() &&
(noTrigger ||
(this.config.textCommand && message.content.startsWith(this.config.textCommand)))
) {
Expand All @@ -63,22 +63,21 @@ export default class GameCommand {
*/
public async handleInteraction(interaction: Interaction, noTrigger = false): Promise<void> {
if (
interaction instanceof CommandInteraction &&
interaction.channel instanceof TextChannel &&
interaction.member instanceof GuildMember &&
interaction?.isCommand() &&
interaction.inCachedGuild() &&
interaction.channel?.isText() &&
(noTrigger || interaction.commandName === this.config.command)
) {
// Retrieve the inviter and create an interaction tunnel
const tunnnel = new CommandInteractionMessagingTunnel(interaction);
const tunnel = new CommandInteractionMessagingTunnel(interaction);

// Retrieve invited user from options if provided
const mentionned = interaction.options.getMember(
this.config.commandOptionName ?? 'opponent',
false
);
const invited = mentionned instanceof GuildMember ? mentionned : undefined;
const member = await interaction.member.fetch();
const mentionned =
interaction.options.getMember(this.config.commandOptionName ?? 'opponent', false) ??
undefined;

return this.handleInvitation(tunnnel, interaction.member, invited);
return this.handleInvitation(tunnel, member, mentionned);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/bot/messaging/CommandInteractionMessagingTunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export default class CommandInteractionMessagingTunnel extends MessagingTunnel {
* @inheritdoc
*/
public async replyWith(answer: MessagingAnswer, _direct?: boolean): Promise<Message> {
// Fetch current reply if deferred externally and not register in this tunnel
if (!this.reply && this.interaction.deferred) {
this._reply = (await this.interaction.fetchReply()) as Message;
}

// Edit the reply if already exists
if (this.reply) {
await this.editReply(answer);
Expand Down

0 comments on commit 582f909

Please sign in to comment.