JaBa/dashboard/routes/guild-stats.js

47 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-01-04 02:18:28 +05:00
const express = require("express"),
utils = require("../utils"),
CheckAuth = require("../auth/CheckAuth"),
router = express.Router();
2022-01-04 02:18:28 +05:00
router.get("/:serverID", CheckAuth, async (req, res) => {
2022-01-04 02:18:28 +05:00
// Check if the user has the permissions to edit this guild
const guild = req.client.guilds.cache.get(req.params.serverID);
if (!guild || !req.userInfos.displayedGuilds || !req.userInfos.displayedGuilds.find((g) => g.id === req.params.serverID)) {
return res.render("404", {
user: req.userInfos,
translate: req.translate,
currentURL: `${req.client.config.dashboard.baseURL}${req.originalUrl}`
2022-01-04 02:18:28 +05:00
});
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
// Fetch guild informations
const membersData = await req.client.membersData.find({
guildID: guild.id
}).lean();
2022-01-04 02:18:28 +05:00
const leaderboards = {
money: utils.sortArrayOfObjects("money", membersData),
level: utils.sortArrayOfObjects("level", membersData)
2022-01-04 02:18:28 +05:00
};
for (const cat in leaderboards) {
const e = leaderboards[cat];
if (e.length > 10) e.length = 10;
2022-01-13 00:26:23 +05:00
}
2022-01-04 02:18:28 +05:00
const stats = {
money: await utils.fetchUsers(leaderboards.money, req.client),
level: await utils.fetchUsers(leaderboards.level, req.client)
};
res.render("stats/guild", {
user: req.userInfos,
stats,
bot: req.client,
guildID: guild.id,
translate: req.translate,
currentURL: `${req.client.config.dashboard.baseURL}${req.originalUrl}`,
2022-01-04 02:18:28 +05:00
});
});
module.exports = router;