I'm trying to run a Discord bot using the discord.js library, but I'm getting the following error: TypeError: Cannot read properties of undefined (reading 'GUILD_MESSAGES'). I have installed the latest version of the discord.js library and enabled the relevant intents in the Discord Developer Portal, but the error persists. Here is my code:
const Discord = require('discord.js');
// Specify which intents the client should have access to
const intents = new Discord.Intents([
Discord.Intents.GUILD_MESSAGES,
Discord.Intents.DIRECT_MESSAGES
]);
// Pass the intents array as an option when creating the Client object
const client = new Discord.Client({ intents: Discord.Intents.ALL });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', message => {
if (message.content === '!ping') {
// Get a list of all guild channels
const channels = message.guild.channels.cache;
// Select a random channel from the list
const channel = channels.random();
// Get a list of all members in the channel
const members = channel.members.array();
// Select a random member from the list
const member = members[Math.floor(Math.random() * members.length)];
// Send a message to the channel mentioning the random member
channel.send(`${member}, you have been pinged!`)