onoff123

All Of The Level Code

Feb 22nd, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. const Discord = require("discord.js");
  2. const client = new Discord.Client();
  3. const config = require("./config.json");
  4. const SQLite = require("better-sqlite3");
  5. const sql = new SQLite('./scores.sqlite');
  6.  
  7. client.on("ready", () => {
  8. // Check if the table "points" exists.
  9. const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';").get();
  10. if (!table['count(*)']) {
  11. // If the table isn't there, create it and setup the database correctly.
  12. sql.prepare("CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER);").run();
  13. // Ensure that the "id" row is always unique and indexed.
  14. sql.prepare("CREATE UNIQUE INDEX idx_scores_id ON scores (id);").run();
  15. sql.pragma("synchronous = 1");
  16. sql.pragma("journal_mode = wal");
  17. }
  18.  
  19. // And then we have two prepared statements to get and set the score data.
  20. client.getScore = sql.prepare("SELECT * FROM scores WHERE user = ? AND guild = ?");
  21. client.setScore = sql.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points, level) VALUES (@id, @user, @guild, @points, @level);");
  22. });
  23.  
  24. client.on("message", message => {
  25. if (message.author.bot) return;
  26. let score;
  27. if (message.guild) {
  28. score = client.getScore.get(message.author.id, message.guild.id);
  29. if (!score) {
  30. score = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, points: 0, level: 1 }
  31. }
  32. score.points++;
  33. const curLevel = Math.floor(0.1 * Math.sqrt(score.points));
  34. if(score.level < curLevel) {
  35. score.level++;
  36. message.reply(`You've leveled up to level **${curLevel}**! Ain't that dandy?`);
  37. }
  38. client.setScore.run(score);
  39. }
  40. if (message.content.indexOf(config.prefix) !== 0) return;
  41.  
  42. const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
  43. const command = args.shift().toLowerCase();
  44.  
  45. // Command-specific code here!
  46. });
  47. if(command === "points") {
  48. return message.reply(`You currently have ${score.points} points and are level ${score.level}!`);
  49. }
  50. if(command === "give") {
  51. // Limited to guild owner - adjust to your own preference!
  52. if(!message.author.id === message.guild.owner) return message.reply("You're not the boss of me, you can't do that!");
  53.  
  54. const user = message.mentions.users.first() || client.users.cache.get(args[0]);
  55. if(!user) return message.reply("You must mention someone or give their ID!");
  56.  
  57. const pointsToAdd = parseInt(args[1], 10);
  58. if(!pointsToAdd) return message.reply("You didn't tell me how many points to give...")
  59.  
  60. // Get their current points.
  61. let userscore = client.getScore.get(user.id, message.guild.id);
  62. // It's possible to give points to a user we haven't seen, so we need to initiate defaults here too!
  63. if (!userscore) {
  64. userscore = { id: `${message.guild.id}-${user.id}`, user: user.id, guild: message.guild.id, points: 0, level: 1 }
  65. }
  66. userscore.points += pointsToAdd;
  67.  
  68. // We also want to update their level (but we won't notify them if it changes)
  69. let userLevel = Math.floor(0.1 * Math.sqrt(score.points));
  70. userscore.level = userLevel;
  71.  
  72. // And we save it!
  73. client.setScore.run(userscore);
  74.  
  75. return message.channel.send(`${user.tag} has received ${pointsToAdd} points and now stands at ${userscore.points} points.`);
  76. }
  77.  
  78. if(command === "leaderboard") {
  79. const top10 = sql.prepare("SELECT * FROM scores WHERE guild = ? ORDER BY points DESC LIMIT 10;").all(message.guild.id);
  80.  
  81. // Now shake it and show it! (as a nice embed, too!)
  82. const embed = new Discord.MessageEmbed()
  83. .setTitle("Leaderboard")
  84. .setAuthor(client.user.username, client.user.avatarURL())
  85. .setDescription("Our top 10 points leaders!")
  86. .setColor(0x00AE86);
  87.  
  88. for(const data of top10) {
  89. embed.addFields({ name: client.users.cache.get(data.user).tag, value: `${data.points} points (level ${data.level})` });
  90. }
  91. return message.channel.send({embed});
  92. }
  93.  
  94. client.login(config.ofcourseiputmytokebuticantputithere);
Advertisement
Add Comment
Please, Sign In to add comment