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.
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 => { /* ... */ })
// ...
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_CREATEandMESSAGE_UPDATEevents have the full member object available.