Advertisement
Donovan_DMC

UPDATED & TESTED Discord.JS Example for SO

May 5th, 2018
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Discord = require("discord.js");
  2. const client = new Discord.Client();
  3. const token="BOT_TOKEN";
  4. // make the prefix easily changeable
  5. var prefix="+";
  6.  
  7. client.on("ready", () => {
  8.     console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
  9. });
  10.  
  11. client.on("message", async message => {
  12.     try{
  13. if(message.author.bot) return;
  14. if(message.content.indexOf(prefix) !== 0) return;
  15.  
  16. // separate the message into "command" and "args" (Array of content after command broken up by spaces)
  17. // e.g. if we have the message "+say Is this the real life?" , we'll get the following:
  18. // command = say
  19. // args = ["Is", "this", "the", "real", "life?"]
  20. const args = message.content.slice(prefix.length).trim().split(/\s+/g);
  21. const command = args.shift().toLowerCase();
  22.  
  23. switch(command) {
  24.     case "help":
  25.         try {
  26.         var gm=message.guild.members.array();
  27.         var i=0;
  28.         var ii=0;
  29.         console.log(`Scanning through ${message.guild.memberCount} members for help command`);
  30.         for(var member in gm) {
  31.          if(gm[member].roles.some(r=>["helper"].includes(r.name.toLowerCase()))) {
  32.                 if(gm[member].roles.some(r=>["on duty"].includes(r.name.toLowerCase()))) {
  33.                   gm[member].send(`Help command ran:\nUser: ${message.author.tag} (${message.author.id})\nChannel: ${message.channel.name} (${message.channel.id}\nGuild: ${message.guild.name} (${message.guild.id})\nContent: ${args.join(" ")}`);
  34.                } else {
  35.                    ii++;
  36.                }
  37.            } else {
  38.                i++;
  39.            }
  40.         }
  41.         if(+i+ii==message.guild.memberCount) {
  42.             message.reply("There are currently no available staff to answer this question, please ask again later.");
  43.             console.log(`No staff were around to answer the question "${args.join(" ")}"" by user ${message.author.tag} (${message.author.id} in channel #${message.channel.name} (${message.channel.id}) of guild ${message.guild.name} (${message.guild.id})`);
  44.        } else {
  45.            message.reply("Your message has been sent to an available staff member!");
  46.        }
  47.        }catch(e){console.log(e)}
  48.        break;
  49.  
  50.    case "onduty":
  51.        try {
  52.        var roleid=message.guild.roles.find("name","on duty").id;
  53.        if(message.member.roles.some(r=>["helper"].includes(r.name.toLowerCase()))) {
  54.            // make sure they do not already have the role
  55.            if(message.member.roles.some(r=>["on duty"].includes(r.name.toLowerCase()))) {
  56.                return "You are already on duty!";
  57.            }
  58.            //add the role
  59.            message.member.addRole(roleid);
  60.            //alert them that they are now on duty
  61.            message.author.send("You are now on duty!");
  62.            return console.log(`User ${message.author.tag} (${message.author.id} is now on duty!`);
  63.        } else {
  64.            return message.reply("You do not have permission to use this!");
  65.        }
  66.        }catch(e){console.log(e)}
  67.        break;
  68.  
  69.    case "offduty":
  70.        try {
  71.        var roleid=message.guild.roles.find("name","on duty").id;
  72.        if(message.member.roles.some(r=>["helper"].includes(r.name.toLowerCase()))) {
  73.            // make sure they have the role already
  74.            if(!message.member.roles.some(r=>["on duty"].includes(r.name.toLowerCase()))) {
  75.                return "You are not on duty!";
  76.            }
  77.            //remove the role
  78.            message.member.removeRole(roleid);
  79.            //alert them that they are now off duty
  80.            return message.author.send("You are now off duty!");
  81.        } else {
  82.           message.reply("You do not have permission to use this!");
  83.           return console.log(`User ${message.author.tag} (${message.author.id} is now off duty!`);
  84.        }
  85.        }catch(e){console.log(e)}
  86.        break;
  87.    }
  88.    }catch(e){console.log(e)}
  89.    
  90.    // uncomment the next line to delete the command messages after people run them
  91.    //message.delete().catch(noerr=>{});
  92. });
  93.  
  94. client.login(token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement