Interaction Create Event
The interactionCreate
event in Discord.js is triggered whenever an interaction is created. This event is useful for handling various types of interactions such as slash commands, button clicks, and select menu selections. By listening to this event, you can execute specific code whenever a user interacts with your bot in these ways.
Project Structure
Now inside the client
folder add our interactionCreate.ts
file.
- ready.ts
- messageCreate.ts
- interactionCreate.ts
Create interactionCreate.ts
Now, we have to write the logic for our interactionCreate
event. This file will contain the logic that should be executed whenever a new interaction is created. Typically, you will check the type of interaction and then execute the corresponding action.
This interaction event handler comes with developer commands, bot permissions, and user permission checks built-in.
import {
ChatInputCommandInteraction,
PermissionsBitField,
Interaction,
EmbedBuilder,
GuildMember,
} from 'discord.js'
import { ExtendedClient } from '../../interfaces/ExtendedClient'
import { logger } from '../../utils/logger'
import config from '../../configs/botConfig'
import { COLORS, EMOJIS } from '../../constants/botConst'
export const eventHandlerInteraction = (client: ExtendedClient) => {
client.on('interactionCreate', async (interaction: Interaction) => {
if (!interaction.isCommand() || !interaction.guildId) return
const commandName = interaction.commandName
const command = client.slashCommands.get(commandName) // Use slashCommands collection
if (!command) {
logger.warn(`Command not found: ${commandName}`)
return
}
if (
command.userPermissions &&
!(interaction.member?.permissions as Readonly<PermissionsBitField>)?.has(
PermissionsBitField.resolve(command.userPermissions),
)
) {
const userPermEmbed = new EmbedBuilder()
.setColor(COLORS.yellow)
.setTitle('You cannot use this command!')
.setDescription(
`${EMOJIS.caution} You don't have the required permissions to use this commandz`,
)
.setTimestamp()
await interaction.reply({ embeds: [userPermEmbed], ephemeral: true })
return
}
const botMember = interaction.guild?.members.cache.get(client.user?.id || '') as GuildMember
if (
command.botPermissions &&
!botMember.permissions.has(PermissionsBitField.resolve(command.botPermissions))
) {
const botPermEmbed = new EmbedBuilder()
.setColor(COLORS.yellow)
.setTitle('I cannot use this command!')
.setDescription(
`${EMOJIS.caution} I don't have the required permissions to run this command`,
)
.setTimestamp()
await interaction.reply({ embeds: [botPermEmbed], ephemeral: true })
return
}
if (command.devOnly && !config.DEVELOPER_IDS.includes(interaction.user.id)) {
const devOnlyEmbed = new EmbedBuilder()
.setColor(COLORS.yellow)
.setTitle('You cannot use this command!')
.setDescription(`${EMOJIS.caution} This command is only for developers.`)
.setTimestamp()
await interaction.reply({ embeds: [devOnlyEmbed], ephemeral: true })
return
}
try {
await command.executeSlash(interaction as ChatInputCommandInteraction, client)
} catch (error) {
logger.error(`Error executing command: ${commandName} - ${error}`)
console.log(error)
const errorEmbed = new EmbedBuilder()
.setColor(COLORS.red)
.setTitle('Oops!')
.setDescription(`${EMOJIS.failed} There was an error trying to execute that command!`)
.setTimestamp()
await interaction.reply({ embeds: [errorEmbed], ephemeral: true })
}
})
}
You can add more interactions, for example, context interactions and other Discord.js interactions.
Done, now let's go ahead!