As89q98123678

Untitled

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