我的不和谐机器人的不和谐 XP 问题"无法读取未定义的属性'缓存'



我试图为我的bot设置discord-xp,但每当我尝试使用排行榜命令时,它显示一个错误:

username: client.users.cache.get(key.userID) ? client.users.cache.get(key.userID).username : "Unknown",
^
TypeError: Cannot read property 'cache' of undefined
at c:Users224isOneDriveDocumentsBot worknode_modulesdiscord-xpindex.js:289:32
at Array.map (<anonymous>)
at Function.computeLeaderboard (c:Users224isOneDriveDocumentsBot worknode_modulesdiscord-xpindex.js:283:19)
at Client.<anonymous> (c:Users224isOneDriveDocumentsBot workindex.js:61:32)
at processTicksAndRejections (node:internal/process/task_queues:94:5)

下面是代码,问题出在末尾的方法上:

const mongoose = require("mongoose");
const levels = require("./models/levels.js");
var mongoUrl;
class DiscordXp {

/**
* @param {string} [userId] - Discord user id.
* @param {string} [guildId] - Discord guild id.
*/

static async fetch(userId, guildId, fetchPosition = false) {
if (!userId) throw new TypeError("An user id was not provided.");
if (!guildId) throw new TypeError("A guild id was not provided.");

const user = await levels.findOne({
userID: userId,
guildID: guildId
});
if (!user) return false;

if (fetchPosition === true) {
const leaderboard = await levels.find({
guildID: guildId
}).sort([['xp', 'descending']]).exec();

user.position = leaderboard.findIndex(i => i.userID === userId) + 1;
}


/* To be used with canvacord or displaying xp in a pretier fashion, with each level the cleanXp stats from 0 and goes until cleanNextLevelXp when user levels up and gets back to 0 then the cleanNextLevelXp is re-calculated */
user.cleanXp = user.xp - this.xpFor(user.level);
user.cleanNextLevelXp = this.xpFor(user.level + 1) - this.xpFor(user.level);

return user;
}

/**
* @param {string} [guildId] - Discord guild id.
* @param {number} [limit] - Amount of maximum enteries to return.
*/


static async fetchLeaderboard(guildId, limit) {
if (!guildId) throw new TypeError("A guild id was not provided.");
if (!limit) throw new TypeError("A limit was not provided.");

var users = await levels.find({ guildID: guildId }).sort([['xp', 'descending']]).exec();

return users.slice(0, limit);
}

/**
* @param {string} [client] - Your Discord.CLient.
* @param {array} [leaderboard] - The output from 'fetchLeaderboard' function.
*/

static async computeLeaderboard(client, leaderboard, fetchUsers = false) {
if (!client) throw new TypeError("A client was not provided.");
if (!leaderboard) throw new TypeError("A leaderboard id was not provided.");

if (leaderboard.length < 1) return [];

const computedArray = [];

if (fetchUsers) {
for (const key of leaderboard) {
const user = await client.users.fetch(key.userID) || { username: "Unknown", discriminator: "0000" };
computedArray.push({
guildID: key.guildID,
userID: key.userID,
xp: key.xp,
level: key.level,
position: (leaderboard.findIndex(i => i.guildID === key.guildID && i.userID === key.userID) + 1),
username: user.username,
discriminator: user.discriminator
});
}
} else {
leaderboard.map(key => computedArray.push({
guildID: key.guildID,
userID: key.userID,
xp: key.xp,
level: key.level,
position: (leaderboard.findIndex(i => i.guildID === key.guildID && i.userID === key.userID) + 1),
username: client.users.cache.get(key.userID) ? client.users.cache.get(key.userID).username : "Unknown",
discriminator: client.users.cache.get(key.userID) ? client.users.cache.get(key.userID).discriminator : "0000"
}));
}

return computedArray;
}
}
module.exports = DiscordXp;

这意味着client不是Client的实例。如果您正在使用命令处理程序,请确保以正确的顺序传入参数。这是命令处理程序的一个常见错误。我无法提供任何代码,因为我找不到错误发生的地方。

最新更新