JaBa/dashboard/routes/guild-stats.js

56 lines
1.5 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)
2022-01-04 02:18:28 +05:00
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
});
const memberData = await req.client.findOrCreateMember({ id: req.userInfos.id, guildId: guild.id });
2022-01-13 23:09:43 +05:00
2022-01-04 02:18:28 +05:00
// Fetch guild informations
const membersData = await req.client.membersData.find({
guildID: guild.id,
}).lean(),
members = membersData.map((m) => {
return {
id: m.id,
money: m.money + m.bankSold,
};
}).sort((a, b) => b.money - a.money);
2022-01-04 02:18:28 +05:00
const leaderboards = {
money: members,
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),
2022-01-04 02:18:28 +05:00
};
res.render("stats/guild", {
user: req.userInfos,
stats,
2022-01-13 23:09:43 +05:00
memberData: memberData,
bot: req.client,
2022-01-14 00:12:59 +05:00
convertTime: req.convertTime,
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;