Skip to content

Commit

Permalink
Gh-56: Add method to handle custom interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Nov 21, 2021
1 parent 6238012 commit 97a665f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 21 deletions.
12 changes: 11 additions & 1 deletion src/bot/TicTacToeBot.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import AppCommandRegister from '@bot/command/AppCommandRegister';
import GameCommand from '@bot/command/GameCommand';
import EventHandler from '@bot/EventHandler';
import GameStateManager from '@bot/state/GameStateManager';
import Config from '@config/Config';
import { Client, Message, WSEventType } from 'discord.js';
import AppCommandRegister from './command/AppCommandRegister';

/**
* Manages all interactions with the Discord bot.
Expand Down Expand Up @@ -84,4 +84,14 @@ export default class TicTacToeBot {
public handleMessage(message: Message): void {
this.command.handleMessage(message, true);
}

/**
* Programmatically handles a discord.js interaction to request a game.
*
* @param interaction Discord.js interaction object
* @param client Discord.js client instance
*/
public handleInteraction(interaction: any, client: Client): void {
this.command.handleInteraction(client, interaction, true);
}
}
43 changes: 23 additions & 20 deletions src/bot/command/GameCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,35 @@ export default class GameCommand {
/**
* Handles an incoming interaction.
*
* @param client discord.js client
* @param interaction discord.js interaction instance
* @param noTrigger true to bypass trigger checks
*/
public async handleInteraction(client: Client, interaction: any): Promise<void> {
// Retrieve all interaction entities from cache or Discord API
const channel = client.channels.cache.get(interaction.channel_id);
const guild = client.guilds.cache.get(interaction.guild_id);
const user = await client.users.fetch(interaction.member.user.id);

public async handleInteraction(
client: Client,
interaction: any,
noTrigger = false
): Promise<void> {
if (
guild &&
user &&
channel instanceof TextChannel &&
interaction.data.type === 1 &&
interaction.data.name === this.config.slashCommand &&
!user.bot
noTrigger ||
(interaction.data.type === 1 && interaction.data.name === this.config.slashCommand)
) {
// Retrieve the inviter and create an interaction tunnel
const inviter = await guild.members.fetch(user.id);
const tunnel = new InteractionMessagingTunnel(interaction, client, inviter, channel);
// Retrieve all interaction entities from cache or Discord API
const channel = client.channels.cache.get(interaction.channel_id);
const guild = client.guilds.cache.get(interaction.guild_id);

// Retrieve invited user from options if provided
const invited = interaction.data.options
? await guild.members.fetch(interaction.data.options[0]?.value)
: undefined;
if (guild && channel instanceof TextChannel) {
// Retrieve the inviter and create an interaction tunnel
const inviter = await guild.members.fetch(interaction.member.user.id);
const tun = new InteractionMessagingTunnel(interaction, client, inviter, channel);

return this.handleInvitation(tunnel, inviter, invited);
// Retrieve invited user from options if provided
const invited = interaction.data.options
? await guild.members.fetch(interaction.data.options[0]?.value)
: undefined;

return this.handleInvitation(tun, inviter, invited);
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class TicTacToe {

/**
* Attaches an external Discord Client to the module.
*
* @param client Discord.js client instance
*/
public attach(client: Client): void {
this.bot.attachToClient(client);
Expand All @@ -80,6 +82,16 @@ class TicTacToe {
this.bot.handleMessage(message);
}

/**
* Programmatically handles a discord.js interaction to request a game.
*
* @param interaction Discord.js interaction object
* @param client Discord.js client instance
*/
public handleInteraction(interaction: any, client: Client): void {
this.bot.handleInteraction(interaction, client);
}

/**
* Register a listener to a specific event by its name.
*
Expand Down

0 comments on commit 97a665f

Please sign in to comment.