other arguments than `client` is not being passed to event handler

I am scaffolding code for my bot based of this guide: https://discordjs.guide/creating-your-bot/event-handling.html#reading-event-files But when any events other than ready (which requires 2 or more arguments passed) are invoked, it crashes with the following error:
2 |
3 | module.exports = {
4 | name: Events.InteractionCreate,
5 | once: false,
6 | async execute(client, interaction) {
7 | if(!interaction.isChatInputCommand()) return
^
TypeError: undefined is not an object (evaluating 'interaction.isChatInputCommand')
2 |
3 | module.exports = {
4 | name: Events.InteractionCreate,
5 | once: false,
6 | async execute(client, interaction) {
7 | if(!interaction.isChatInputCommand()) return
^
TypeError: undefined is not an object (evaluating 'interaction.isChatInputCommand')
I suspect that when interactionCreate is called (when I invoke a slash command, for example), interaction is not passed to the event file. Sample code index.ts (the part where I register events)
const eventsPath = path.join(__dirname, 'events')
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'))
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file)
const event = require(filePath)
if (event.once) {
client.once(event.name, (...args) => event.execute(...args))
} else {
client.on(event.name, (...args) => event.execute(...args))
}
}
const eventsPath = path.join(__dirname, 'events')
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'))
for (const file of eventFiles) {
const filePath = path.join(eventsPath, file)
const event = require(filePath)
if (event.once) {
client.once(event.name, (...args) => event.execute(...args))
} else {
client.on(event.name, (...args) => event.execute(...args))
}
}
events/interactionCreate.js
const { Events } = require('discord.js')

module.exports = {
name: Events.InteractionCreate,
once: false,
async execute(client, interaction) {
if(!interaction.isChatInputCommand()) return
// Handling commands ...
},
}
const { Events } = require('discord.js')

module.exports = {
name: Events.InteractionCreate,
once: false,
async execute(client, interaction) {
if(!interaction.isChatInputCommand()) return
// Handling commands ...
},
}
Yes I know it's not a good idea mixing ES5 and ESM, but I don't know does discord.js support it fully (I just returned from a year hiatus), the guide website still have references using ES5... Any help would be appreciated.
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
6 Replies
d.js toolkit
d.js toolkit4mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
treble/luna
treble/luna4mo ago
No event, except for the ready event emits with a client you either should remove the client parameter, as virtually all djs structures have a client property, or pass it in in your event handler
Azure
AzureOP4mo ago
alright, I will try removing the client parameter.. @wolvinny 🌈 it works! thank you so much.. by the way, is my current code structure (main file is typescript-based, events and commands are still ES5-based) okay, should I change it or keep it that way?
treble/luna
treble/luna4mo ago
djs is fully compatible with typescript so it should work
Azure
AzureOP4mo ago
alr.. thank you so much for answering my questions hope you a good day sir
Azure
AzureOP4mo ago
Want results from more Discord servers?
Add your server