最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is my messageCreate event not working? (discord.js) - Stack Overflow

programmeradmin1浏览0评论

I tried making a discord bot..

I looked for some tutorials but my code doesn't seem to work..

I created a simple ping pong mand but for some reason its not working!

Heres my bot.js code:

require('dotenv').config();

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, 'GuildMessages'] });

client.on('ready', () => {
console.log(`Thunder bot is ready! Tag is ${client.user.tag}`);
});

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

client.login(process.env.TOKEN);

But the ping pong mand is not working!

I tried making a discord bot..

I looked for some tutorials but my code doesn't seem to work..

I created a simple ping pong mand but for some reason its not working!

Heres my bot.js code:

require('dotenv').config();

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, 'GuildMessages'] });

client.on('ready', () => {
console.log(`Thunder bot is ready! Tag is ${client.user.tag}`);
});

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

client.login(process.env.TOKEN);

But the ping pong mand is not working!

Share Improve this question asked Aug 15, 2022 at 5:34 RavioliRavioli 331 silver badge6 bronze badges 6
  • Dont you need to use it like messageCreate.content and messageCreate.reply since you write messageCreate on client.on function? – Batu.Khan Commented Aug 15, 2022 at 6:05
  • In the client.on('messageCreate') event listener, you have named the message you get as messageCreate. Then you are trying to reply to a variable which doesn't exist which is message. So you just have to change that – Caladan Commented Aug 15, 2022 at 6:29
  • Voted to close as it's typo: client.on('message', (messageCreate) should be client.on('messageCreate', (message) =. You'll also need the GatewayIntentBits.MessageContent intent. – Zsolt Meszaros Commented Aug 15, 2022 at 12:17
  • @Batu.Khan I did that.. still not working! – Ravioli Commented Aug 15, 2022 at 14:34
  • @Caladan still not working – Ravioli Commented Aug 15, 2022 at 14:35
 |  Show 1 more ment

4 Answers 4

Reset to default 6

There are 2 reasons your bot isn't responding to you:

  1. Your bot doesn't have 'MessageContent' intent
const client = new Client({ intents: ['Guilds', 'GuildMessages', 'MessageContent'] });
  1. client.on('message'... may result to a DeprecationWarning
    Here is the correction:
client.on('messageCreate', (message) => {
    if (message.content === 'ping'){
        message.reply('Pong!')
    }
});
  1. You need to use the following intents to read and react to messages:
{ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] }
  1. The event you want to listen for is called "messageCreate" (you were listening for "message"):
client.on('messageCreate', (message) => {
if (message.content === 'ping'){
    message.reply('Pong!')
}
});

This should work:

require('dotenv').config();

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });

client.on('ready', () => {
console.log(`Thunder bot is ready! Tag is ${client.user.tag}`);
});

client.on('messageCreate', (message) => {
if (message.content === 'ping'){
    console.log("!")
    message.reply('Pong!')
}
});

client.login(process.env.TOKEN);

2 reasons this is happening;

  1. You put message instead of messageCreate and vice versa in your client.on(). Put this instead:
client.on('messageCreate', message => {
    if (message.content === 'ping'){
        message.reply('Pong!')
    }
});
  1. You're missing the messageContent intent.
const client = new Client({ intents: ["Guilds", "GuildMessages", "MessageContent"] });

Following above solutions initially gave me this error:

This error was solved by having the 'message content intent' toggled on in the 'Bot' section of the developer portal:

发布评论

评论列表(0)

  1. 暂无评论