discordjs / guide

The official guide for discord.js, created and maintained by core members of its community.
https://discordjs.guide
MIT License
1.57k stars 2.3k forks source link

client can't be used within the command files? #287

Closed ghost closed 5 years ago

ghost commented 5 years ago

ReferenceError: bot is not defined at Object.execute (C:\Users\serge\source\repos\DiscordMusicBot\DiscordMusicBot\Commands\ping.js:8:46) at Client.<anonymous> (C:\Users\serge\source\repos\DiscordMusicBot\DiscordMusicBot\app.js:75:35) at Client.emit (events.js:210:5) at MessageCreateHandler.handle (C:\Users\serge\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34) at WebSocketPacketManager.handle (C:\Users\serge\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65) at WebSocketConnection.onPacket (C:\Users\serge\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35) at WebSocketConnection.onMessage (C:\Users\serge\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17) at WebSocket.onMessage (C:\Users\serge\node_modules\ws\lib\event-target.js:120:16) at WebSocket.emit (events.js:210:5) at Receiver.receiverOnMessage (C:\Users\serge\node_modules\ws\lib\websocket.js:789:20)

bot is the name for my client. It cannot be used within the ping.js file. Any help is appreciated with this

ghost commented 5 years ago

This is the code I have inside the ping command. I'm tidying my bot up by having individual files for commands instead of them all in one file

const discord = require("discord.js");

module.exports = {
    name: 'ping',
    description: 'Ping!',
    execute(message, args) {
        return message.channel.sendEmbed(new discord.RichEmbed()
            .setTitle(":ping_pong: Pong! " + bot.ping.toFixed() + " ms.")
            .setColor(config.favoritehexcolor))
    },
};
almostSouji commented 5 years ago

you can access the client via message.client (for other events that same principle applies to other discord.js structures), we don't pass the client to the command file scope for this reason.

reference: https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=client