
Discord.js:检索 v14 中的消息内容
在 Discord.js v14 中,通过 message.content 访问消息内容可能会返回空值。此问题可以通过启用消息内容特权网关意图来解决。
启用消息内容意图
- 导航到 Discord 开发者门户。
- 选择您的机器人并转到“机器人”设置。
- 在“特权网关意图”,启用“消息内容”意图。
更新 Discord.js 代码
启用意图后,更新您的 Discord。 js代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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);
|
登录后复制
补充备注
- 如果您使用的是 Discord.js v13,您还需要在开发者门户上启用消息内容意图。
- 如果您的机器人使用Discord API v10,将 MESSAGE_CONTENT 标志添加到您的意图中:
1 2 3 4 5 6 7 8 9 | const { Client, Intents } = require ( 'discord.js' );
const client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.MESSAGE_CONTENT,
],
});
|
登录后复制
以上是如何修复 Discord.js v14 中的空消息内容?的详细内容。更多信息请关注PHP中文网其他相关文章!