MCPcopy
hub / github.com/codebymitch/TitanBot / getLeaderboard

Function getLeaderboard

src/utils/database.js:838–894  ·  view source on GitHub ↗
(client, guildId, limit = 10)

Source from the content-addressed store, hash-verified

836}
837
838export async function getLeaderboard(client, guildId, limit = 10) {
839 try {
840 if (!client.db || typeof client.db.list !== "function") {
841 logger.error("Database client is not available for getLeaderboard.");
842 return [];
843 }
844
845 const prefix = `guild:${guildId}:leveling:users:`;
846 let keys = await client.db.list(prefix);
847
848 if (!Array.isArray(keys)) {
849 if (typeof keys === 'object' && keys !== null) {
850 keys = Object.keys(keys).filter(key => key.startsWith(prefix));
851 } else {
852 return [];
853 }
854 }
855
856 if (keys.length === 0) {
857 return [];
858 }
859
860 const userDataPromises = keys.map(async (key) => {
861 try {
862 const userId = key.replace(prefix, '');
863 const data = await client.db.get(key);
864 if (!data) return null;
865
866 const unwrapped = unwrapReplitData(data);
867 return {
868 userId,
869 xp: unwrapped.xp || 0,
870 level: unwrapped.level || 0,
871 totalXp: unwrapped.totalXp || 0,
872rank: 0
873 };
874 } catch (error) {
875 logger.error(`Error processing leaderboard key ${key}:`, error);
876 return null;
877 }
878 });
879
880 let userData = (await Promise.all(userDataPromises)).filter(Boolean);
881
882 userData.sort((a, b) => (b.totalXp || 0) - (a.totalXp || 0));
883
884 userData = userData.map((user, index) => ({
885 ...user,
886 rank: index + 1
887 }));
888
889 return userData.slice(0, limit);
890 } catch (error) {
891 logger.error(`Error getting leaderboard for guild ${guildId}:`, error);
892 return [];
893 }
894}
895

Callers 1

executeFunction · 0.90

Calls 3

unwrapReplitDataFunction · 0.85
listMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected