Home >Web Front-end >JS Tutorial >How to Fix Empty Message Content in Discord.js v14?

How to Fix Empty Message Content in Discord.js v14?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-02 18:56:11538browse

How to Fix Empty Message Content in Discord.js v14?

Discord.js: Retrieving Message Content in v14

In Discord.js v14, accessing message content through message.content may return empty values. This issue can be resolved by enabling the Message Content privileged gateway intent.

Enable Message Content Intent

  1. Navigate to the Discord Developer Portal.
  2. Select your bot and go to the "Bot" settings.
  3. Under "Privileged Gateway Intents," enable the "Message Content" intent.

Update Discord.js Code

Once the intent is enabled, update your Discord.js code as follows:

const { Client, GatewayIntentBits, Partials } = require('discord.js');

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

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

bot.login(process.env.token1);

Additional Notes

  • If you're using Discord.js v13, you'll also need to enable the Message Content intent on the developer portal.
  • If your bot uses the Discord API v10, 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,
  ],
});

The above is the detailed content of How to Fix Empty Message Content in Discord.js v14?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn