Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. const SQLite = require("better-sqlite3");
  2. const sql = new SQLite('./src/db.sqlite');
  3.  
  4. client.on('ready', () => {
  5. const table = sql.prepare("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'scores';").get();
  6. if (!table['count(*)']) {
  7. sql.prepare("CREATE TABLE scores (id TEXT PRIMARY KEY, user TEXT, guild TEXT, points INTEGER);").run();
  8. sql.prepare("CREATE UNIQUE INDEX idx_scores_id ON scores (id);").run();
  9. sql.pragma("synchronous = 1");
  10. sql.pragma("journal_mode = wal");
  11. }
  12. client.getScore = sql.prepare("SELECT * FROM scores WHERE user = ? AND guild = ?");
  13. client.setScore = sql.prepare("INSERT OR REPLACE INTO scores (id, user, guild, points) VALUES (@id, @user, @guild, @points);");
  14. });
  15.  
  16. client.on("message", async (message) => {
  17. // Check Catnips
  18. if(command === "catnips") {
  19. return message.reply(`You currently have ${score.points} catnips!`);
  20. }
  21.  
  22. // Leaderboard
  23. if(command === "lb") {
  24. const top10 = sql.prepare("SELECT * FROM scores WHERE guild = ? ORDER BY points DESC LIMIT 10;").all(message.guild.id);
  25. const embed = new MessageEmbed()
  26. .setTitle("Leaderboard")
  27. .setDescription("Our top 10 catnip leaders!")
  28. .setColor(emcolor);
  29.  
  30. for(const data of top10) {
  31. embed.addField(client.users.get(data.user).tag, `${data.points} catnips`);
  32. }
  33. return message.channel.send({embed});
  34. }
  35.  
  36.  
  37. // Give Points
  38. if(command === "give") {
  39. if(message.author.id !== cfg.owner) return;
  40. const user = message.mentions.users.first() || client.users.get(args[0]);
  41. if(!user) return message.reply("You must mention someone or give their ID!");
  42. const pointsToAdd = parseInt(args[1], 10);
  43. if(!pointsToAdd) return message.reply("You didn't tell me how many points to give...")
  44. let userscore = client.getScore.get(user.id, message.guild.id);
  45. if (!userscore) {
  46. userscore = { id: `${message.guild.id}-${user.id}`, user: user.id, guild: message.guild.id, points: 0, level: 1 }
  47. }
  48. userscore.points += pointsToAdd;
  49. let userLevel = Math.floor(0.1 * Math.sqrt(score.points));
  50. userscore.level = userLevel;
  51. client.setScore.run(userscore);
  52. return message.channel.send(`${user.tag} has received ${pointsToAdd} catnips and now stands at ${userscore.points} catnips.`);
  53. }
  54. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement