[英]ReferenceError: Discord is not defined; just when trying to kick
I can't kick anyone with my discord bot because os this happening, idk why.我不能用我的 discord 机器人踢任何人,因为这发生了,我知道为什么。 This is what I get in my console:
这是我在控制台中得到的:
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)
And this is part of the code:这是代码的一部分:
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);
}
And this is the kick command that doesn't work (it only breaks when it should ban someone, the rest works):这是无效的踢命令(它只在应该禁止某人时才中断,rest 有效):
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)
},
};
If you haven't redacted anything from the kick command script, and copied it here as-is, you're missing the require statement for the Discord class.如果您没有从 kick 命令脚本中编辑任何内容,并将其按原样复制到此处,则您缺少 Discord class 的 require 语句。
Try importing it like const Discord = require('discord.js');
尝试像
const Discord = require('discord.js');
一样导入它like you did in the first code block.就像你在第一个代码块中所做的那样。
Try defining discord inside of the kick file,尝试在 kick 文件中定义 discord,
const Discord = require("Discord")
module.exports..........
Or Call it in on exports.execute或者在exports.execute上调用它
module.exports = {
name: 'kick',
description: 'kicks the mentioned user',
execute(client, message, args, Discord){}
}
if you call it in the parameters you must also apply it to the command handler如果您在参数中调用它,则还必须将其应用于命令处理程序
command.execute(client, message, args, Discord)
Discord isn't defined in the command file, as the other answers have said.正如其他答案所说,Discord 没有在命令文件中定义。 If you want to save a few lines, just put this in your
index.js
file.如果您想保存几行,只需将其放入您的
index.js
文件中。
global.Discord = require('discord.js')
I'm not sure what node version is required for this, but it should work if you are on the LTS.我不确定这需要什么节点版本,但如果你在 LTS 上它应该可以工作。 In this error, the error was pretty descriptive and stack overflow shouldn't be needed.
在此错误中,该错误非常具有描述性,因此不需要堆栈溢出。 Try reading the error and figuring it out next time.
尝试阅读错误并在下次解决它。 It clearly said it wasn't defined.
它明确表示它没有定义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.