顶级成员排行榜Discord.js

时间:2020-08-03 09:32:02

标签: javascript discord bots

因此,我对几种类型的东西,金钱排行榜,测验等做出的排行榜命令很少。但是我在调​​平排行榜时遇到了问题,当我键入命令时会出现此错误

(node:34294) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'username' of undefined
at Object.exports.run (/home/Lignjoslav/commands/generalno/leveltop.js:21:65)
at processTicksAndRejections (internal/process/task_queues.js:93:5)

所有排行榜都是这样制作的,我对它们没有问题,它们工作得很好,但是这个有这个问题。这是命令:

const Discord = require("discord.js");
const db = require("quick.db");

exports.run = async (client, message, args) => {

    let komande = await db.fetch(`kanal_${message.guild.id}_komande`);
    if (message.channel.id !== komande && !message.member.permissions.has("ADMINISTRATOR"))
        return message.channel.send({
            embed: {
                title: `:video_game: Komande`,
                description: `Ovu komandu možeš koristiti samo u kanalu <#${komande}>`,
                color: 0x4CAAFF
            }
        });

    let levelTOP = await db
        .all()
        .filter(data => data.ID.startsWith(`level_${message.guild.id}`))
        .sort((a, b) => b.data - a.data);
    let content = "";

    for (let i = 0; i < levelTOP.length; i++) {
        if (i === 10) break;

        let user = client.users.cache.get(levelTOP[i].ID.split("_")[2]).username;

        content += `**${i + 1}.** __${user}__ - ${levelTOP[i].data} ?\n`;
    }

    const embed = new Discord.MessageEmbed()
        .setDescription(`**__Tabela članova sa najviše levela__**\n\n${content}`)
        .setColor("#4CAAFF");

    message.channel.send(embed);
};
exports.help = {
    name: "leveltop",
    description: "lista članova sa najviše levela",
    usage: "leveltop",
    category: "generalno",
    listed: true
};

1 个答案:

答案 0 :(得分:0)

client.users.cache只是一个缓存,它可能会或可能不会保持连接到机器人的用户的全部数据。而是尝试调用异步client.users.fetch(id)

const user = await client.users.fetch(levelTOP[i].ID.split("_")[2]);

content += `**${i + 1}.** __${user.username}__ - ${levelTOP[i].data} ?\n`;