Home > Blockchain >  Discord bot not answering messages
Discord bot not answering messages

Time:04-17

i've been trying a new project which is to code a discord bot with discord.js in node. The source code for interactions in the discord.js documentation works but, when i followed online tutorials, the bot does not reply to my messages in the discord server. please do correct me!, the bot seems to go online as my console does work. but the rest is history

const Discord = require('discord.js');
const env = require("./botconfig.json"); //json file containing tokens & IDs
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
let prefix = env.prefix;




client.on("ready", ()=>{
console.log(`${client.user.tag} is online in ${client.guilds.cache.size} servers`)
console.log(`the prefix is ${prefix}`);
})

client.on("messageCreate", Message => {
if (Message === "ping") {
    Message.channel.send('Pong.');
    }

 });

 client.login(env.token);

CodePudding user response:

On the client.on("messageCreate", the second argument is a function that takes a Message object, NOT the message as a string! (check the class here https://discord.js.org/#/docs/discord.js/stable/class/Message). You may wanna try the following:

// Access the `content` property of the message
if (Message.content === "ping") {
    Message.channel.send('Pong.');
}

Also seems the tutorials you're seeing are kind of deprecated. Discord.js has a lot of new stuff to make those easier and more organized. I would suggest you to try building your bot while following the official docs which are pretty good :) https://discord.js.org/#/docs/discord.js/stable/general/welcome

CodePudding user response:

The messageCreate event does not pass a string, it passes a Discord.js Message class instance, as DJS is object-oriented. Just access the .content property such as message.content.

  • Related