【问题标题】:I keep getting this error in my Discord Bot我的 Discord Bot 中不断出现此错误
【发布时间】:2022-07-17 01:55:36
【问题描述】:

我是创建 Discord.js 机器人的新手,但我不断收到此错误: 机器人现已上线!

HIT
C:\Users\kerix\Desktop\MrMiner\bot\index.js:20
    let commandMethod = commands.get(name);
                        ^

ReferenceError: commands is not defined
    at Client.<anonymous> (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)

我的代码是:

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)

我该如何解决这个错误?

【问题讨论】:

  • 1.这是一个警告,而不是错误。 2. 你是否尝试过完全按照信息告诉你的去做?使用'messageCreate' 而不是'message'?
  • OP 的错误不是关于使用message 而不是messageCreate 的弃用警告。是这样的:ReferenceError: commands is not defined at Client.

标签: node.js discord.js


【解决方案1】:

您在ready 处理程序中定义变量commands,因此它只能在ready 处理程序中访问。在外部定义它以便能够在 interactionCreate 处理程序中访问它:

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)

这应该可以解决您遇到的特定错误。

【讨论】:

    猜你喜欢
    • 2020-12-17
    • 2021-10-14
    • 2022-11-27
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2022-11-28
    • 2021-03-30
    • 2021-02-16
    相关资源
    最近更新 更多