Leaderboard in quick.db

actually, it’s the leaderboard message.
you first define it, var leaderboardMessage, but it has no value, therefor it’s undefined. then in the loop, you add on to leaderboard message, but the undefined stays there.
basically, change var leaderboardMessage to var leaderboardMessage = \``

then for the IDs, use a replace function to replace the moons_”828282idlol” to “”

const db = require('quick.db');

exports.run = async (client, message, args) => {
  
  let user = message.author;
  let guild = message.guild;
  
  let embed = new Discord.RichEmbed().setColor([54, 57, 63]).setTimestamp();
  
  embed.setAuthor("LEADERBOARD | " + guild.name, guild.iconURL);
  
  let place = "SEM COLOCAÇÃO";
  
  await db.startsWith(`total_points_`, {
    sort:'.data'
  }).then(async resp => {
    
    resp.length = 10;
    let xp, level;
    
    let a = 1;
    for (var i in resp) {
      let id = resp[i].ID.split('_')[2];
      let total = await db.fetch(`total_points_${id}`);
      level = await db.fetch(`level_${id}`);
      if (level === null) level = 0;
      xp = await db.fetch(`xp_${id}`);
      if (xp === null) xp = 0;
      if (total === null) total = 0;
      let name;
      try {
        name = await client.users.get(id).username;
      } catch (e) {
        name = `${id}`;
      }
      embed.addField(`[${a}] ${name}`, `Level: ${level} [XP: ${xp}]`, false);
      a++;
    }
    
  });
  
  embed.setDescription(`:clipboard: Top 10`);
  
  embed.setFooter(`O teu lugar no pódio é #${place}`, user.avatarURL);
  
  //embed.setThumbnail(url);
  
  message.channel.send(embed);
  
}```

//Aqui é uma leaderboard de XP, mas voce pode fazer uma para coins somente trocando as referencias.

//Here is an XP Leaderboard, but you can modify to an coin leaderboad.

It worked in most of the code. Now it doesn’t show the undefined, but I need to get the username of the user. And sorry @NexxyZ1, but your code doesn’t solve. :confused:

Ok. So. I’ve found a way to get the username from the id. But now I’ve tested and the pages doesn’t work.

Code:

const db = require('quick.db');

exports.run = async (bot, message, args) => {
  async function getLeaderboard (page, per_page) {
  // Get all data not sorted
  const resp = await db.startsWith('moons_', {sort: '.data'});
  // Pagination
    
  var page = page || 1,
  per_page = per_page || 5,
  offset = (page - 1) * per_page,

  paginatedItems = resp.slice(offset).slice(0, per_page),
  total_pages = Math.ceil(resp.length / per_page);
    
    let id = resp.slice('moons_')
      console.log(id)
    
  
  // Leaderboard Message -> Make your message as you want.
  var leaderboardMessage = '';
  for (var i in paginatedItems) {
    let id = resp[i].ID.replace('moons_', '');
    let name;
      try {
        name = await bot.users.get(id).username;
      } catch (e) {
        name = `${id}`;
      }
    leaderboardMessage += `${name} | Moons: ${paginatedItems[i].data} \n`;//${paginatedItems[i].ID}
  }
  
  let end = {
    page: page,
    per_page: per_page,
    pre_page: page - 1 ? page - 1 : null,
    next_page: (total_pages > page) ? page + 1 : null,
    total: resp.length,
    total_pages: total_pages,
    data: paginatedItems,
    message: leaderboardMessage
  };

  // RESULT
    console.log(leaderboardMessage)
    const topembed = new Discord.RichEmbed()
    .setColor(16777215)
    .setAuthor('Top de Moons')
    .setDescription('Top de Moons do S1mple', `Página: ${page}`)
    .addField(leaderboardMessage, `Top 5`)
    .setFooter(`Página: ${page} | Por página: ${per_page}`)
    return message.channel.send(topembed)
}
  
  //if (args) {
    //const lepage = parseInt(args[0]);
    //getLeaderboard (lepage, 5)
  //} else {
  getLeaderboard (1, 5)
//}
  
}

what do you mean the pages don’t work

if I use getLeaderboard (2, 5) It will give me the moons of the 2nd page, but not the usernames.

there is yt tutorials on this subject and a quickdb server. I think they could help you too.