【问题标题】:Discord Bot - ReferenceError: Intents is not definedDiscord Bot - ReferenceError:未定义意图
【发布时间】:2021-10-28 12:11:32
【问题描述】:

我正在尝试为我的服务器编写一个简单的机器人,但发生了这个错误:

ReferenceError: Intents 未定义

我的代码很简单:

const Discord = require('discord.js');
const bot = new Discord.Client();

bot.login('TOKENcode');

bot.on("message", (message) =>{
    console.log('hello world!');
})

我该怎么办?我只想在 Discord 上写一条消息,让我的 Visual Studio Code 提示说“hello world!”

【问题讨论】:

    标签: javascript json android-intent discord bots


    【解决方案1】:

    根据Discord.js documentation你需要将Intents传递给你client初始化。为机器人开发者设置了新的 Discord 规则,每个机器人创建者都需要传递意图。

    例如,这是一个简单的构造,从文档中复制:

    // Define Client and Intents
    const { Client, Intents } = require('discord.js');
    
    // Set flags for your bot
    // All flags: https://discord.js.org/#/docs/main/stable/class/Intents?scrollTo=s-FLAGS
    const client = new Client({
        intents: [
            Intents.FLAGS.GUILDS,
            Intents.FLAGS.GUILD_MESSAGES
        ]
    });
    
    
    client.on('ready', () => {
        console.log(`Logged in as ${client.user.tag}!`);
    });
    
    // Receive messages sent in your server.
    client.on("messageCreate", message => {
        console.log('Hello World!');
    });
    
    client.login('token');
    

    ! 请注意,Discord.js 版本 13 需要在您的计算机上安装 Node.js v16。通过在控制台中输入 node -v 确保您已安装此软件。

    【讨论】:

      猜你喜欢
      • 2021-04-16
      • 1970-01-01
      • 2019-03-25
      • 2022-01-13
      • 2021-12-20
      • 2021-03-26
      • 2021-08-07
      • 2022-01-05
      • 2020-02-20
      相关资源
      最近更新 更多