Advertisement
Ryyan

Untitled

Jul 8th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 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(`Serving ${client.guilds.size} servers`);
  20. });
  21.  
  22. client.on("guildCreate", guild => {
  23. // This event triggers when the bot joins a guild.
  24. console.log(`New guild joined: ${guild.name} (id: ${guild.id}). This guild has ${guild.memberCount} members!`);
  25. client.user.setActivity(`Serving ${client.guilds.size} servers`);
  26. });
  27.  
  28. client.on("guildDelete", guild => {
  29. // this event triggers when the bot is removed from a guild.
  30. console.log(`I have been removed from: ${guild.name} (id: ${guild.id})`);
  31. client.user.setActivity(`Serving ${client.guilds.size} servers`);
  32. });
  33.  
  34.  
  35. client.on("message", async message => {
  36. // This event will run on every single message received, from any channel or DM.
  37.  
  38. // It's good practice to ignore other bots. This also makes your bot ignore itself
  39. // and not get into a spam loop (we call that "botception").
  40. if(message.author.bot) return;
  41.  
  42. // Also good practice to ignore any message that does not start with our prefix,
  43. // which is set in the configuration file.
  44. if(message.content.indexOf(config.prefix) !== 0) return;
  45.  
  46. // Here we separate our "command" name, and our "arguments" for the command.
  47. // e.g. if we have the message "+say Is this the real life?" , we'll get the following:
  48. // command = say
  49. // args = ["Is", "this", "the", "real", "life?"]
  50. const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  51. const command = args.shift().toLowerCase();
  52.  
  53. // Let's go with a few common example commands! Feel free to delete or change those.
  54.  
  55. if(command === "ping") {
  56. // Calculates ping between sending a message and editing it, giving a nice round-trip latency.
  57. // The second ping is an average latency between the bot and the websocket server (one-way, not round-trip)
  58. const m = await message.channel.send("Ping?");
  59. m.edit(`Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms`);
  60. }
  61.  
  62. if(command === "say") {
  63. // makes the bot say something and delete the message. As an example, it's open to anyone to use.
  64. // To get the "message" itself we join the `args` back into a string with spaces:
  65. const sayMessage = args.join(" ");
  66. // Then we delete the command message (sneaky, right?). The catch just ignores the error with a cute smiley thing.
  67. message.delete().catch(O_o=>{});
  68. // And we get the bot to say the thing:
  69. message.channel.send(sayMessage);
  70. }
  71.  
  72. if(command === "kick") {
  73. // This command must be limited to mods and admins. In this example we just hardcode the role names.
  74. // Please read on Array.some() to understand this bit:
  75. // https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some?
  76. if(!message.member.roles.some(r=>["Administrator", "Moderator"].includes(r.name)) )
  77. return message.reply("Sorry, you don't have permissions to use this!");
  78.  
  79. // Let's first check if we have a member and if we can kick them!
  80. // message.mentions.members is a collection of people that have been mentioned, as GuildMembers.
  81. // We can also support getting the member by ID, which would be args[0]
  82. let member = message.mentions.members.first() || message.guild.members.get(args[0]);
  83. if(!member)
  84. return message.reply("Please mention a valid member of this server");
  85. if(!member.kickable)
  86. return message.reply("I cannot kick this user! Do they have a higher role? Do I have kick permissions?");
  87.  
  88. // slice(1) removes the first part, which here should be the user mention or ID
  89. // join(' ') takes all the various parts to make it a single string.
  90. let reason = args.slice(1).join(' ');
  91. if(!reason) reason = "No reason provided";
  92.  
  93. // Now, time for a swift kick in the nuts!
  94. await member.kick(reason)
  95. .catch(error => message.reply(`Sorry ${message.author} I couldn't kick because of : ${error}`));
  96. message.reply(`${member.user.tag} has been kicked by ${message.author.tag} because: ${reason}`);
  97.  
  98. }
  99.  
  100. if(command === "ban") {
  101. // Most of this command is identical to kick, except that here we'll only let admins do it.
  102. // In the real world mods could ban too, but this is just an example, right? ;)
  103. if(!message.member.roles.some(r=>["Administrator"].includes(r.name)) )
  104. return message.reply("Sorry, you don't have permissions to use this!");
  105.  
  106. let member = message.mentions.members.first();
  107. if(!member)
  108. return message.reply("Please mention a valid member of this server");
  109. if(!member.bannable)
  110. return message.reply("I cannot ban this user! Do they have a higher role? Do I have ban permissions?");
  111.  
  112. let reason = args.slice(1).join(' ');
  113. if(!reason) reason = "No reason provided";
  114.  
  115. await member.ban(reason)
  116. .catch(error => message.reply(`Sorry ${message.author} I couldn't ban because of : ${error}`));
  117. message.reply(`${member.user.tag} has been banned by ${message.author.tag} because: ${reason}`);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement