Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. // Load up the discord.js library
  2.  
  3. const Discord = require("discord.js");
  4.  
  5.  
  6.  
  7. // This is your client. Some people call it `bot`, some people call it `self`,
  8.  
  9. // some might call it `cootchie`. Either way, when you see `client.something`, or `bot.something`,
  10.  
  11. // this is what we're refering to. Your client.
  12.  
  13. const client = new Discord.Client();
  14.  
  15.  
  16.  
  17. // Here we load the config.json file that contains our token and our prefix values.
  18.  
  19. const config = require("./config.json");
  20.  
  21. // config.token contains the bot's token
  22.  
  23. // config.prefix contains the message prefix.
  24.  
  25.  
  26.  
  27. client.on("ready", () => {
  28.  
  29. // This event will run if the bot starts, and logs in, successfully.
  30.  
  31. console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
  32.  
  33. // Example of changing the bot's playing game to something useful. `client.user` is what the
  34.  
  35. // docs refer to as the "ClientUser".
  36.  
  37. client.user.setActivity(`Serving ${client.guilds.size} servers`);
  38.  
  39. });
  40.  
  41.  
  42.  
  43. client.on("guildCreate", guild => {
  44.  
  45. // This event triggers when the bot joins a guild.
  46.  
  47. console.log(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`);
  48.  
  49. client.user.setActivity(`Serving ${client.guilds.size} servers`);
  50.  
  51. });
  52.  
  53.  
  54.  
  55. client.on("guildDelete", guild => {
  56.  
  57. // this event triggers when the bot is removed from a guild.
  58.  
  59. console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`);
  60.  
  61. client.user.setActivity(`Serving ${client.guilds.size} servers`);
  62.  
  63. });
  64.  
  65.  
  66.  
  67.  
  68.  
  69. client.on("message", async message => {
  70.  
  71. // This event will run on every single message received, from any channel or DM.
  72.  
  73.  
  74.  
  75. // It's good practice to ignore other bots. This also makes your bot ignore itself
  76.  
  77. // and not get into a spam loop (we call that "botception").
  78.  
  79. if(message.author.bot) return;
  80.  
  81.  
  82.  
  83. // Also good practice to ignore any message that does not start with our prefix,
  84.  
  85. // which is set in the configuration file.
  86.  
  87. if(message.content.indexOf(config.prefix) !== 0) return;
  88.  
  89.  
  90.  
  91. // Here we separate our "command" name, and our "arguments" for the command.
  92.  
  93. // e.g. if we have the message "+say Is this the real life?" , we'll get the following:
  94.  
  95. // command = say
  96.  
  97. // args = ["Is", "this", "the", "real", "life?"]
  98.  
  99. const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  100.  
  101. const command = args.shift().toLowerCase();
  102.  
  103.  
  104.  
  105. // Let's go with a few common example commands! Feel free to delete or change those.
  106.  
  107.  
  108.  
  109. if(command === "ping") {
  110.  
  111. // Calculates ping between sending a message and editing it, giving a nice round-trip latency.
  112.  
  113. // The second ping is an average latency between the bot and the websocket server (one-way, not round-trip)
  114.  
  115. const m = await message.channel.send("Ping?");
  116.  
  117. m.edit(`Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms`);
  118.  
  119. }
  120.  
  121.  
  122.  
  123. if(command === "say") {
  124.  
  125. // makes the bot say something and delete the message. As an example, it's open to anyone to use.
  126.  
  127. // To get the "message" itself we join the `args` back into a string with spaces:
  128. //if(!message.member.roles.some(r=>["Owner", "Co-owner"].includes(r.name)) )
  129.  
  130. const sayMessage = args.join(" ");
  131.  
  132. // Then we delete the command message (sneaky, right?). The catch just ignores the error with a cute smiley thing.
  133.  
  134. message.delete().catch(O_o=>{});
  135.  
  136. // And we get the bot to say the thing:
  137.  
  138. message.channel.send(sayMessage);
  139.  
  140. }
  141.  
  142.  
  143.  
  144. if(command === "kick") {
  145.  
  146. // This command must be limited to mods and admins. In this example we just hardcode the role names.
  147.  
  148. // Please read on Array.some() to understand this bit:
  149.  
  150. // https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some?
  151.  
  152. if(!message.member.roles.some(r=>["Manager", "Supervisors"].includes(r.name)) )
  153.  
  154. return message.reply("Sorry, you don't have permissions to use this!");
  155.  
  156.  
  157.  
  158. // Let's first check if we have a member and if we can kick them!
  159.  
  160. // message.mentions.members is a collection of people that have been mentioned, as GuildMembers.
  161.  
  162. // We can also support getting the member by ID, which would be args[0]
  163.  
  164. let member = message.mentions.members.first() || message.guild.members.get(args[0]);
  165.  
  166. if(!member)
  167.  
  168. return message.reply("Please mention a valid member of this server");
  169.  
  170. if(!member.kickable)
  171.  
  172. return message.reply("I cannot kick this user! Do they have a higher role? Do I have kick permissions?");
  173.  
  174.  
  175.  
  176. // slice(1) removes the first part, which here should be the user mention or ID
  177.  
  178. // join(' ') takes all the various parts to make it a single string.
  179.  
  180. let reason = args.slice(1).join(' ');
  181.  
  182. if(!reason) reason = "No reason provided";
  183.  
  184.  
  185.  
  186. // Now, time for a swift kick in the nuts!
  187.  
  188. await member.kick(reason)
  189.  
  190. .catch(error => message.reply(`Sorry ${message.author} I couldn't kick because of : ${error}`));
  191.  
  192. message.reply(`${member.user.tag} has been kicked by ${message.author.tag} because: ${reason}`);
  193.  
  194.  
  195.  
  196. }
  197.  
  198.  
  199.  
  200. if(command === "ban") {
  201.  
  202. // Most of this command is identical to kick, except that here we'll only let admins do it.
  203.  
  204. // In the real world mods could ban too, but this is just an example, right? ;)
  205.  
  206. if(!message.member.roles.some(r=>["Manager"].includes(r.name)) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement