0
votes

I can't kick anyone with my discord bot because os this happening, idk why. 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):

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

3 Answers

1
votes

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.

Try importing it like const Discord = require('discord.js'); like you did in the first code block.

0
votes

Try defining discord inside of the kick file,


const Discord = require("Discord")

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

Or Call it in on 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)
0
votes

Discord isn't defined in the command file, as the other answers have said. If you want to save a few lines, just put this in your index.js file.

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. 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.