首页 文章

试图为Discord bot提供多个命令文件

提问于
浏览
0

问题:有没有办法为discord bot提供多个命令文件?

我所说的“命令文件”是包含用户实际交互的if / else语句和命令的文件 .

我所说的“多个文件”是指不止一个可以响应命令的“命令文件” .

我正在尝试将我的趣味和管理命令分成两个单独的文件,但目前只有一个正在工作 . 我知道其中一个问题出在package.json文件中,它说:“main”:“index.js”,

{
  "name": "n00bly783",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "discord.js": "^11.2.1",
    "discord.js-commando": "^0.9.0"
  }
}

有没有办法将单个package.json文件连接到多个其他.js文件?如果是这样,我需要做的就是让多个discord bot .js文件开始工作吗?

1 回答

  • 0

    您不一定需要编辑包.json .

    要使用多个文件,您只需执行以下操作:

    (commandfile1.js)

    module.exports = {
      commandName: {
        variableToCallToRunFunction: (arguments) => { //usually called run
          //the command
        }
      }
    }
    

    (mainfile.js)

    commandfile1 = require('./commandfile1.js') //if commandfile1 is in the same folder as this file
    yourBot.on('message', msg => {
      if (msg.content.startsWith(yourPrefix){
        cmdmsg = msg.content.replace(yourprefix, '')
        for (command in commandfile1){
          if (cmdmsg.startsWith(commandfile1[command])){
            commandfile1[command].variableToCallToRunFunction(arguements) //usually just .run()
          } 
        }
      }
    }
    

    这是一种方法,您只需将单独的命令文件导出为对象,并使每个命令成为对象中的单独条目 . 然后,每次发送带有机器人前缀的消息时,循环执行所有命令并运行用户指定的命令 .

相关问题