【问题标题】:Discord.js reads Intents as undefinedDiscord.js 将 Intent 读取为未定义
【发布时间】:2022-07-20 00:57:50
【问题描述】:

我的简单测试代码

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const { MessageEmbed, channels } = require('discord.js');

client.login('no peeking');

client.on('ready', readyUser);

function readyUser(){
    console.log('Bot is ready and logged in');
}

client.on("messageCreate", (message) => {
    const prefix = "!";
    const args = message.content.substring(prefix.length).split(" ");
    const command = args.shift().toLowerCase();
    if (message.author.id != client.user.id) {
    }
});

报错

TypeError: Cannot read properties of undefined (reading 'FLAGS') 关于第 2 行。

根据我的发现,这可能是由于我的意图在 Discord 开发者门户中被禁用。即使我启用了它们,问题仍然存在。

What I found (enabled intents)

【问题讨论】:

标签: node.js discord discord.js


【解决方案1】:

在 discord.js v14 中,在客户端声明意图时需要使用GatewayIntentBits。一个例子是这样的:

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

【讨论】:

    【解决方案2】:

    如果您使用的是 discord.js v14,Intents 将不再可用。

    您需要改用GatewayIntentBits

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

    【讨论】:

      猜你喜欢
      • 2022-06-10
      • 2021-07-06
      • 2021-11-17
      • 1970-01-01
      • 2023-01-14
      • 2023-02-21
      • 2020-12-05
      • 2021-04-14
      • 2021-06-27
      相关资源
      最近更新 更多