1

I have this code where I want the bot to reply to a command, in the console it says it has received the message but does not reply.

client.on('message', msg => 
  {
    console.log("Message recieved.")
    if (msg.content === 'ping') 
    {
             msg.reply("pong")
         msg.delete()
    }
  });

I have tried replacing the code with different code in different ways, but nothing has worked.

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Do you know if the code inside the if statement runs? – Gavin Morrow Nov 12 '22 at 00:36
  • Does the bot have the [message content intent](https://discord-api-types.dev/api/discord-api-types-v10/enum/GatewayIntentBits#MessageContent)? – Gavin Morrow Nov 12 '22 at 00:42
  • Does this answer your question? [message.content doesn't have any value in Discord.js v14](https://stackoverflow.com/questions/73036854/message-content-doesnt-have-any-value-in-discord-js-v14) – Zsolt Meszaros Nov 12 '22 at 08:52

2 Answers2

1

You are deleting the message immediately, which might be the problem.

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

The above is how I'm sure anyone would write it. As a side note, have you granted the bot permissions to send messages?

kryozor
  • 315
  • 1
  • 7
0

Welcome to StackOverflow, ReverseAlpha!

Shawn's answer is correct, but I would like to expand upon it.

It's possible that your bot's request to reply to the message (the promise sent by Discord.js to the API) succeeds after the request to delete that message, and once the message is deleted you can't reply to it. If you really wanted to delete the original message after replying, you could use async/await to wait for the message reply to complete, and then delete the original message.

Besides, you're listening on the message event while since Discord.js v13 the event has been renamed to messageCreate.

I've rewritten your code to fix these changes (and also a small spelling error, I tend to do that)

client.on('messageCreate', async msg => {
    console.log("Message received.")
    if (msg.content === 'ping') {
        await msg.reply("pong")
        await msg.delete()
    }
});

Hope it helps!

Thunder
  • 461
  • 2
  • 6