Search code examples
javascriptnode.jsdiscordbots

Error when trying to create Discord client instance with node.js


I am trying to build a small Discord bot, but it can't read/acces messages.

The bot has all permissions possible but seems to not receive/detect messages...

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

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
  console.log('Message received')
  if (msg.content === 'ping') {
    msg.reply('Pong!');
  }
});
client.login('Token');

I added code that would log any sent messages, even if they aren't a command, but the Bot doesn't log anything.

Edit: The login confirmation works, its the right account. But the Bot still doesn't receive any messages...


Solution

  • If the bot is properly authenticated and the token value is entered correctly, a possible cause of the problem is that it forgot one of the "assumptions" specified when creating the client.

    To receive messages, upgrade the messages you're currently using instead of:

    const client = new Discord.Client({
      object: ["GUILDS", "GUILD_MESSAGES"]
    });
    console.log(`Logged in with ${client.user.tag}!`);
    client.on('Message', msg => { console.log('Message Received') });
    
    client.login('credential');
    

    In this example, "GUARDS" and "GUILD_MESSAGES"; The topic was mentioned. The server and program needed to receive the message.

    Thanks!