SHARE
TWEET

top like probot

gaber-elsayed Feb 21st, 2020 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const SQLite = require("better-sqlite3");
  2. const sql = new SQLite('./scores.sqlite');
  3.  
  4. client.on("ready", () => {
  5.   // Check if the table "points" exists.
  6.   const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';").get();
  7.   if (!table['count(*)']) {
  8.     // If the table isn't there, create it and setup the database correctly.
  9.     sql.prepare("CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER, level INTEGER);").run();
  10.     // Ensure that the "id" row is always unique and indexed.
  11.     sql.prepare("CREATE UNIQUE INDEX idx_scores_id ON scores (id);").run();
  12.     sql.pragma("synchronous = 1");
  13.     sql.pragma("journal_mode = wal");
  14.   }
  15.  
  16.   // And then we have two prepared statements to get and set the score data.
  17.   client.getScore = sql.prepare("SELECT * FROM scores WHERE user = ? AND guild = ?");
  18.   client.setScore = sql.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points, level) VALUES (@id, @user, @guild, @points, @level);");
  19. });
  20.  
  21. client.on("message", message => {
  22.     var prefix = "!"
  23.   if (message.author.bot) return;
  24.   let score;
  25.   if (message.guild) {
  26.     score = client.getScore.get(message.author.id, message.guild.id);
  27.     if (!score) {
  28.       score = { id: `${message.guild.id}-${message.author.id}`, user: message.author.id, guild: message.guild.id, points: 0, level: 1 }
  29.     }
  30.     score.points++;
  31.     const curLevel = Math.floor(0.1 * Math.sqrt(score.points));
  32.     if(score.level < curLevel) {
  33.       message.reply(`You've leveled up to level **${curLevel}**! Ain't that dandy?`);
  34.     }
  35.     client.setScore.run(score);
  36.   }
  37.   if (message.content.indexOf(prefix) !== 0) return;
  38.  
  39.   const args = message.content.slice(prefix.length).trim().split(/ +/g);
  40.   const command = args.shift().toLowerCase();
  41.  
  42.   if(command === "top") {
  43.   const top10 = sql.prepare("SELECT * FROM scores WHERE guild = ? ORDER BY points DESC LIMIT 10;").all(message.guild.id);
  44.  
  45.     // Now shake it and show it! (as a nice embed, too!)
  46.   const embed = new Discord.RichEmbed()
  47.     .setTitle("Leaderboard") //هنا بس تغير الكلمات ويصير مثل برو بوت
  48.     .setAuthor(client.user.username, client.user.avatarURL)
  49.     .setDescription("Our top 10 points leaders!")
  50.     .setColor(0x00AE86);
  51.  
  52.   for(const data of top10) {
  53.     embed.addField(client.users.get(data.user).tag, `${data.points} points (level ${data.level})`);
  54.   }
  55.   return message.channel.send({embed});
  56. }
  57. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top