Advertisement
Guest User

Untitled

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