Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. //XP CODE
  2. const Enmap = require("enmap");
  3. client.points = new Enmap({name: "points"});
  4.  
  5. client.on("message", message => {
  6. // As usual, ignore all bots.
  7. if (message.author.bot) return;
  8.  
  9. // If this is not in a DM, execute the points code.
  10. if (message.guild) {
  11. // We'll use the key often enough that simplifying it is worth the trouble.
  12. const key = `${message.guild.id}-${message.author.id}`;
  13.  
  14. // Triggers on new users we haven't seen before.
  15. client.points.ensure(`${message.guild.id}-${message.author.id}`, {
  16. user: message.author.id,
  17. guild: message.guild.id,
  18. points: 0,
  19. level: 1
  20. });
  21.  
  22. client.points.inc(key, "points");
  23.  
  24. // Calculate the user's current level
  25. const curLevel = Math.floor(0.1 * Math.sqrt(client.points.get(key, "points")));
  26.  
  27. // Act upon level up by sending a message and updating the user's level in enmap.
  28. if (client.points.get(key, "level") < curLevel) {
  29. message.reply(`You've leveled up to level **${curLevel}**! Ain't that dandy?`);
  30. client.points.set(key, curLevel, "level");
  31. }
  32. }
  33. // Rest of message handler
  34. });
  35.  
  36. client.on("message", message => {
  37. if (message.author.bot) return;
  38. if (message.guild) { /* Points Code Here */ }
  39. if (message.content.indexOf(config.prefix) !== 0) return;
  40.  
  41. const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  42. const command = args.shift().toLowerCase();
  43.  
  44. if (command === "points") {
  45. const key = `${message.guild.id}-${message.author.id}`;
  46. return message.channel.send(`You currently have ${client.points.get(key, "points")} points, and are level ${client.points.get(key, "level")}!`);
  47.  
  48. if(command === "leaderboard") {
  49. // Get a filtered list (for this guild only), and convert to an array while we're at it.
  50. const filtered = client.points.filter( p => p.guild === message.guild.id ).array();
  51.  
  52. // Sort it to get the top results... well... at the top. Y'know.
  53. const sorted = filtered.sort((a, b) => b.points - a.points);
  54.  
  55. // Slice it, dice it, get the top 10 of it!
  56. const top10 = sorted.splice(0, 10);
  57.  
  58. // Now shake it and show it! (as a nice embed, too!)
  59. const embed = new Discord.RichEmbed()
  60. .setTitle("Leaderboard")
  61. .setAuthor(client.user.username, client.user.avatarURL)
  62. .setDescription("Our top 10 points leaders!")
  63. .setColor(0x00AE86);
  64. for(const data of top10) {
  65. embed.addField(client.users.get(data.user).tag, `${data.points} points (level ${data.level})`);
  66. }
  67. return message.channel.send({embed});
  68.  
  69. if(command === "give") {
  70. // Limited to guild owner - adjust to your own preference!
  71. if(message.author.id !== message.guild.ownerID)
  72. return message.reply("You're not the boss of me, you can't do that!");
  73.  
  74. const user = message.mentions.users.first() || client.users.get(args[0]);
  75. if(!user) return message.reply("You must mention someone or give their ID!");
  76.  
  77. const pointsToAdd = parseInt(args[1], 10);
  78. if(!pointsToAdd)
  79. return message.reply("You didn't tell me how many points to give...")
  80.  
  81. // Ensure there is a points entry for this user.
  82. client.points.ensure(`${message.guild.id}-${user.id}`, {
  83. user: message.author.id,
  84. guild: message.guild.id,
  85. points: 0,
  86. level: 1
  87. });
  88.  
  89. // Get their current points.
  90. let userPoints = client.points.get(`${message.guild.id}-${user.id}`, "points");
  91. userPoints += pointsToAdd;
  92.  
  93.  
  94. // And we save it!
  95. client.points.set(`${message.guild.id}-${user.id}`, userPoints, "points")
  96.  
  97. message.channel.send(`${user.tag} has received ${pointsToAdd} points and now stands at ${userPoints} points.`);
  98. }
  99.  
  100. if(command === "cleanup") {
  101. // Let's clean up the database of all "old" users,
  102. // and those who haven't been around for... say a month.
  103.  
  104. // Get a filtered list (for this guild only).
  105. const filtered = client.points.filter( p => p.guild === message.guild.id );
  106.  
  107. // We then filter it again (ok we could just do this one, but for clarity's sake...)
  108. // So we get only users that haven't been online for a month, or are no longer in the guild.
  109. const rightNow = new Date();
  110. const toRemove = filtered.filter(data => {
  111. return !message.guild.members.has(data.user) || rightNow - 2592000000 > data.lastSeen;
  112. });
  113.  
  114. toRemove.forEach(data => {
  115. client.points.delete(`${message.guild.id}-${data.user}`);
  116. });
  117.  
  118. message.channel.send(`I've cleaned up ${toRemove.size} old farts.`);
  119. }
  120. }
  121. }
  122. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement