Bot does not receive messages

Viewed 70

I'm trying to make my bot reply on messages but it doesn't work. Does anybody know how to fix this?

Code:

const Discord = require('discord.js');

const client = new Discord.Client({ intents: ['GuildMessages', 'GuildMessageReactions', 'GuildMessageTyping', 'MessageContent', ''] })

const prefix = '!';

client.once('ready', () => {
    console.log('MONKE BOT RISES');
});

client.on('messageCreate', message => {
    message.channel.send('pong');
    console.log('hoi');
    if(!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'ping'){
        message.channel.send('pong');
    }
});



client.login('<token>');
1 Answers

You'll need to enable the MessageContent gateway intent in the Discord Developer Portal due to its priviledged nature.

enter image description here

Check out a good article on this in the docs.


I have tested your code, and everything works correctly.

let message = {
    content: "!ping"
};
const prefix = '!';

const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();

if (command === 'ping') {
    console.log('pong!');
};

Therefore this behaviour can only have been caused by incorrectly assigned intents, or an error in your code during instantiation of Client.

As I said before, double check MessageContent is enabled in the dev portal. Then try instantiating Client like this

const { Client, GatewayIntentBits } = require('discord.js');

const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });

If this doesn't work, check you're using the latest version of node and discord.js (npm list discord.js, node --version).

Related