Why is my Discord bot not working on Node.js?

The bot starts, but does not respond to !ping and !sum.
const { Client, GatewayIntentBits } = require('discord.js');

const bot = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages,]
});
const prefix = "!";

bot.on('ready', async () => {
console.log(`${bot.user.username} запустился`)
});

bot.on("messageCreate", function(message) {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;

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

if (command === "ping") {
const timeTaken = Date.now() - message.createdTimestamp;
message.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
}

else if (command === "sum") {
const numArgs = args.map(x => parseFloat(x));
const sum = numArgs.reduce((counter, x) => counter += x);
message.reply(`The sum of all the arguments you provided is ${sum}!`);
}
});

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

const bot = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages,]
});
const prefix = "!";

bot.on('ready', async () => {
console.log(`${bot.user.username} запустился`)
});

bot.on("messageCreate", function(message) {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;

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

if (command === "ping") {
const timeTaken = Date.now() - message.createdTimestamp;
message.reply(`Pong! This message had a latency of ${timeTaken}ms.`);
}

else if (command === "sum") {
const numArgs = args.map(x => parseFloat(x));
const sum = numArgs.reduce((counter, x) => counter += x);
message.reply(`The sum of all the arguments you provided is ${sum}!`);
}
});

bot.login('');
7 Replies
d.js toolkit
d.js toolkit14mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
dank
dank14mo ago
Errors?
vo0ov
vo0ovOP14mo ago
discord.js - 14.13.0 Node.js - v21.1.0 No errors
kin.ts
kin.ts14mo ago
you missed the MessageContent intent, because you didn't have the intent, everytime ur bot receive a messageCreate event, <Message>.content will always be an empty string, and stuck at if(!message.content.startsWith(prefix)) return; btw, #rules 2
vo0ov
vo0ovOP14mo ago
pls send corrected code
kin.ts
kin.ts14mo ago
just add the MessageContent intent in your client intents like how you add the Guilds and GuildMessages intent
vo0ov
vo0ovOP14mo ago
Thanks!
Want results from more Discord servers?
Add your server