21

With discord v14, I was trying to use the messageCreate event, however, after a user types a message in discord, message.content doesn't have any data as shown below:

Message {
  channelId: '998889338475655188',
  guildId: '948995127148425246',
  id: '998925735668498433',
  createdTimestamp: 1658232854526,
  type: 0,
  system: false,
  content: '',
  author: User 

I have tried searching around and can't find any solution to the issue, the code I am using relating to discord is:

import { Client, GatewayIntentBits, Partials } from "discord.js";

const bot = new Client({
  'intents': [
    GatewayIntentBits.DirectMessages,
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildBans,
    GatewayIntentBits.GuildMessages
  ],
  'partials': [Partials.Channel]
});

bot.on('messageCreate', async (message) => {
  console.log(message);
});

bot.login(process.env.token1)

Does anyone have any idea what is wrong or what needs changing from the new update?

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
user19530601
  • 223
  • 1
  • 2
  • 6
  • confirm you have the guild messages intent set and content/msg is not partial? – 0xLogN Jul 19 '22 at 12:32
  • @0xLogN It will work if the message is sent from itself. For instance I did: ```bot.on('ready', () => { console.log(`The Discord bot ${bot.user.username} is ready!`); bot.channels.cache.get(outputChannelID).send(`The Discord bot ${bot.user.username} is ready!`); });``` And in the console.log from `'messageCreate' async (message)` I get `content: The Discord bot bot.user.username is ready!` But not for users. – user19530601 Jul 19 '22 at 12:42

1 Answers1

57

Make sure you enable the message content intent on your developer portal and add the GatewayIntentBits.MessageContent enum to your intents array.

Applications ↦ Settings ↦ Bot ↦ Privileged Gateway Intents

enter image description here

You'll also need to add the MessageContent intent:

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

And make sure to use the messageCreate event instead of message:

client.on('messageCreate', (message) => {});

If you're using discord.js v13, you'll need to enable the message content intent on your developer portal and if your bot uses the Discord API v10, you will need to add the MESSAGE_CONTENT flag to your intents:

const { Client, Intents } = require('discord.js');
const client = new Client({
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
    Intents.FLAGS.MESSAGE_CONTENT,
  ],
});
Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
  • 1
    Discordjs v14 migrading doesn't say to add GatewayIntentBits.MessageContent they forgot to add it or smth – It'sMateo20 Aug 10 '22 at 12:14
  • 1
    @It'sMateo20 — It's a change in the permissions requirements at Discord's end. I assume discord.js has supported it for a while. – Quentin Aug 16 '22 at 10:43
  • For anyone facing the same issue but in DMs. You should add `GatewayIntentBits.DirectMessages` in the intents as explained above – shivanshPurple Jul 31 '23 at 14:12