[英]Trying to have multiple command files for Discord bot
问题:有没有办法让一个不和谐机器人拥有多个命令文件?
我所说的“命令文件”是包含用户实际交互的 if/else 语句和命令的文件。
我所说的“多个文件”是指制作多个可以响应命令的“命令文件”。
我试图将我的 fun 和 admin 命令分成 2 个单独的文件,但目前只有一个在工作。 我知道问题之一出在 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 文件开始工作吗?
您不一定需要编辑包 .json。
要使用多个文件,您只需执行以下操作:
(commandfile1.js)
module.exports = {
commandName: {
variableToCallToRunFunction: (arguments) => { //usually called run
//the command
}
}
}
(主文件.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()
}
}
}
}
这是一种方法,您所要做的就是将单独的命令文件导出为一个对象,并使每个命令成为对象中的一个单独条目。 然后每次发送带有机器人前缀的消息时,循环执行所有命令并运行用户指定的命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.