Guest User

Code

a guest
May 30th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Discord = require('discord.js')
  2. var client = new Discord.Client()
  3. var cmds = {}
  4. var errorlvl
  5.  
  6. var prefix = '!'
  7. function weAreReady() {
  8.     console.log('The bot has successfully logged in.')
  9. }
  10.  
  11. function addCmd(name, argNumber, cmd, usage) {
  12.     cmds[name] = {name:name,func: cmd, usage: usage,argNum: argNumber}
  13. }
  14.  
  15. client.on('ready', weAreReady)
  16. client.on('message', function (message) {
  17.    
  18.     if (message.content.startsWith(prefix)) {
  19.         cmd = message.content.split(prefix).pop()
  20.         actualcmd = cmd.split('_').pop()
  21.         var args
  22.         if (cmds[actualcmd] == undefined) {
  23.             //It is not a vailid command
  24.             console.log('invalid cmd')
  25.             console.log('cmd: ' + actualcmd)
  26.         } else {
  27.             //Get to actually parsing the command
  28.             args = cmd.split(cmd).pop().split('_')
  29.             console.log(args)
  30.             console.log(cmds[actualcmd].argNum)
  31.             if (args.length === cmds[actualcmd].argNum + 1) {
  32.                
  33.                 //Correct number of args
  34.                 args.unshift(message);
  35.                 cmds[actualcmd].func.apply(this, args)
  36.             } else {
  37.                
  38.                 cmds[actualcmd].usage(message)
  39.             }
  40.  
  41.  
  42.         }
  43.     }
  44. })
  45.  
  46. addCmd('hello', 0, function (message) {
  47.     message.channel.send('Hello, ' + message.author.username)
  48.     errorlvl = 0
  49. }, function (message) {
  50.     errorlvl = 1
  51.     message.channel.send(message.author.username + ', the arguments are not complete. The full usage of this command is: !hello')
  52. })
  53. client.login('MzE4ODk2MDM5NDM4NDUwNjk5.DA5Cuw.EyshBBnXnrETKf2KPU-MKclulpg')
Advertisement
Add Comment
Please, Sign In to add comment