'Hi Im New To Node.js And Im Making A Discord Bot And I Keep Getting This Error

Im New To Making A Discord.js Bot And I Keep Getting This Error: The Bot Is Online Now! HIT C:\Users\kerix\Desktop\MrMiner\bot\index.js:20 let commandMethod = commands.get(name); ^

ReferenceError: commands is not defined at Client. (C:\Users\kerix\Desktop\MrMiner\bot\index.js:20:25) at Client.emit (node:events:527:28) at InteractionCreateAction.handle (C:\Users\kerix\Desktop\MrMiner\bot\node_modules\discord.js\src\client\actions\InteractionCreate.js:74:12) at Object.module.exports [as INTERACTION_CREATE] (C:\Users\kerix\Desktop\MrMiner\bot\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36) at WebSocketManager.handlePacket (C:\Users\kerix\Desktop\MrMiner\bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31) at WebSocketShard.onPacket (C:\Users\kerix\Desktop\MrMiner\bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22) at WebSocketShard.onMessage (C:\Users\kerix\Desktop\MrMiner\bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10) at WebSocket.onMessage (C:\Users\kerix\Desktop\MrMiner\bot\node_modules\ws\lib\event-target.js:199:18) at WebSocket.emit (node:events:527:28) at Receiver.receiverOnMessage (C:\Users\kerix\Desktop\MrMiner\bot\node_modules\ws\lib\websocket.js:1137:20)

My Code Is:

const Discord = require("discord.js");
const { token } = require('./config.js');
const Bot = new Discord.Client({intents: [Discord.Intents.FLAGS.GUILD_MEMBERS, Discord.Intents.FLAGS.GUILDS]})
require("./slash-register")()

Bot.on('ready', () => {
    console.log("The Bot Is Online Now!")

    let commands = Bot.application.commands;

})

Bot.on('interactionCreate',async interaction => {
    console.log("HIT")
    if(!interaction.isCommand) return;
    let name = interaction.commandName;
    let options = interaction.options;

    let commandMethod = commands.get(name);
    if(!commandMethod) return;

    await interaction.deferReply();

    commandMethod(Bot, interaction)
})

Bot.login(token)

Please Help Me Fix This Error.



Solution 1:[1]

You are defining the variable commands in the ready handler, so it will only be accessible in the ready handler. Define it outside to be able to access it in the interactionCreate handler:

let commands;

Bot.on('ready', () => {
    console.log("The Bot Is Online Now!")

    commands = Bot.application.commands;

})

Bot.on('interactionCreate',async interaction => {
    console.log("HIT")
    if(!interaction.isCommand()) return;
    let name = interaction.commandName;
    let options = interaction.options;

    let commandMethod = commands.get(name);
    if(!commandMethod) return;

    await interaction.deferReply();

    commandMethod(Bot, interaction)
})

Bot.login(token)

That should fix the specific error you are getting.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cannicide