【问题标题】:How do I fix ReferenceError: command is not defined如何修复 ReferenceError:未定义命令
【发布时间】:2021-08-24 18:51:48
【问题描述】:

我正在尝试制作一个不和谐的机器人,但我收到错误“ReferenceError: command is not defined”我不明白为什么,有人可以帮忙吗?

这是我的代码:

console.clear();
    
const Discord = require('discord.js');
    
const client = new Discord.Client({ intents: ['GUILDS', 'GUILD_MESSAGES']});

const Client = require("./Structures/Client.js");

const config = require("./Data/config.json");

client.commands = new Discord.Collection();

const fs = require("fs");

fs.readdirSync("./src/Commands")
  .filter((file) => file.endsWith(".js"))
  .forEach((file) => {
    /**
     * @type {Command}
     */
    const commands = require(`./Commands/${file}`);
    console.log(`Command ${command.name} loaded`);
    client.commands.set(command.name, command);
  });

client.on("ready", () => console.log("SpidBot is online!"));

client.on("messageCreate", (message) => {
  //console.log(message.content);
//if (message.content == "!Spid") message.reply("Hello!");

if (!message.content.startsWith(config.prefix)) return;

const args = message.content.substring(config.prefix.length).split(/ +/);

const command = client.commands.find((cmd) => cmd.name == args[0]);
});

client.login(config.token);

这是错误:

C:\Users\Aly\Desktop\SpidBot\src\index.js:22
                  console.log(`Command ${command.name} loaded`);
                                         ^
  
  ReferenceError: command is not defined
      at C:\Users\Aly\Desktop\SpidBot\src\index.js:22:26
      at Array.forEach (<anonymous>)
      at Object.<anonymous> (C:\Users\Aly\Desktop\SpidBot\src\index.js:17:3)
      at Module._compile (node:internal/modules/cjs/loader:1101:14)
      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
      at Module.load (node:internal/modules/cjs/loader:981:32)
      at Function.Module._load (node:internal/modules/cjs/loader:822:12)
      at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
      at node:internal/main/run_main_module:17:47

似乎还有一些错误,有人可以帮我解决它吗?

【问题讨论】:

    标签: discord.js


    【解决方案1】:

    你不小心拼错了第 21 行的变量名:

    const commands = require(`./Commands/${file}`);
    

    您将变量命名为“commands”。只需删除“s”,它应该可以工作。

    应该是这样的:

    const command = require(`./Commands/${file}`);
    

    【讨论】:

    • @Spademin 我说的是“const commands”,不应该是“const command”吗?
    • 哦,抱歉,我刚刚试了一下,现在可以了,非常感谢!
    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2020-07-24
    相关资源
    最近更新 更多