【问题标题】:Error with Discord bot change prefix command (discord.js)Discord bot 更改前缀命令出错 (discord.js)
【发布时间】:2018-01-03 00:58:15
【问题描述】:

我有一个问题,我真的不知道如何解决。当我尝试在我的代码中实现更改前缀命令时,我想使用它时遇到错误。首先,代码都在一个文件中并且工作正常,但是我拆分了文件,以便命令每个都有不同的文件。这是代码不再起作用的地方。我确定这是因为我不擅长 javascript,但请帮助我!

这是主文件:

const Discord = require("discord.js");
const config = require("./config.json");
const client = new Discord.Client();
const newUsers = [];
const fs = require("fs")

// Error en warning berichten.
client.on("error", (e) => console.error(e));
client.on("warn", (e) => console.warn(e));


// This loop reads the /events/ folder and attaches each event file to the appropriate event.
fs.readdir("./events/", (err, files) => {
  if (err) return console.error(err);
  files.forEach(file => {
    let eventFunction = require(`./events/${file}`);
    let eventName = file.split(".")[0];
    // super-secret recipe to call events with all their proper arguments *after* the `client` var.
    client.on(eventName, (...args) => eventFunction.run(client, ...args));
  });
});

client.on("message", message => {
  if (message.author.bot) return;
  if(message.content.indexOf(config.prefix) !== 0) return;

  // This is the best way to define args. Trust me.
  const args = message.content.split(/\s+/g);
  const command = args.shift().slice(config.prefix.length).toLowerCase();

  // The list of if/else is replaced with those simple 2 lines:
  try {
    let commandFile = require(`./commands/${command}.js`);
    commandFile.run(client, message, args);
  } catch (err) {
    console.error(err);
  }
});

exports.run = (client, message, args) => {
  if(!args || args.size < 1) return message.reply("Must provide a command name to reload.");
  // the path is relative to the *current folder*, so just ./filename.js
  delete require.cache[require.resolve(`./${args[0]}.js`)];
  message.reply(`The command ${args[0]} has been reloaded`);
};

client.login(config.token);

这是前缀文件:

exports.run = (client, message, args) => {
  const modRole = message.guild.roles.find("name", "Discord Admin");
  if (!modRole) 
    return console.log("The Discord Admin role does not exist.");

  if (!message.member.roles.has(modRole.id))
    return message.reply("You can't use this command.");

   // Verandert de prefix van de command (vb. "!prefix +" ontvangt de "+" dus word het "+prefix").
      let newPrefix = message.content.split(" ").slice(1, 2)[0];
      // Verandert de configuratie in memory
      config.prefix = newPrefix;
      // Slaat het bestand op.
      fs.writeFile("./config.json", JSON.stringify(config), (err) => console.error);
}

并且在配置文件中是:"prefix":"!"

我确实尝试了很多东西,但我在 node.js 中保留了这个错误:

ReferenceError: 配置未定义 在 Object.exports.run (C:\Users\fleur\Desktop\botdiscord\commands\prefix.js:12:7)

【问题讨论】:

    标签: node.js command prefix discord


    【解决方案1】:

    您忘记在commands\prefix.js 中要求config

    如果你想在 node.js 中有一个全局变量,你必须将它分配给全局范围。

    看这篇文章node.js global variables?

    【讨论】:

    • 我需要在这个文件夹中有 config.json 文件prntscr.com/g0xob0 有没有一种方法可以让我执行 require 命令,即使它不在命令文件夹中?
    • 你可以要求它的相对路径:const config = require('../config.json')
    猜你喜欢
    • 2020-10-30
    • 2021-09-10
    • 2023-04-04
    • 2018-09-17
    • 2021-04-07
    • 2020-08-26
    • 2020-05-28
    • 1970-01-01
    • 2018-03-04
    相关资源
    最近更新 更多