1
import { Client, Intents } from 'discord.js';
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.once('ready', () => {
    console.log('Ready!');
});
client.on("message", async msg => {
    if (msg.content === "ping") {
        console.log("ping")
        msg.reply("pong")
    }
});

client.login(token);

I'm using discord.js v13, the bot logs in correctly and outputs "Ready!" and then when I type a message in the server it's online in it doesn't even console.log

Any help appreciated, thanks in advance.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
WORDrc
  • 13
  • 3
  • Also duplicate of: "[message event listener not working properly](https://stackoverflow.com/q/64394000/90527)", "[Having trouble sending a message to a channel with Discord.js](https://stackoverflow.com/q/68795635/90527)", "[Discord bot is not replying to messages](https://stackoverflow.com/q/68804831/90527)". – outis Oct 30 '21 at 22:22

2 Answers2

1

You need the GUILD_MESSAGES intent enabled to receive message events. Additionally, as CcmU said, the message event is deprecated, and should be replaced with messageCreate.

// ...
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
// ...
client.on("messageCreate", async msg => { /* ... */ })
// ...
iamkneel
  • 1,303
  • 8
  • 12
0

As you can see from the docs, the event message is deprecated. You can use instead a listener for the event messageCreated if you want to listen for any new message.

So you can try something like

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

EDIT:

As @chickensalt pointed out, you have to declare in your Intents the appropriate one for handling messages, that is Intents.FLAGS.GUILD_MESSAGES. So, when creating your Client:

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

But remember to use the correct Intent for your needs, because you could have some trouble in handling memory correctly. As the docs suggests:

For larger bots, client state can grow to be quite large. We recommend only storing objects in memory that are needed for a bot's operation. Many bots, for example, just respond to user input through chat commands. These bots may only need to keep guild information (like guild/channel roles and permissions) in memory, since MESSAGE_CREATE and MESSAGE_UPDATE events have the full member object available.

CcmU
  • 780
  • 9
  • 22