Advertisement
freekill313

Untitled

Aug 22nd, 2019
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. // start discord.js init
  2. const config = require("./config.json"); // See config.json below for example
  3. const Discord = require("discord.js"); // Code below supports and is tested under "stable" 11.3.x
  4. const client = new Discord.Client();
  5. // end discord.js init
  6.  
  7. // Initialize **or load** the points database.
  8. const Enmap = require("enmap");
  9. client.points = new Enmap({name: "points"});
  10.  
  11. client.on("ready", () => {
  12. // This event will run if the bot starts, and logs in, successfully.
  13. console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`);
  14. });
  15.  
  16. client.on("message", async (message) => {
  17. // First, ignore itself and all other bots. Also, ignore private messages so a user can't spam the bot for points.
  18. if (!message.guild || message.author.bot) return;
  19.  
  20. /* Now we start the real code for this tutorial */
  21.  
  22. // If this is not in a DM, execute the points code.
  23. if (message.guild) {
  24. // We'll use the key often enough that simplifying it is worth the trouble.
  25. const key = `${message.guild.id}-${message.author.id}`;
  26.  
  27. // Triggers on new users we haven't seen before.
  28. client.points.ensure(key, {
  29. user: message.author.id,
  30. guild: message.guild.id,
  31. points: 0,
  32. level: 1,
  33. lastSeen: new Date()
  34. });
  35.  
  36. // Increment the points and save them.
  37. client.points.inc(key, "points");
  38.  
  39. // Calculate the user's current level
  40. const curLevel = Math.floor(0.1 * Math.sqrt(client.points.get(key, "points")));
  41.  
  42. // Act upon level up by sending a message and updating the user's level in enmap.
  43. if (client.points.get(key, "level") < curLevel) {
  44. message.reply(`You've leveled up to level **${curLevel}**! Ain't that dandy?`);
  45. client.points.set(key, curLevel, "level");
  46. }
  47. }
  48.  
  49. /* END POINTS ATTRIBUTION. Now let's have some fun with commands. */
  50.  
  51. // As usual, we stop processing if the message does not start with our prefix.
  52. if (message.content.indexOf(config.prefix) !== 0) return;
  53.  
  54. // Also we use the config prefix to get our arguments and command:
  55. const args = message.content.split(/\s+/g);
  56. const command = args.shift().slice(config.prefix.length).toLowerCase();
  57.  
  58. // Let's build some useful ones for our points system.
  59.  
  60. if (command === "points") {
  61. const key = `${message.guild.id}-${message.author.id}`;
  62. return message.channel.send(`You currently have ${client.points.get(key, "points")} points, and are level ${client.points.get(key, "level")}!`);
  63. }
  64.  
  65. if(command === "leaderboard") {
  66. // Get a filtered list (for this guild only), and convert to an array while we're at it.
  67. const filtered = client.points.array().filter( p => p.guild === message.guild.id );
  68.  
  69. // Sort it to get the top results... well... at the top. Y'know.
  70. const sorted = filtered.sort((a, b) => a.points < b.points);
  71.  
  72. // Slice it, dice it, get the top 10 of it!
  73. const top10 = sorted.splice(0, 10);
  74.  
  75. // Now shake it and show it! (as a nice embed, too!)
  76. const embed = new Discord.RichEmbed()
  77. .setTitle("Leaderboard")
  78. .setAuthor(client.user.username, client.user.avatarURL)
  79. .setDescription("Our top 10 points leaders!")
  80. .setColor(0x00AE86);
  81. for(const data of top10) {
  82. embed.addField(client.users.get(data.user).tag, `${data.points} points (level ${data.level})`);
  83. }
  84. return message.channel.send({embed});
  85. }
  86.  
  87. if(command === "give") {
  88. // Limited to guild owner - adjust to your own preference!
  89. if(!message.author.id === message.guild.owner) return message.reply("You're not the boss of me, you can't do that!");
  90.  
  91. const user = message.mentions.users.first() || client.users.get(args[0]);
  92. if(!user) return message.reply("You must mention someone or give their ID!");
  93.  
  94. const pointsToAdd = parseInt(args[1], 10);
  95. if(!pointsToAdd) return message.reply("You didn't tell me how many points to give...");
  96.  
  97. const key = `${message.guild.id}-${user.id}`;
  98.  
  99. // Ensure there is a points entry for this user.
  100. client.points.ensure(key, {
  101. user: message.author.id,
  102. guild: message.guild.id,
  103. points: 0,
  104. level: 1,
  105. lastSeen: new Date()
  106. });
  107.  
  108. // Add the points to the enmap for this user.
  109. client.points.math(key, "+", pointsToAdd, "points");
  110.  
  111. message.channel.send(`${user.tag} has received ${pointsToAdd} points and now stands at ${client.points.get(key, "points")} points.`);
  112. }
  113.  
  114. if(command === "cleanup") {
  115. // Let's clean up the database of all "old" users, and those who haven't been around for... say a month.
  116.  
  117. // Get a filtered list (for this guild only).
  118. const filtered = client.points.filter( p => p.guild === message.guild.id );
  119.  
  120. // We then filter it again (ok we could just do this one, but for clarity's sake...)
  121. // So we get only users that haven't been online for a month, or are no longer in the guild.
  122. const rightNow = new Date();
  123. const toRemove = filtered.filter(data => {
  124. return !message.guild.members.has(data.user) || rightNow - 2592000000 > data.lastSeen;
  125. });
  126.  
  127. toRemove.forEach(data => {
  128. client.points.delete(`${message.guild.id}-${data.user}`);
  129. });
  130.  
  131. message.channel.send(`I've cleaned up ${toRemove.size} old farts.`);
  132. }
  133. });
  134.  
  135. // Start the bot by logging it in.
  136. client.login(config.token);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement