Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

ala89 May 19th, 2020 (edited) 754 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class {
  2.         constructor(author, leaderboardFunction, sortFunction, displayFunction, defaultValue, type, timeout = 5 * 60e3) {
  3.             this.type = type
  4.             this.author = author
  5.             this.leaderboardFunction = leaderboardFunction
  6.             this.sortFunction = sortFunction
  7.             this.displayFunction = displayFunction
  8.             this.defaultValue = defaultValue
  9.             this.page = 0
  10.             this.timeout = timeout
  11.         }
  12.  
  13.         loadPage(page = this.page, preventIdentical) {
  14.             const leaderboard = this.generateLeaderboard()
  15.             const correctPage = this.correctPage(leaderboard, page)
  16.             if (preventIdentical && correctPage === this.page) return
  17.             this.page = correctPage
  18.             return this.generateEmbed(leaderboard)
  19.         }
  20.  
  21.         generateLeaderboard() {
  22.             const leaderboard = this.leaderboardFunction().sort((a, b) => this.sortFunction(a[1], b[1])) // [k, v][]
  23.             const userExists = leaderboard.some(e => e[0] === this.author) // [k, v][]
  24.             if (!userExists && leaderboard.length !== 10) leaderboard.push([this.author, this.defaultValue])
  25.             if (leaderboard.length === 11 && leaderboard[10][0] === this.author) leaderboard.pop() // [k, v][]
  26.             return leaderboard
  27.         }
  28.  
  29.         correctPage(leaderboard, page) {
  30.             return Math.min(Math.max(page, 0), Math.floor((leaderboard.length - 1) / 10))
  31.         }
  32.  
  33.         generateEmbed(leaderboard) {
  34.             const displayedUsers = this.generateDisplayedUsers(leaderboard, this.page) // [i, k, v][]
  35.             const embedText = displayedUsers.map(e => `**${e[0] + 1}.** ${e[1] === this.author ? '**Vous**' : `<@!${e[1]}>`} ${this.displayFunction(e[2], e[1], e[0])}`) // v, k, i
  36.             return new Discord.RichEmbed()
  37.                 .setTitle(`Classement${this.type ? ` ${this.type}`: ''} ${this.page * 10 + 1} - ${Math.min((this.page + 1) * 10, Math.max(leaderboard.length, displayedUsers.length))}`)
  38.                 .setDescription(embedText)
  39.                 .setColor('RANDOM')
  40.                 .setFooter(`Page ${this.page + 1}/${Math.ceil(leaderboard.length / 10)}`)
  41.                 .setTimestamp()
  42.         }
  43.  
  44.         generateDisplayedUsers(leaderboard, page) {
  45.             const displayedUsers = leaderboard.map((e, i) => [i, ...e]).slice(page * 10, (page + 1) * 10) // [i, k, v][]
  46.             if (page === 0 && !displayedUsers.some(e => e[1] === this.author)) { // [i, k, v][]
  47.                 const userIndex = leaderboard.findIndex(e => e[0] === this.author) // [k, v][]
  48.                 displayedUsers.push(userIndex === -1 ? [leaderboard.length, this.author, this.defaultValue] : [userIndex, ...leaderboard[userIndex]])
  49.             }
  50.             return displayedUsers
  51.         }
  52.  
  53.         handleSent(sent) {
  54.             sent.react('◀️').then(() => sent.react('▶️')).then(() => sent.react('🔄'))
  55.             leaderboards.set(sent.id, this)
  56.             setTimeout(() => {
  57.                 leaderboards.delete(sent.id)
  58.                 if (sent && !sent.deleted) {
  59.                     ['◀️', '▶️', '🔄'].forEach(e => sent.reactions.get(e).remove())
  60.                 }
  61.             }, this.timeout)
  62.         }
  63.  
  64.         handleReaction(reaction, user) {
  65.             reaction.remove(user)
  66.             if (user.id === this.author) {
  67.                 if (reaction.emoji.name === '▶️') {
  68.                     const embed = this.loadPage(this.page + 1, !0)
  69.                     if (embed) reaction.message.edit(embed)
  70.                 }
  71.                 else if (reaction.emoji.name === '◀️') {
  72.                     const embed = this.loadPage(this.page - 1, !0)
  73.                     if (embed) reaction.message.edit(embed)
  74.                 }
  75.                 else if (reaction.emoji.name === '🔄') {
  76.                     const embed = this.loadPage()
  77.                     if (embed) reaction.message.edit(embed)
  78.                 }
  79.             }
  80.         }
  81.     }
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