Azure
Azure
DIAdiscord.js - Imagine an app
Created by Azure on 8/26/2024 in #djs-questions
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.
10 replies
DIAdiscord.js - Imagine an app
Created by Azure on 10/28/2022 in #djs-questions
Interaction already submitted
8 replies
DIAdiscord.js - Imagine an app
Created by Azure on 9/22/2022 in #djs-questions
guildMemberUpdate doesn't fire for users that joined when the bot was offline
Hello, I'm trying to make a bot that will give a role to an user that has accepted the server rules by checking for the pending property, what I found peculiar is that if the user joined when the bot is offline, when the bot comes back online it refuses to give that user a role even when that user accept the rules. And I also found out that calling await client.guilds.cache.get(guildID).members.fetch() on ready event resolves that issue. Any heads up? Here is my code and a video demonstrating the issue. I have already enabled Guilds and GuildMembers intents, and this issue doesn't occur when the user joins when the bot is up. Or more specifically, it doesn't even fire guildMemberUpdate, as to why the console.log() didn't fire. I don't want to call a fetch for guild members because my bot's prod guild has over 4k members.
29 replies