2017-10-08 11 views
0

私はDiscord.JSライブラリを使用してDiscordボットに取り組んできましたが、問題に遭遇しました。 JavascriptとDiscord.JSの問題がより深刻であることは間違いありませんので、ここで質問して助けてください。Discord Bot | DiscordJS | Javascript

私はすべての私の基本的なコマンド機能を保持するcommandManager.jsというファイルを持っています。そのうちの1人は、/ commands /フォルダからコマンドを自動ロードし、それらのカテゴリに基づいてアレイに割り当てる(エクスポートでコマンドに指定されている)。私は、コマンドとは、例えば、「ピング」それがで返答すべきであると言うときに

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!`); 
    } 
} 

を:

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)"); 
    }); 
} 

は、この後、私は、次のとおりにしたメッセージビット上の実際のボットにコマンドを使用することができますmsg.reply( "user")しかし、それは何も存在しないと言います。私は宣言したように、あなたは好奇心を持っていました。

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(" ") 
) 
); 

をあなたはまだ、カテゴリごとのハンドラでそれを拡張することができますなど

関連する問題