开发者问题收集

ReferenceError:Discord 未定义;就在尝试踢出时

2020-05-05
12665

我无法用我的 discord 机器人踢出任何人,因为这种情况会发生,我不知道为什么。 这是我在控制台中得到的内容:

ReferenceError: Discord is not defined
    at Object.execute (C:\Users\alber\OneDrive\Escritorio\Bot discord\commands\kick.js:17:21)
    at Client. (C:\Users\alber\OneDrive\Escritorio\Bot discord\index.js:34:15)
    at Client.emit (events.js:310:20)
    at MessageCreateAction.handle (C:\Users\alber\OneDrive\Escritorio\Bot discord\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
ebsocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\alber\OneDrive\Escritorio\Bot discord\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\Users\alber\OneDrive\Escritorio\Bot discord\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\alber\OneDrive\Escritorio\Bot discord\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\alber\OneDrive\Escritorio\Bot discord\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:310:20)

这是代码的一部分:

const fs = require('fs');
const Discord = require('discord.js')
const { prefix, token } = require('./config.json');

const client = new Discord.Client();
client.commands = new Discord.Collection();

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);
}

这是不起作用的踢出命令(它只在应该禁止某人时中断,其余的都有效):

module.exports = {
    name: 'kick',
    description: 'kicks the mentioned user',
    execute(client, message, args){
    var kickUser = message.guild.member(message.mentions.users.first() || message.guild.members.fetch(args[0]))    
    if(!kickUser){
        return message.reply('¿no tageas a nadie? Kickear paraguayos es malardo.')
    }
    var kickReason = args.join(' ').slice(22)
    if(!message.member.hasPermission(['KICK_MEMBERS'])){
        return message.reply('enséñame los permisos que los vea yo que parece que no tienes.s')
    }
    if(kickUser.hasPermission(['KICK_MEMBERS'])){
        return message.reply('no puedes echar a alguien con permisos de admin.')
    }

    var kickEmbed = new Discord.RichEmbed()    
    .setDescription('Kick')
    .setColor('#35d4a7')
    .addField('Server', guild.name)
    .addField('Kickeado por', `@${message.author.id}`)
    .addField('A las', message.createdTimestamp)
    .addField('Motivo', kickReason)

    message.guild.member(kickUser).kick(kickReason)
    kickUser.send(kickEmbed)
    },
};
3个回答

如果您还没有从kick命令脚本中编辑任何内容,并且在此处将其复制为IS,则您缺少Discord类的要求。

尝试像 一样尝试导入它const discord = require('discord.js'); 像第一个代码块一样。

Yalin Gunayer
2020-05-05

尝试在 kick 文件中定义 discord,

const Discord = require("Discord")

module.exports..........

或者在 exports.execute 中调用它

module.exports = {
    name: 'kick',
    description: 'kicks the mentioned user',
    execute(client, message, args, Discord){}
}

如果在参数中调用它,您还必须将其应用于命令处理程序

command.execute(client, message, args, Discord)
Supesu
2020-05-05

正如其他答案所说,Discord 未在命令文件中定义。如果您想节省几行,只需将其放入您的 index.js 文件中。

global.Discord = require('discord.js')

我不确定这需要什么节点版本,但如果您使用的是 LTS,它应该可以工作。 在这个错误中,错误非常具有描述性,不需要堆栈溢出。下次尝试阅读错误并找出原因。它清楚地说它没有定义。

Polygon
2021-06-01