Home > Net >  Discord Bot Doesn't reply back (Node.js)
Discord Bot Doesn't reply back (Node.js)

Time:03-29

I'ven struggling creating a bot for my discord server so i used the initial code for the discord bot from discordjs.guide and added a simple command to respond to "ping", i wanted to prove what i did wrong before... but i get no response from the bot. I already give it permissions with the invite link and with a role, someone can tell me what's wrong with the code? I installed discord.js and dotenv

// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');
});

client.on('message', function(msg){
    if(msg.content === 'ping'){
        msg.reply('pong');
    }
});

// Login to Discord with your client's token
client.login(token);

CodePudding user response:

Try define your token in the script. dotenv isn't found anywhere in the code, if you are going to require a .json file, put your token in the .json file, not the .env file.

CodePudding user response:

Your code is missing the GUILD_MESSAGES intent. Simply add it into your intents and your bot should respond to messages. Pay in mind that discord is removing message intents after April 30th so you'd need to go into https://discord.com/developers and into the bot section, turn on message intents.

// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');

// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS. GUILD_MESSAGES] });

// When the client is ready, run this code (only once)
client.once('ready', () => {
    console.log('Ready!');
});

client.on('message', function(msg){
    if(msg.content === 'ping'){
        msg.reply('pong');
    }
});

// Login to Discord with your client's token
client.login(token);

Pay in mind that discord is removing message intents after April 30th so you'd need to go into https://discord.com/developers and into the bot section, turn on message intents.

CodePudding user response:

it's because the client.on("message") has been deprecated

And you should define on your discord client the GUILD_MESSAGES as this

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

and the event to receive the message on your server would be

client.on('messageCreate', function(msg){
    if(msg.content === 'ping'){
        msg.reply('pong');
    }
});

Docs of "messageCreate" event

  • Related