2017-10-08 90 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(" ") 
) 
); 

您仍然可以延長,與每個類別的處理程序等