{"_id":"57a7ee9aa0ac880e00eb8bec","project":"57858bff9e80c32200234f4c","user":"57858823fc4a0f2200f02f01","version":{"_id":"57858bff9e80c32200234f4f","__v":3,"project":"57858bff9e80c32200234f4c","createdAt":"2016-07-13T00:31:59.708Z","releaseDate":"2016-07-13T00:31:59.708Z","categories":["57858bff9e80c32200234f50","57858e1b8a711d2000d1252b","5785a2bf2eca4e0e002b4d67"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"Stable. I Hope.","version_clean":"0.3.0","version":"0.3.00"},"__v":0,"category":{"_id":"57858e1b8a711d2000d1252b","version":"57858bff9e80c32200234f4f","project":"57858bff9e80c32200234f4c","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-07-13T00:40:59.463Z","from_sync":false,"order":1,"slug":"managers","title":"Core Managers"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-08-08T02:29:46.236Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"settings":"","results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":999,"body":"[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Info\"\n}\n[/block]\n**Name** - `CommandHandler`\n**Options** :\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"key\",\n    \"h-1\": \"type\",\n    \"h-2\": \"desc\",\n    \"0-0\": \"prefix\",\n    \"0-1\": \"string\",\n    \"0-2\": \"Prefix to identify commands\"\n  },\n  \"cols\": 3,\n  \"rows\": 1\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Commands\"\n}\n[/block]\nThese are the default commands include.\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Command\",\n    \"h-1\": \"Run\",\n    \"h-2\": \"Context\",\n    \"h-3\": \"Desc\",\n    \"h-4\": \"Usage\"\n  },\n  \"cols\": 5,\n  \"rows\": 1\n}\n[/block]\nThis Function is used internally to AudioPlayer for the commands playFile and stopPlayinghttps://dash.readme.io/project/disnode/v0.3.00/docs/audioplayer\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Config\"\n}\n[/block]\nDefault Config Params\n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"name\",\n    \"h-1\": \"type\",\n    \"h-2\": \"desc\",\n    \"0-0\": \"commands\",\n    \"0-1\": \"Array<CommandObjects>\",\n    \"0-2\": \"Default Commands\"\n  },\n  \"cols\": 3,\n  \"rows\": 1\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Source Code\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\\"use strict\\\"\\n// Command Handler Controls and Parses Every Message against a list of Commands/\\n// Consider the dispatcher of your app\\nconst colors = require('colors');\\nclass CommandHandler{\\n  // Set Inital Varibles\\n  // Prefix: The Command Prefix for this list.\\n  // List: List of Command Objects\\n\\n  constructor(options){\\n\\n    this.prefix = options.prefix;\\n    this.contexts = [];\\n    this.list = [];\\n    this.disnode = options.disnode;\\n    console.log(\\\"[CommandHandler]\\\".grey + \\\" Loaded!\\\".green);\\n  }\\n\\n  LoadList(newList){\\n\\n    console.log(\\\"[CommandHandler]\\\".grey + \\\" Loading Commands...\\\".cyan);\\n    var self = this;\\n\\n    for (var i = 0; i < newList.length; i++) {\\n\\n      var currentCmd = newList[i];\\n\\n      var SUCCESS = true;\\n      var FailReason;\\n      // RUN: context[commandObject.run]({msg: msg, params:GetParmas(msgContent)});\\n      var Context = GetContextByName(self.contexts, currentCmd.context);\\n      if(Context){\\n\\n        if(currentCmd.require){\\n          if(CheckRequirements(Context.obj, currentCmd.require)){\\n            self.list.push(currentCmd);\\n            SUCCESS = true;\\n          }else{\\n            SUCCESS = false;\\n            FailReason = \\\"Missing Requirements: \\\" + currentCmd.require;\\n          }\\n        }else{\\n          self.list.push(currentCmd);\\n          SUCCESS = true;\\n\\n        }\\n      }else{\\n        SUCCESS = false;\\n          FailReason = \\\"No Context: \\\" + currentCmd.context;\\n      }\\n\\n      if(SUCCESS)\\n      {\\n        console.log(\\\"[CommandHandler]\\\".grey + colors.green(\\\" Adding Command (\\\"+currentCmd.cmd+\\\") SUCCESSFUL!\\\"));\\n      }else{\\n        console.log(\\\"[CommandHandler]\\\".grey + colors.red(\\\" Adding Command (\\\"+currentCmd.cmd+\\\") FAILED: \\\" + FailReason));\\n      }\\n\\n    }\\n  }\\n\\n  AddContext(context, name){\\n    var self = this;\\n    self.contexts.push({name: name, obj: context});\\n    console.log(\\\"[CommandHandler]\\\".grey + \\\" Adding new Context: \\\".cyan + name);\\n  }\\n\\n  AddCommand(currentCmd)\\n  {\\n    var self = this;\\n\\n    var SUCCESS = true;\\n    var FailReason;\\n    // RUN: context[commandObject.run]({msg: msg, params:GetParmas(msgContent)});\\n    var Context = GetContextByName(self.contexts, currentCmd.context);\\n    if(Context){\\n\\n      if(currentCmd.require){\\n        if(CheckRequirements(Context.obj, currentCmd.require)){\\n          self.list.push(currentCmd);\\n          SUCCESS = true;\\n        }else{\\n          SUCCESS = false;\\n          FailReason = \\\"Missing Requirements: \\\" + currentCmd.require;\\n        }\\n      }else{\\n        self.list.push(currentCmd);\\n        SUCCESS = true;\\n\\n      }\\n    }else{\\n      SUCCESS = false;\\n        FailReason = \\\"No Context: \\\" + currentCmd.context;\\n    }\\n\\n    if(SUCCESS)\\n    {\\n      console.log(\\\"[CommandHandler]\\\".grey + colors.green(\\\" Adding Command (\\\"+currentCmd.cmd+\\\") SUCCESSFUL!\\\"));\\n    }else{\\n      console.log(\\\"[CommandHandler]\\\".grey + colors.red(\\\" Adding Command (\\\"+currentCmd.cmd+\\\") FAILED: \\\" + FailReason));\\n    }\\n\\n  }\\n  // Parse the message and run any commands it contains\\n  RunMessage(msg){\\n    // Get the prefix\\n    var self = this;\\n    var msgContent = msg.content;\\n    var firstLetter = msgContent.substring(0,1)\\n\\n    if(msgContent == \\\"!VICTORY\\\"){\\n      self.disnode.bot.sendMessage(msg.channel, \\\"VictoryForFire!\\\");\\n    }\\n    // Check if it is the prefix, else ignore\\n    if(firstLetter == this.prefix){\\n      var command = \\\"\\\";\\n      // Check if the message has a space, require for command parsing\\n      if(CheckSpace(msgContent)){\\n        // Get command string as anything before the first space\\n        command = msgContent.substring(1,msgContent.indexOf(\\\" \\\"));\\n        console.log(command);\\n      }else {\\n        // Get the command as just the string (minus the prefix)\\n        command = msgContent.substring(1);\\n      }\\n\\n      // Check if command is registered\\n      if(CheckForCommand(command, this.list)){\\n        // Get the command\\n        var commandObject = GetCommand(command, this.list);\\n        // Run the command\\n        console.log(commandObject.run);\\n\\n        var context = GetContextByName(self.contexts,commandObject.context).obj;\\n        if(context){\\n          if(commandObject.params){\\n            if(commandObject.role){\\n              if(CheckRole(commandObject.role, msg.channel.server.rolesOfUser(msg.author))){\\n                context[commandObject.run]({msg: msg, params:GetParmas(msgContent)}, commandObject.params);\\n              }else{\\n                self.disnode.bot.sendMessage(msg.channel, \\\"Do Not Have Role: \\\" + commandObject.role);\\n              }\\n            }else{\\n              context[commandObject.run]({msg: msg, params:GetParmas(msgContent)}, commandObject.params);\\n            }\\n\\n          }else{\\n            if(commandObject.role){\\n              if(CheckRole(commandObject.role, msg.channel.server.rolesOfUser(msg.author))){\\n                context[commandObject.run]({msg: msg, params:GetParmas(msgContent)}, commandObject);\\n              }else{\\n                self.disnode.bot.sendMessage(msg.channel, \\\"Do Not Have Role: \\\" + commandObject.role);\\n              }\\n            }else{\\n              context[commandObject.run]({msg: msg, params:GetParmas(msgContent)}, commandObject);\\n            }\\n          }\\n\\n        }\\n      }\\n    }\\n  }\\n}\\n\\nfunction CheckRole(role, roles){\\n  var found;\\n  for (var i = 0; i < roles.length; i++) {\\n    if(roles[i].name == role){\\n      return true;\\n    }\\n  }\\n  return found;\\n}\\n\\nfunction CheckRequirements(context, requirements){\\n  var foundAllRequires = true;\\n\\n  for(var i=0;i<requirements.length;i++){\\n    if(!context[requirements[i]]){\\n      foundAllRequires = false;\\n    }\\n  }\\n\\n  return foundAllRequires;\\n}\\n\\nfunction GetContextByName(list, name){\\n  var found;\\n  for (var i = 0; i < list.length; i++) {\\n    if(list[i].name == name){\\n      found =list[i];\\n    }\\n  }\\n\\n  return found;\\n}\\n\\nfunction CheckSpace(toCheck){\\n  if(toCheck.indexOf(\\\" \\\") != -1){\\n    return true;\\n  }\\n  else{\\n    return false;\\n  }\\n}\\n\\nfunction CheckForCommand(toSearch, list){\\n  for (var i = 0; i < list.length; i++) {\\n    if(list[i].cmd == toSearch){\\n      return true;\\n    }\\n  }\\n  return false;\\n}\\n\\nfunction GetCommand(toSearch, list){\\n  var returnCommand;\\n  for (var i = 0; i < list.length; i++) {\\n    if(list[i].cmd == toSearch){\\n      returnCommand = list[i];\\n    }\\n  }\\n  return returnCommand;\\n}\\n\\nfunction GetParmas(raw){\\n  var parms = [];\\n  var lastSpace = -1;\\n  var end = false;\\n  while(!end){\\n\\n\\n    var BeginSpace = raw.indexOf(\\\" \\\", lastSpace);\\n    var EndSpace = -1;\\n    if(BeginSpace != -1){\\n       EndSpace = raw.indexOf(\\\" \\\", BeginSpace + 1);\\n\\n\\n       if(EndSpace == -1){\\n         EndSpace = raw.length;\\n         end = true;\\n       }\\n\\n       var param = raw.substring(BeginSpace + 1, EndSpace);\\n       var containsQuoteIndex = param.indexOf('\\\"');\\n\\n\\n\\n       var BeginQuote = -1;\\n       var EndQuote = -1;\\n       if(containsQuoteIndex != -1){\\n         BeginQuote = raw.indexOf('\\\"', BeginSpace);\\n\\n\\n         EndQuote = raw.indexOf('\\\"', BeginQuote + 1);\\n\\n         if(EndQuote != -1){\\n           BeginSpace = BeginQuote;\\n           EndSpace = EndQuote;\\n           param = raw.substring(BeginSpace + 1, EndSpace);\\n\\n\\n           console.log(\\\" \\\");\\n         }\\n       }\\n\\n       lastSpace = EndSpace;\\n\\n       if(param != \\\"\\\"){\\n         parms.push(param);\\n       }else{\\n\\n       }\\n\\n\\n\\n    }else{\\n      end = true;\\n    }\\n  }\\n  return parms;\\n}\\nmodule.exports = CommandHandler;\\n\",\n      \"language\": \"javascript\",\n      \"name\": \"ManagerSource\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"commandhandler","type":"basic","title":"CommandHandler"}
[block:api-header] { "type": "basic", "title": "Info" } [/block] **Name** - `CommandHandler` **Options** : [block:parameters] { "data": { "h-0": "key", "h-1": "type", "h-2": "desc", "0-0": "prefix", "0-1": "string", "0-2": "Prefix to identify commands" }, "cols": 3, "rows": 1 } [/block] [block:api-header] { "type": "basic", "title": "Commands" } [/block] These are the default commands include. [block:parameters] { "data": { "h-0": "Command", "h-1": "Run", "h-2": "Context", "h-3": "Desc", "h-4": "Usage" }, "cols": 5, "rows": 1 } [/block] This Function is used internally to AudioPlayer for the commands playFile and stopPlayinghttps://dash.readme.io/project/disnode/v0.3.00/docs/audioplayer [block:api-header] { "type": "basic", "title": "Config" } [/block] Default Config Params [block:parameters] { "data": { "h-0": "name", "h-1": "type", "h-2": "desc", "0-0": "commands", "0-1": "Array<CommandObjects>", "0-2": "Default Commands" }, "cols": 3, "rows": 1 } [/block] [block:api-header] { "type": "basic", "title": "Source Code" } [/block] [block:code] { "codes": [ { "code": "\"use strict\"\n// Command Handler Controls and Parses Every Message against a list of Commands/\n// Consider the dispatcher of your app\nconst colors = require('colors');\nclass CommandHandler{\n // Set Inital Varibles\n // Prefix: The Command Prefix for this list.\n // List: List of Command Objects\n\n constructor(options){\n\n this.prefix = options.prefix;\n this.contexts = [];\n this.list = [];\n this.disnode = options.disnode;\n console.log(\"[CommandHandler]\".grey + \" Loaded!\".green);\n }\n\n LoadList(newList){\n\n console.log(\"[CommandHandler]\".grey + \" Loading Commands...\".cyan);\n var self = this;\n\n for (var i = 0; i < newList.length; i++) {\n\n var currentCmd = newList[i];\n\n var SUCCESS = true;\n var FailReason;\n // RUN: context[commandObject.run]({msg: msg, params:GetParmas(msgContent)});\n var Context = GetContextByName(self.contexts, currentCmd.context);\n if(Context){\n\n if(currentCmd.require){\n if(CheckRequirements(Context.obj, currentCmd.require)){\n self.list.push(currentCmd);\n SUCCESS = true;\n }else{\n SUCCESS = false;\n FailReason = \"Missing Requirements: \" + currentCmd.require;\n }\n }else{\n self.list.push(currentCmd);\n SUCCESS = true;\n\n }\n }else{\n SUCCESS = false;\n FailReason = \"No Context: \" + currentCmd.context;\n }\n\n if(SUCCESS)\n {\n console.log(\"[CommandHandler]\".grey + colors.green(\" Adding Command (\"+currentCmd.cmd+\") SUCCESSFUL!\"));\n }else{\n console.log(\"[CommandHandler]\".grey + colors.red(\" Adding Command (\"+currentCmd.cmd+\") FAILED: \" + FailReason));\n }\n\n }\n }\n\n AddContext(context, name){\n var self = this;\n self.contexts.push({name: name, obj: context});\n console.log(\"[CommandHandler]\".grey + \" Adding new Context: \".cyan + name);\n }\n\n AddCommand(currentCmd)\n {\n var self = this;\n\n var SUCCESS = true;\n var FailReason;\n // RUN: context[commandObject.run]({msg: msg, params:GetParmas(msgContent)});\n var Context = GetContextByName(self.contexts, currentCmd.context);\n if(Context){\n\n if(currentCmd.require){\n if(CheckRequirements(Context.obj, currentCmd.require)){\n self.list.push(currentCmd);\n SUCCESS = true;\n }else{\n SUCCESS = false;\n FailReason = \"Missing Requirements: \" + currentCmd.require;\n }\n }else{\n self.list.push(currentCmd);\n SUCCESS = true;\n\n }\n }else{\n SUCCESS = false;\n FailReason = \"No Context: \" + currentCmd.context;\n }\n\n if(SUCCESS)\n {\n console.log(\"[CommandHandler]\".grey + colors.green(\" Adding Command (\"+currentCmd.cmd+\") SUCCESSFUL!\"));\n }else{\n console.log(\"[CommandHandler]\".grey + colors.red(\" Adding Command (\"+currentCmd.cmd+\") FAILED: \" + FailReason));\n }\n\n }\n // Parse the message and run any commands it contains\n RunMessage(msg){\n // Get the prefix\n var self = this;\n var msgContent = msg.content;\n var firstLetter = msgContent.substring(0,1)\n\n if(msgContent == \"!VICTORY\"){\n self.disnode.bot.sendMessage(msg.channel, \"VictoryForFire!\");\n }\n // Check if it is the prefix, else ignore\n if(firstLetter == this.prefix){\n var command = \"\";\n // Check if the message has a space, require for command parsing\n if(CheckSpace(msgContent)){\n // Get command string as anything before the first space\n command = msgContent.substring(1,msgContent.indexOf(\" \"));\n console.log(command);\n }else {\n // Get the command as just the string (minus the prefix)\n command = msgContent.substring(1);\n }\n\n // Check if command is registered\n if(CheckForCommand(command, this.list)){\n // Get the command\n var commandObject = GetCommand(command, this.list);\n // Run the command\n console.log(commandObject.run);\n\n var context = GetContextByName(self.contexts,commandObject.context).obj;\n if(context){\n if(commandObject.params){\n if(commandObject.role){\n if(CheckRole(commandObject.role, msg.channel.server.rolesOfUser(msg.author))){\n context[commandObject.run]({msg: msg, params:GetParmas(msgContent)}, commandObject.params);\n }else{\n self.disnode.bot.sendMessage(msg.channel, \"Do Not Have Role: \" + commandObject.role);\n }\n }else{\n context[commandObject.run]({msg: msg, params:GetParmas(msgContent)}, commandObject.params);\n }\n\n }else{\n if(commandObject.role){\n if(CheckRole(commandObject.role, msg.channel.server.rolesOfUser(msg.author))){\n context[commandObject.run]({msg: msg, params:GetParmas(msgContent)}, commandObject);\n }else{\n self.disnode.bot.sendMessage(msg.channel, \"Do Not Have Role: \" + commandObject.role);\n }\n }else{\n context[commandObject.run]({msg: msg, params:GetParmas(msgContent)}, commandObject);\n }\n }\n\n }\n }\n }\n }\n}\n\nfunction CheckRole(role, roles){\n var found;\n for (var i = 0; i < roles.length; i++) {\n if(roles[i].name == role){\n return true;\n }\n }\n return found;\n}\n\nfunction CheckRequirements(context, requirements){\n var foundAllRequires = true;\n\n for(var i=0;i<requirements.length;i++){\n if(!context[requirements[i]]){\n foundAllRequires = false;\n }\n }\n\n return foundAllRequires;\n}\n\nfunction GetContextByName(list, name){\n var found;\n for (var i = 0; i < list.length; i++) {\n if(list[i].name == name){\n found =list[i];\n }\n }\n\n return found;\n}\n\nfunction CheckSpace(toCheck){\n if(toCheck.indexOf(\" \") != -1){\n return true;\n }\n else{\n return false;\n }\n}\n\nfunction CheckForCommand(toSearch, list){\n for (var i = 0; i < list.length; i++) {\n if(list[i].cmd == toSearch){\n return true;\n }\n }\n return false;\n}\n\nfunction GetCommand(toSearch, list){\n var returnCommand;\n for (var i = 0; i < list.length; i++) {\n if(list[i].cmd == toSearch){\n returnCommand = list[i];\n }\n }\n return returnCommand;\n}\n\nfunction GetParmas(raw){\n var parms = [];\n var lastSpace = -1;\n var end = false;\n while(!end){\n\n\n var BeginSpace = raw.indexOf(\" \", lastSpace);\n var EndSpace = -1;\n if(BeginSpace != -1){\n EndSpace = raw.indexOf(\" \", BeginSpace + 1);\n\n\n if(EndSpace == -1){\n EndSpace = raw.length;\n end = true;\n }\n\n var param = raw.substring(BeginSpace + 1, EndSpace);\n var containsQuoteIndex = param.indexOf('\"');\n\n\n\n var BeginQuote = -1;\n var EndQuote = -1;\n if(containsQuoteIndex != -1){\n BeginQuote = raw.indexOf('\"', BeginSpace);\n\n\n EndQuote = raw.indexOf('\"', BeginQuote + 1);\n\n if(EndQuote != -1){\n BeginSpace = BeginQuote;\n EndSpace = EndQuote;\n param = raw.substring(BeginSpace + 1, EndSpace);\n\n\n console.log(\" \");\n }\n }\n\n lastSpace = EndSpace;\n\n if(param != \"\"){\n parms.push(param);\n }else{\n\n }\n\n\n\n }else{\n end = true;\n }\n }\n return parms;\n}\nmodule.exports = CommandHandler;\n", "language": "javascript", "name": "ManagerSource" } ] } [/block]