JaBa/dashboard/routes/guild-manager.js

140 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-12-10 21:39:54 +05:00
const express = require("express"),
utils = require("../utils"),
CheckAuth = require("../auth/CheckAuth"),
router = express.Router();
router.get("/:serverID", CheckAuth, async(req, res) => {
// 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-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
// Fetch guild informations
const guildInfos = await utils.fetchGuild(guild.id, req.client, req.user.guilds);
const memberData = await req.client.findOrCreateMember({ id: req.userInfos.id, guildID: guild.id });
2021-12-10 21:39:54 +05:00
res.render("manager/guild", {
guild: guildInfos,
user: req.userInfos,
memberData: memberData,
2021-12-10 21:39:54 +05:00
translate: req.translate,
bot: req.client,
convertTime: req.convertTime,
2021-12-10 21:39:54 +05:00
currentURL: `${req.client.config.dashboard.baseURL}/${req.originalUrl}`
});
});
router.post("/:serverID", CheckAuth, async(req, res) => {
// 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-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
const guildData = await req.client.findOrCreateGuild({ id: guild.id });
const data = req.body;
if (data.language) {
const language = req.client.languages.find((language) => language.aliases[0].toLowerCase() === data.language.toLowerCase());
2021-12-16 23:42:58 +05:00
if (language) guildData.language = language.name;
if (data.prefix.length >= 1 && data.prefix.length < 2000) guildData.prefix = data.prefix;
2021-12-10 21:39:54 +05:00
await guildData.save();
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
if (Object.prototype.hasOwnProperty.call(data, "welcomeEnable") || Object.prototype.hasOwnProperty.call(data, "welcomeUpdate")) {
const welcome = {
enabled: true,
message: data.message,
2021-12-24 20:52:27 +05:00
channel: guild.channels.cache.find((ch) => "#" + ch.name === data.channel).id,
2021-12-10 21:39:54 +05:00
withImage: data.withImage === "on"
};
guildData.plugins.welcome = welcome;
guildData.markModified("plugins.welcome");
await guildData.save();
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
if (Object.prototype.hasOwnProperty.call(data, "welcomeDisable")) {
const welcome = {
enabled: false,
message: null,
channel: null,
withImage: null
};
guildData.plugins.welcome = welcome;
guildData.markModified("plugins.welcome");
await guildData.save();
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
if (Object.prototype.hasOwnProperty.call(data, "goodbyeEnable") || Object.prototype.hasOwnProperty.call(data, "goodbyeUpdate")) {
const goodbye = {
enabled: true,
message: data.message,
2021-12-24 20:52:27 +05:00
channel: guild.channels.cache.find((ch) => "#" + ch.name === data.channel).id,
2021-12-10 21:39:54 +05:00
withImage: data.withImage === "on"
};
guildData.plugins.goodbye = goodbye;
guildData.markModified("plugins.goodbye");
await guildData.save();
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
if (Object.prototype.hasOwnProperty.call(data, "goodbyeDisable")) {
const goodbye = {
enabled: false,
message: null,
channel: null,
withImage: null
};
guildData.plugins.goodbye = goodbye;
guildData.markModified("plugins.goodbye");
await guildData.save();
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
if (Object.prototype.hasOwnProperty.call(data, "autoroleEnable") || Object.prototype.hasOwnProperty.call(data, "autoroleUpdate")) {
const autorole = {
enabled: true,
2021-12-24 20:52:27 +05:00
role: guild.roles.cache.find((r) => "@" + r.name === data.role).id
2021-12-10 21:39:54 +05:00
};
guildData.plugins.autorole = autorole;
guildData.markModified("plugins.autorole");
await guildData.save();
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
if (Object.prototype.hasOwnProperty.call(data, "autoroleDisable")) {
const autorole = {
enabled: false,
role: null
};
guildData.plugins.autorole = autorole;
guildData.markModified("plugins.autorole");
await guildData.save();
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
if (Object.prototype.hasOwnProperty.call(data, "suggestions")) {
2021-12-16 23:42:58 +05:00
if (data.suggestions === req.translate("common:NO_CHANNEL")) guildData.plugins.suggestions = false;
2021-12-24 20:52:27 +05:00
else guildData.plugins.suggestions = guild.channels.cache.find((ch) => "#" + ch.name === data.suggestions).id;
2021-12-16 23:42:58 +05:00
if (data.modlogs === req.translate("common:NO_CHANNEL")) guildData.plugins.modlogs = false;
2021-12-24 20:52:27 +05:00
else guildData.plugins.modlogs = guild.channels.cache.find((ch) => "#" + ch.name === data.modlogs).id;
2021-12-16 23:42:58 +05:00
2021-12-30 02:03:21 +05:00
if (data.birthdays === req.translate("common:NO_CHANNEL")) guildData.plugins.birthdays = false;
else guildData.plugins.birthdays = guild.channels.cache.find((ch) => "#" + ch.name === data.birthdays).id;
2021-12-10 21:39:54 +05:00
guildData.markModified("plugins");
2022-01-13 00:26:23 +05:00
}
2021-12-10 21:39:54 +05:00
await guildData.save();
2021-12-24 20:52:27 +05:00
res.redirect(303, `/manage/${guild.id}`);
2021-12-10 21:39:54 +05:00
});
module.exports = router;