2

I'm trying out V14 of Discord.js, and there are so many new things! But this whole intents thing, I'm not getting for some reason. My messageCreate is not firing, and from other posts, I believe it has something to do with intents. I've messed around with intents, but nothing seems to work. Here is my code:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (message) => {
  console.log("new message...")
  console.log(message.message)
  console.log(message.content)
if (message.mentions.users.first() === client) {
  message.reply("Hey!.")
} 
});
client.login(process.env.token);

Thanks!!

MrMythical
  • 8,908
  • 2
  • 17
  • 45
Tyler Mazur
  • 63
  • 1
  • 7

2 Answers2

2

Figured it out! You need to turn on message content intents in the developer portal:

Picture

Also, you need to have messageContent intent at the top:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [ 
  GatewayIntentBits.DirectMessages,
  GatewayIntentBits.Guilds,
  GatewayIntentBits.GuildBans,
  GatewayIntentBits.GuildMessages,
  GatewayIntentBits.MessageContent,] });
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (message) => {
if (message.content === "<@1023320497469005938>") {
  message.reply("Hey!")
} 
});
client.login(process.env.token);

As for getting the ping from the message, not my initial question but something I needed to fix, just use if (message.content) === "<@BotID>" {CODE HERE}

MrMythical
  • 8,908
  • 2
  • 17
  • 45
Tyler Mazur
  • 63
  • 1
  • 7
1

You need the GuildMessages intent.

Note: You do not need the MessageContent intent in messages that ping the bot, as bots can receive content from messages that ping them without the intent.

const client = new Client({ 
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages
  ]
});
// ...
client.on("messageCreate", (message) => {
  // ...
  if (message.mentions.users.has(client.user.id)) { // this could be modified to make it have to *start* with or have only a ping
    message.reply("Hey!.")
  } 
});
// ...
MrMythical
  • 8,908
  • 2
  • 17
  • 45