简体   繁体   English

尝试为 Discord bot 设置多个命令文件

[英]Trying to have multiple command files for Discord bot

Question: Is there any way to have multiple command files for a discord bot?问题:有没有办法让一个不和谐机器人拥有多个命令文件?

What I mean by "command file" is the file that contains the if/else statements and commands for the users to actually interact with.我所说的“命令文件”是包含用户实际交互的 if/else 语句和命令的文件。

What I mean by "multiple files" is making more than one of these "command files" that can respond to commands.我所说的“多个文件”是指制作多个可以响应命令的“命令文件”。

I'm trying to split my fun and admin commands into 2 separate files but currently only one is working.我试图将我的 fun 和 admin 命令分成 2 个单独的文件,但目前只有一个在工作。 I know that one of the problems lie in the package.json file where it says: "main": "index.js",我知道问题之一出在 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"
  }
}

Is there a way to connect a single package.json file to multiple other .js files?有没有办法将单个 package.json 文件连接到多个其他 .js 文件? And if so, is that all I need to do to get multiple discord bot .js files to start working?如果是这样,我需要做的就是让多个 discord bot .js 文件开始工作吗?

You don't necessarily need to edit the package .json.您不一定需要编辑包 .json。

To use multiple files, you can simply do the following:要使用多个文件,您只需执行以下操作:

(commandfile1.js) (commandfile1.js)

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

(mainfile.js) (主文件.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(arguments) //usually just .run()
      } 
    }
  }
}

This is one way to do it, all you have to do is export the separate command file as an object, and make every command a separate entry in the object.这是一种方法,您所要做的就是将单独的命令文件导出为一个对象,并使每个命令成为对象中的一个单独条目。 Then every time a message is sent with your bots' prefix in front of it, cycle through all of the commands and run the one the user specified.然后每次发送带有机器人前缀的消息时,循环执行所有命令并运行用户指定的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM