Home > Enterprise >  Discord bot not detecting messages?
Discord bot not detecting messages?

Time:09-22

I'm trying to get my first Discord bot to work and log in console when a message is detected, but nothing happens. Heres my code, whats wrong with it?

const Discord = require("discord.js");  
const client = new Discord.Client({ intents: [ 'DIRECT_MESSAGES', 'GUILD_MESSAGES' ] });

const token = 'ODg5OTc4NzQyOTg0ODE4Njk4.YUpHSw.Q_0-h1spjFyVpRP-rU_LFEbzit4';

const PREFIX = '!';

client.on('ready', () =>{

    console.log('Bot Online! Woohoo!');

});


client.on('message', message =>{

console.log('Message registered!');

});

client.login(token);

CodePudding user response:

Discord.js V13 has a few changed events types. In this case the event for receiving messages has changed from message to messageCreate.

See upgrade changes here.

! Note that using the message event will throw a deprecated error message which will be removed in the future.

Update: You need to add partials as well if you want your bot work on direct messages:

partials: ["MESSAGE", "CHANNEL"]

Add intent GUILDS if you wish to receive both server and direct messages. So altogether:

const Discord = require("discord.js");  
const client = new Discord.Client({
    intents: [
        /*
            Intents 'GUILDS' is required
            if you wish to receive (message) events
            from guilds as well.

            If you don't want that, do not add it.
            Your bot will only receive events
            from Direct Messages only.
        */
        'GUILDS',
        'DIRECT_MESSAGES',
        'GUILD_MESSAGES'
    ],
    partials: ['MESSAGE', 'CHANNEL'] // Needed to get messages from DM's as well
});
const token = 'YourBotToken';
const PREFIX = '!';

client.on('ready', () =>{
    console.log('Bot Online! Woohoo!');
});


client.on('messageCreate', message =>{
    console.log('Message registered!');
});

client.login(token);

P.S. Regenerate your bot token on the developer dashboard.

CodePudding user response:

if you just started to work with the discord.js library I highly recommend to look a the discord.js documentation would also recommend not showing your bot token here since people can use it as they want, what your looking for could be somthing like this

       const Discord = require("discord.js");  
        const client = new Discord.Client({ intents:['DIRECT_MESSAGES', 'GUILD_MESSAGES' ],
        partials: ['MESSAGE', 'CHANNEL'] });
        
        const token = 'Your_token_Here';
        
        const PREFIX = '!';
        
        client.on('ready', () =>{
            console.log('Bot Online!'); //bot successfully logged in
        
        });
        client.on('message', message => {
            console.log(message.content); //log messages
        });
        
        client.login(token);
  • Related