繁体   English   中英

每当我尝试启动我的 discord 机器人时,我都会收到一条错误消息:ReferenceError: Discord is not defined

[英]Whenever I try to start my discord bot I get an error that says: ReferenceError: Discord is not defined

我正在使用命令处理程序处理这个 discord 机器人,所有命令都在它们自己的文件中,我导入到主文件中

这是我的代码:

const { Client, Intents } = require("discord.js");

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.commands = new Discord.Collection();
const fs = require('fs');
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))
for(const file of commandFiles){
    const command = require(`./commands/${file}`)
    client.commands.set(command.name, command) 
}

const { prefix, token } = require('./config.json')

client.once('ready', () => {
    console.log('BOT IS READY')
})

client.on('message', message => {
    if(!message.content.startsWith(prefix)||message.author.bot) return

    const args = message.content.slice(prefix.length).split(/ +/)
    const command = args.shift().toLowerCase()

    if(command === 'charlongolo'){
        client.commands.get('charlongolo').execute(message, args)
    } else if(command === 'timster'){
        client.commands.get('timster').execute(message, args)
    }
})

client.login(token);

错误在于您如何导入disord.js然后使用它,因此有两种方法可以 go 解决此问题:您可以导入整个模块,也可以通过 object 解构导入。

整个模块

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

Object 解构

const { Client, Intents, Collection } = require("discord.js");
...
const client = new Client({
    intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
});
client.commands = new Collection();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM