Advertisement
Guest User

Untitled

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