I am using discord.js to create a bot and have been following their guide until now to create the command and event handlers, including their format of putting each event handler and command function in its own file with its own exported module. This has worked fine for the 'ready' and 'interactionCreate' events, but for some reason my 'guildCreate' and 'guildDelete' events do not seem to be doing anything.
Here is how I am setting up the event listeners:
const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs
.readdirSync(eventsPath)
.filter((file) => file.endsWith('.js'));
eventFiles.forEach((file) => {
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));
}
});
Here is the contents of guildCreate.js (guildDelete.js is the same except the name of the event is 'guildDelete'):
module.exports = {
name: 'guildCreate',
execute(guild) {
console.log('joined guild');
console.log(guild.name);
},
};
I'm not sure why this is not printing anything as I can't see how it differs from the way the ready event is handled in the guide and that is working fine for me (https://discordjs.guide/creating-your-bot/event-handling.html#individual-event-files).
Turns out that setting the intents to be 8 (the administrator permissions as calculated using the bit math calculator in the Discord Developer Portal) does not include access to guild events and I had to specifically set Intents.Flags.GUILDS