2017-10-08 64 views
0

所以我一直在使用Discord.JS库处理一个不和机器人,我遇到了一个问题。很确定这个问题更多地涉及Javascript和Discord.JS,所以我会在这里问一下,希望得到一些帮助。Discord Bot | DiscordJS | Javascript

我有一个名为commandManager.js的文件,它包含我所有的基本命令功能。其中之一是自动加载/ commands /文件夹中的命令,并根据它们的类别(通过导出在命令中指定)将它们分配给一个数组。

global.userCommands = {}; 
global.modCommands = {}; 
global.adminCommands = {}; 
global.ownerCommands = {}; 

exports.init = function(bot) 
{ 
    fs.readdir("./commands/", (error, files) => 
    { 
     if (error) 
     { 
      logger.error(error); 
     } 

     files.forEach(file => 
     { 
      let commandFile = require(`../commands/${file}`); 
      let commandName = file.split(".")[0]; 
      if (commandFile.info.category == "User") 
      { 
       userCommands[commandName] = commandFile; 
      } 
      else if (commandFile.info.category == "Mod") 
      { 
       modCommands[commandName] = commandFile; 
      } 
      else if (commandFile.info.category == "Admin") 
      { 
       adminCommands[commandName] = commandFile; 
      } 
      else if (commandFile.info.category == "Owner") 
      { 
       ownerCommands[commandName] = commandFile; 
      } 
      else 
      { 
       logger.warn("Could not add the command " + commandName + " to any of the categories"); 
      } 
     }); 

     logger.info("Loaded " + files.length + " command(s)"); 
    }); 
} 

然后在此之后,我可以在消息比特实际的机器人,我做了如下使用命令:

exports.run = function(bot, msg) 
{ 
    const args = msg.content.slice(config.prefix.length).trim().split(/ +/g); 
    const command = args.shift().toLowerCase(); 
    const cleanCommand = command.slice(config.prefix.length); 

    if (msg.author.bot) 
    { 
     return; 
    } 
    else if (msg.content.indexOf(config.prefix) !== 0) 
    { 
     return; 
    } 
    else if (has.call(userCommands, cleanCommand)) 
    { 
     msg.reply("user"); 
    } 
    else if (has.call(modCommands, cleanCommand)) 
    { 

    } 
    else if (has.call(adminCommands, cleanCommand)) 
    { 

    } 
    else if (has.call(ownerCommands, cleanCommand)) 
    { 

    } 
    else 
    { 
     msg.reply(`that command does not even exist! You can say ${config.prefix}help for a list of commands!`); 
    } 
} 

所以当我说的命令,例如“平”它应该回复msg.reply(“用户”),但它表示它不存在任何。我宣称已经像这样一个全球性的事情,因此你很好奇。

global.has = Object.prototype.hasOwnProperty; 

如果你想看到它是如下命令:

exports.info = 
{ 
    name: "Ping", 
    permission: 10, 
    category: "User", 
    about: "Makes the bot respond with pong, useful to see if the bot is working." 
}; 

exports.run = function(msg) 
{ 
    msg.channel.send("Pong!"); 
} 

任何提示,引用的例子,或只是普通的勺子喂养是100%的欢迎。另外,如果你想分享一些更好的技术来做我正在做的事情,请让我知道,因为我只是JS的初学者。

+0

为什么不'userCommands [cleanCommand]'?而类别应该是什么? –

+1

那么,如果我将来要做一个命令列表,那么类别就是我将它们分开的想法。不知道为什么不诚实与你。任何类别的更好的想法,或我可以做什么,而不是得到相同的结果? –

回答

0

以下是完全基于意见了......

我敢打赌,你想使用嵌套的命令也如播放音乐播放视频,所以在我看来一个命令树将成为这里的路。由于这些也不需要解析文件夹中的命令,相反我们可以有一个更独立的结构。启动文件应该是这样的:

module.exports = (sub, exit) => ({ 
    //a global handler applied to all commands: 
    [sub]:(user)=>"Hi there!", 
    //a main exit point (if a command cannot be found) 
    [exit]:{ 
    run:(user,command)=>"Sorry ${command} doesnt exist" 
    }, 
    //commands 
    play: { 
    //the following is applied to "play whatever" 
    [exit]:{ 
     desc:"a random command", 
     run:(user, ...args) => args.join(" "), 
     category:"random" 
    }, 
    //the following is applied to all subcommands 
    [sub]:(user)=> user.admin?"start playing":new Error("you must be an admin"), 

    //the subcommands 
    video:{ 
     //sub sub commands maybe 
     [exit]: { 
     info: "to play videos", 
     run(user, videoname)=> videoname+".mp4" 
     } 
    }, 
    //one can also use require here 
    whatever: require("./whatever.js")(sub,exit) 
}); 

有了这种模式能够(通过[sub])申请代码一大堆命令和命令可以用子等(通过[exit])运行。现在让我们来实现路由:

const exit = Symbol("exit"), sub = Symbol("sub"); 
//requiring the above tree 
const routes = require("./commands.js")(exit,sub); 
//implementing the router: 
function route(tree, user, params){ 
    var res; 
    //the [sub] implementation 
    if(tree[sub]){ 
    res = tree[sub](user, ...params); 
    if(res instanceof Error){ 
     //if an error applied exit now 
     return "An error occured:"+res; 
    } 
    } 

    if(tree[ params[0] ]){ 
    return res + "\n" + route(tree[ params[0] , user, params.slice(1)); 
    } else { 
    return res + " \n" + tree[exit].run(user, params); 
    } 
} 

所以要让它运行:

msg.reply(
    route(
    routes, 
    {admin:true, /*...*/ }, 
    msg.content.split(" ") 
) 
); 

您仍然可以延长,与每个类别的处理程序等