-2

I have make a simple script to test my bot. Bot started and I show "is online" on discord and log a message.

But why when I type a command, bot not sending any message?

const Discord   = require("discord.js")
const client    = new Discord.Client()

let token = "xxx";

client.login(token)
client.on("ready", ready => {
    console.log("Bot lancé avec succès 10 !")
    client.user.setActivity("IN DEVELOPPEMENT")
});


client.on("message", message => {
  if (message.content === "!ping") {
       console.log("Bot lancé avec succès 111111 !")
    message.channel.send("Pong.")
  }
})

Is there a reason why this is not working? Am I missing something?

Re9iNee
  • 412
  • 5
  • 15

1 Answers1

0

Message based commands are no longer recommended as a way to interact with Discord bots. You can see how to set up slash commands using this discord.js official guide, you can see the official guide for responding to slash commands here.

If you would like to still set up an event listener for messages and respond to them your message listener code should look like this:

client.on("messageCreate", message => {
  if (message.content === "!ping") {
    console.log("Bot lancé avec succès 111111 !")
    message.channel.send("Pong.")
  }

The reason it did not work is because you were listening to the event message which does not exist, the event messageCreate on the other-hand does exist and is emitted whenever a user sends a message.

dylanface
  • 123
  • 7
  • hello i have test your code but not working. ```client.on("messageCreate", message => { if (message.content === "!ping") { console.log("Bot lancé avec succès 111111 !") message.channel.send("Pong.") }``` All is ok, why i not receive pong message when i type !ping TY – Arthur Gorbana May 15 '23 at 01:32
  • You need to update your Gateway Intents as detailed here in the [discord.js official guide](https://discordjs.guide/popular-topics/intents.html#privileged-intents). The intent you need is `MessageContent` @ArthurGorbana – dylanface May 15 '23 at 01:44
  • ```const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.MessageContent, ], });``` I have this error : **TypeError: Cannot read properties of undefined (reading 'MessageContent')** – Arthur Gorbana May 15 '23 at 01:58
  • I have node 16.x – Arthur Gorbana May 15 '23 at 02:11