1

I've been trying to get this command to work, but when I execute it, it doesn't send anything and it doesn't even give me an error message. Help would be much appreciated :)

const db = require('quick.db');
const Discord = require('discord.js');
const bot = new Discord.Client();

module.exports = {
    name: "lbeggs",
    description: "Check the global leaderboard of eggs",

    async execute (message, args) {
        let money = await db.fetch(`eggs_${message.author.id}`, { sort: '.data' })
        if(money == null) return db.set(`eggs_${message.author.id}`, 0)

        let content = "";

        for (let i = 0; i < money.length; i++){
            let user = bot.users.cache.get(money[i].ID.split('_')[2]).username

            content += `${i+1}. ${user} - ${money[i].data} \n`;

            const embed = new Discord.MessageEmbed()
            .setTitle(`Global Egg Leaderboard`)
            .setDescription(`${content}`)
            .setColor("#0000FF")
            .setTimestamp()

            message.channel.send(embed);
        }
        
    } 
} 

bot.login(process.env.ABE_BOT_TOKEN);
chickenloveryt
  • 43
  • 1
  • 2
  • 8
  • Well, it's weird that it doesn't give you an error. I would expect it to be caused by the content variable being longer than 2048 characters, which is the limit for description of embeds. When it happened to me, it gave me an error tho. I would recommed you adding .limit(some number, maybe like 25) to the sort, so it would only show top 25 people. Then you could add the placement of that specific user below it, just make sure it isn't longer than 2048 characters. – Pandicon Jan 26 '21 at 20:21
  • Thank you for the response! For that .limit(25) you mentioned, where should I put it in my code? Thanks again! – chickenloveryt Feb 03 '21 at 21:59
  • I assume that money is a sorted list from highest to lowest. I do it differently, but if you have the list sorted, you can run the for loop only up to 25 times. Easy way to do it is to make a variable (lets say b) and check if money.length is higher than 25. If so, you set the 'b' to 25, otherwise you make it money.length. Then you do for(let i = 0; i<= b; i++) {}. Also, doesn't it send the embed multiple times since you have message.channel.send(embed) in the for loop? You could definetely create and send the embed after the loop ends. – Pandicon Feb 04 '21 at 22:18
  • Thank you for the clarification! And luckily since I now fixed that, I'm getting visible errors in my terminal! It seems like the only error now is "Cannot read property 'ID' of undefined" in the let user statement. I'm a bit confused on this one because it would get users from different servers and compare their amounts, but would I use a function to search for users in guilds like `message.guild.members.cache.get()`? – chickenloveryt Feb 06 '21 at 00:27
  • Try logging `money[i]` before you try to get ID from it. Seems like money[i] is undefined for whatever reason. Are there people saved in the database? If not, that could be the problem, because money would be null and null[i] is undefined. But as I said, try logging `money` and then `money[i]`. – Pandicon Feb 07 '21 at 13:20
  • Yeah, I would say there are at least 50 people in the database (the same thing happens when I remove the message.guild.id). I don't get why I have that problem because defined above if money === null, money = 0. If you would like, I can put the code I have atm in a [Pastebin](https://pastebin.com/scJxw3ZX) because maybe I did something wrong there. Also sorry about the wait lol. – chickenloveryt Feb 23 '21 at 22:38
  • On line 20, you fetch the user by money[i].id. However, if money[i] is undefined, it won't get the userID, and it can't fetch the user from it. So you have undefined user, but then you do user.username, which results in an error. I would suggest putting in `if(!money[i]) break` on line 19 (first line of the for loop) which stops the loop once it reaches the end of money. Also, did you try to console log money? Because as I said, line 20 won't work if the money[i] is undefined, so if money is 0, then it will break. – Pandicon Mar 13 '21 at 17:29

0 Answers0