From 3ca54654e2ce0b1579d6e6906f9804c27bf8e5a2 Mon Sep 17 00:00:00 2001 From: Carolina Mitchell Date: Fri, 29 Mar 2024 18:25:15 +1300 Subject: [PATCH] Prettify code --- src/Routes/bots.ts | 155 ++++++++++++++++++++--------------- src/Routes/search.ts | 2 +- src/Routes/servers.ts | 56 +++++++------ src/Routes/templates.ts | 4 +- src/Util/Services/banned.ts | 6 +- src/Util/Services/discord.ts | 27 +++--- src/app.ts | 28 ++++--- 7 files changed, 160 insertions(+), 118 deletions(-) diff --git a/src/Routes/bots.ts b/src/Routes/bots.ts index 64f163ea..4a341663 100644 --- a/src/Routes/bots.ts +++ b/src/Routes/bots.ts @@ -144,7 +144,10 @@ router.post( } if (req.body.clientID) { - if (Number.isNaN(req.body.clientID) || req.body.clientID.includes(" ")) { + if ( + Number.isNaN(req.body.clientID) || + req.body.clientID.includes(" ") + ) { error = true; errors.push(res.__("common.error.bot.arr.invalidClientID")); } @@ -287,24 +290,26 @@ router.post( query: `{guild(id:"${req.body.widgetServer}"){id}}` }), headers: { "Content-Type": "application/json" } - }).then(async (fetchRes: fetchRes) => { - const data: any = await fetchRes.json(); - if (data && !data.guild?.id) { + }) + .then(async (fetchRes: fetchRes) => { + const data: any = await fetchRes.json(); + if (data && !data.guild?.id) { + error = true; + errors.push( + res.__( + "common.error.listing.arr.widgetbot.guildNotFound" + ) + ); + } + }) + .catch(() => { error = true; errors.push( res.__( "common.error.listing.arr.widgetbot.guildNotFound" ) ); - } - }).catch(() => { - error = true; - errors.push( - res.__( - "common.error.listing.arr.widgetbot.guildNotFound" - ) - ); - }); + }); let fetchChannel = true; @@ -352,24 +357,26 @@ router.post( query: `{channel(id:"${req.body.widgetChannel}"){id}}` }), headers: { "Content-Type": "application/json" } - }).then(async (fetchRes: fetchRes) => { - const data: any = await fetchRes.json(); - if (!data.channel?.id) { + }) + .then(async (fetchRes: fetchRes) => { + const data: any = await fetchRes.json(); + if (!data.channel?.id) { + error = true; + errors.push( + res.__( + "common.error.listing.arr.widgetbot.channelNotFound" + ) + ); + } + }) + .catch(() => { error = true; errors.push( res.__( "common.error.listing.arr.widgetbot.channelNotFound" ) ); - } - }).catch(() => { - error = true; - errors.push( - res.__( - "common.error.listing.arr.widgetbot.channelNotFound" - ) - ); - }); + }); } if (req.body.twitter?.length > 15) { @@ -591,7 +598,7 @@ router.post( shortDesc: req.body.shortDescription, longDesc: req.body.longDescription, modNotes: req.body.modNotes, - lastDenyReason: botExists.lastDenyReason || '', + lastDenyReason: botExists.lastDenyReason || "", reviewNotes: [], editors, commands, @@ -682,7 +689,7 @@ router.post( shortDesc: req.body.shortDescription, longDesc: req.body.longDescription, modNotes: req.body.modNotes, - lastDenyReason: botExists.lastDenyReason || '', + lastDenyReason: botExists.lastDenyReason || "", reviewNotes: [], editors, commands, @@ -1184,7 +1191,10 @@ router.post( } if (req.body.clientID) { - if (Number.isNaN(req.body.clientID) || req.body.clientID.includes(" ")) { + if ( + Number.isNaN(req.body.clientID) || + req.body.clientID.includes(" ") + ) { error = true; errors.push(res.__("common.error.bot.arr.invalidClientID")); } @@ -1366,24 +1376,26 @@ router.post( query: `{guild(id:"${req.body.widgetServer}"){id}}` }), headers: { "Content-Type": "application/json" } - }).then(async (fetchRes: fetchRes) => { - const data: any = await fetchRes.json(); - if (data && !data.guild?.id) { + }) + .then(async (fetchRes: fetchRes) => { + const data: any = await fetchRes.json(); + if (data && !data.guild?.id) { + error = true; + errors.push( + res.__( + "common.error.listing.arr.widgetbot.guildNotFound" + ) + ); + } + }) + .catch(() => { error = true; errors.push( res.__( "common.error.listing.arr.widgetbot.guildNotFound" ) ); - } - }).catch(() => { - error = true; - errors.push( - res.__( - "common.error.listing.arr.widgetbot.guildNotFound" - ) - ); - }); + }); let fetchChannel = true; @@ -1431,24 +1443,26 @@ router.post( query: `{channel(id:"${req.body.widgetChannel}"){id}}` }), headers: { "Content-Type": "application/json" } - }).then(async (fetchRes: fetchRes) => { - const data: any = await fetchRes.json(); - if (!data.channel?.id) { + }) + .then(async (fetchRes: fetchRes) => { + const data: any = await fetchRes.json(); + if (!data.channel?.id) { + error = true; + errors.push( + res.__( + "common.error.listing.arr.widgetbot.channelNotFound" + ) + ); + } + }) + .catch(() => { error = true; errors.push( res.__( "common.error.listing.arr.widgetbot.channelNotFound" ) ); - } - }).catch(() => { - error = true; - errors.push( - res.__( - "common.error.listing.arr.widgetbot.channelNotFound" - ) - ); - }); + }); } if (req.body.twitter?.length > 15) { @@ -1965,7 +1979,9 @@ router.get("/:id", variables, async (req: Request, res: Response) => { }); router.get("/:id/exists", permission.auth, async (req, res) => { - res.type("text").send(String(await global.redis?.hexists("bots", req.params.id))); + res.type("text").send( + String(await global.redis?.hexists("bots", req.params.id)) + ); }); router.get( @@ -2576,7 +2592,10 @@ router.post( } if (req.body.clientID) { - if (Number.isNaN(req.body.clientID) || req.body.clientID.includes(" ")) { + if ( + Number.isNaN(req.body.clientID) || + req.body.clientID.includes(" ") + ) { error = true; errors.push(res.__("common.error.bot.arr.invalidClientID")); } @@ -2758,24 +2777,26 @@ router.post( query: `{guild(id:"${req.body.widgetServer}"){id}}` }), headers: { "Content-Type": "application/json" } - }).then(async (fetchRes: fetchRes) => { - const data: any = await fetchRes.json(); - if (data && !data.guild?.id) { + }) + .then(async (fetchRes: fetchRes) => { + const data: any = await fetchRes.json(); + if (data && !data.guild?.id) { + error = true; + errors.push( + res.__( + "common.error.listing.arr.widgetbot.guildNotFound" + ) + ); + } + }) + .catch(() => { error = true; errors.push( res.__( "common.error.listing.arr.widgetbot.guildNotFound" ) ); - } - }).catch(() => { - error = true; - errors.push( - res.__( - "common.error.listing.arr.widgetbot.guildNotFound" - ) - ); - }); + }); let fetchChannel = true; @@ -3566,7 +3587,7 @@ router.post( $set: { vanityUrl: "", lastDenyReason: req.body.reason, - "status.archived": true, + "status.archived": true } } ); diff --git a/src/Routes/search.ts b/src/Routes/search.ts index e0125f54..f6b9d1ab 100644 --- a/src/Routes/search.ts +++ b/src/Routes/search.ts @@ -52,7 +52,7 @@ router.get("/", variables, (req: Request, res: Response) => { router.post("/", variables, async (req: Request, res: Response) => { let { query, only }: { query: string; only: string[] } = req.body; - if (!query || typeof(query) !== "string" || typeof(only) !== "object") + if (!query || typeof query !== "string" || typeof only !== "object") return res.status(400).json({ error: true, status: 400, diff --git a/src/Routes/servers.ts b/src/Routes/servers.ts index a731cb62..48b225ff 100644 --- a/src/Routes/servers.ts +++ b/src/Routes/servers.ts @@ -201,24 +201,26 @@ router.post( query: `{channel(id:"${req.body.previewChannel}"){id}}` }), headers: { "Content-Type": "application/json" } - }).then(async (fetchRes: fetchRes) => { - const data: any = await fetchRes.json(); - if (!data.channel?.id) { + }) + .then(async (fetchRes: fetchRes) => { + const data: any = await fetchRes.json(); + if (!data.channel?.id) { + error = true; + errors.push( + res.__( + "common.error.listing.arr.widgetbot.channelNotFound" + ) + ); + } + }) + .catch(() => { error = true; errors.push( res.__( "common.error.listing.arr.widgetbot.channelNotFound" ) ); - } - }).catch(() => { - error = true; - errors.push( - res.__( - "common.error.listing.arr.widgetbot.channelNotFound" - ) - ); - }); + }); } if (!req.body.shortDescription) { @@ -438,7 +440,9 @@ router.get("/:id", variables, async (req: Request, res: Response) => { }); router.get("/:id/exists", permission.auth, async (req, res) => { - res.type('text').send(String(await global.redis?.hexists("servers", req.params.id))); + res.type("text").send( + String(await global.redis?.hexists("servers", req.params.id)) + ); }); router.get( @@ -628,24 +632,26 @@ router.post( query: `{channel(id:"${req.body.previewChannel}"){id}}` }), headers: { "Content-Type": "application/json" } - }).then(async (fetchRes: fetchRes) => { - const data: any = await fetchRes.json(); - if (!data.channel?.id) { + }) + .then(async (fetchRes: fetchRes) => { + const data: any = await fetchRes.json(); + if (!data.channel?.id) { + error = true; + errors.push( + res.__( + "common.error.listing.arr.widgetbot.channelNotFound" + ) + ); + } + }) + .catch(() => { error = true; errors.push( res.__( "common.error.listing.arr.widgetbot.channelNotFound" ) ); - } - }).catch(() => { - error = true; - errors.push( - res.__( - "common.error.listing.arr.widgetbot.channelNotFound" - ) - ); - }); + }); } if (!req.body.shortDescription) { diff --git a/src/Routes/templates.ts b/src/Routes/templates.ts index b6988cee..01ab52b3 100644 --- a/src/Routes/templates.ts +++ b/src/Routes/templates.ts @@ -356,7 +356,9 @@ router.get("/:id", variables, async (req: Request, res: Response) => { }); router.get("/:id/exists", permission.auth, async (req, res) => { - res.type('text').send(String(await global.redis?.hexists("templates", req.params.id))); + res.type("text").send( + String(await global.redis?.hexists("templates", req.params.id)) + ); }); router.get( diff --git a/src/Util/Services/banned.ts b/src/Util/Services/banned.ts index 85660a65..3e9abdfd 100644 --- a/src/Util/Services/banned.ts +++ b/src/Util/Services/banned.ts @@ -29,7 +29,11 @@ export async function updateBanlist() { await guilds.main.bans .fetch() .then(async (bans) => { - if (bans.size > 0) await global.redis?.hmset("bans", ...bans.map((ban) => [ban.user.id, true])); + if (bans.size > 0) + await global.redis?.hmset( + "bans", + ...bans.map((ban) => [ban.user.id, true]) + ); }) .catch((e) => console.error(e)); } diff --git a/src/Util/Services/discord.ts b/src/Util/Services/discord.ts index 82f671b5..542a5b5b 100644 --- a/src/Util/Services/discord.ts +++ b/src/Util/Services/discord.ts @@ -73,17 +73,22 @@ bot.on("ready", async () => { ); } else { console.time("Cache: Bot cache"); - botCache.getAllBots().then(async (bots) => { - const botsToFetch = []; - bots.forEach(async (bot) => { - if (guilds.main.members.cache.has(bot._id)) - botsToFetch.push(bot._id); - }); - guilds.main.members - .fetch({ user: botsToFetch }) - .then((x) => console.log(`Discord: Retrieved ${x.size} members!`)) - .catch(() => null); // It is most likely that DEL has another instance running to handle this, so catch the error and ignore. - }).catch((e) => console.error(e)); + botCache + .getAllBots() + .then(async (bots) => { + const botsToFetch = []; + bots.forEach(async (bot) => { + if (guilds.main.members.cache.has(bot._id)) + botsToFetch.push(bot._id); + }); + guilds.main.members + .fetch({ user: botsToFetch }) + .then((x) => + console.log(`Discord: Retrieved ${x.size} members!`) + ) + .catch(() => null); // It is most likely that DEL has another instance running to handle this, so catch the error and ignore. + }) + .catch((e) => console.error(e)); console.timeEnd("Cache: Bot cache"); await global.redis.del("fetch_lock"); } diff --git a/src/app.ts b/src/app.ts index d4c186c5..e55cbb6c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -180,7 +180,9 @@ new Promise((resolve, reject) => { }); }); } else { - console.log("Cache: No one has the cache lock currently, acquiring it."); + console.log( + "Cache: No one has the cache lock currently, acquiring it." + ); // 300 seconds is a good rule of thumb, it is expected that DEL has another instance running. await global.redis.setex("fetch_lock", 300, hostname()); console.log("Discord: Also acquired the discord lock!"); @@ -255,17 +257,19 @@ new Promise((resolve, reject) => { app.set("trust proxy", 1); // trust first proxy } - app.use(session({ - store: new RedisStore({ client: global.redis }), - secret: settings.secrets.cookie, - resave: false, - saveUninitialized: false, - cookie: { - secure: global.env_prod, - httpOnly: true, - maxAge: 1000 * 60 * 60 * 3 - } - })); + app.use( + session({ + store: new RedisStore({ client: global.redis }), + secret: settings.secrets.cookie, + resave: false, + saveUninitialized: false, + cookie: { + secure: global.env_prod, + httpOnly: true, + maxAge: 1000 * 60 * 60 * 3 + } + }) + ); app.use(passport.initialize()); app.use(passport.session());