Advertisement
Guest User

Untitled

a guest
Feb 11th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. client.on('message', pMessage => {
  2.     if (executable(pMessage.content) == true) {
  3.         execute(pMessage.content, pMessage.channel).then(sResult => { pMessage.channel.send(sResult) });
  4.     }
  5. });
  6.  
  7. async function execute(sMessage, pChannel = null) {
  8.    
  9.     // strip off the initial ! that marks the message as a command; whatever comes immediately afterwards is the command
  10.     let args = sMessage.substring(1).trim().split(/ +/g);
  11.     let cmd = args[0];
  12.     // don't include the command itself among the rest of the arguments passed to it
  13.     args = args.splice(1);
  14.    
  15.     // resolve nested commands first if they exist
  16.     let sInnerCom = ""; // nested command input
  17.     let sInnerComResult = ""; // nested command output
  18.     if ((args.includes("[") == true) && (args.includes("]") == true)) { // consider anything in [ ] a nested command
  19.         let iStart = args.indexOf("[");
  20.         let iEnd = args.lastIndexOf("]");
  21.         sInnerCom = args.slice(iStart+1,iEnd).join(" ");
  22.        
  23.         if (executable(sInnerCom) == true) {
  24.             let pInnerPromise = await execute(sInnerCom, pChannel);
  25.             pInnerPromise.then(result => { sInnerComResult = result });
  26.         }
  27.        
  28.         // take all the elements of args that were used to encode the nested command
  29.         // and replace them with the output we just got from that command
  30.         // so any subsequent commands drawing from args will use it as an input string
  31.         let throwaway = args.splice(iStart, iEnd-iStart+1, sInnerComResult);
  32.     }  
  33.    
  34.     let sComResult = "";
  35.     switch(cmd){
  36.        
  37.         ...
  38.        
  39.         case "aut":
  40.             if (args.length < 1) {
  41.                 // input string that should be in args actually isn't, so grab the content of the 2nd most recent message instead
  42.                 pMessages = await pChannel.fetchMessages({ limit: 2 });
  43.                 sComResult = com_aut([pMessages.last().content]);
  44.                 });
  45.             } else {
  46.                 // input string is present in args, so just use that
  47.                 sComResult = com_aut(args);
  48.             }
  49.             break;
  50.     }
  51.     return sComResult;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement