From aa97a1514c91563bf22b31bb580581aa453dca17 Mon Sep 17 00:00:00 2001 From: "Ean Milligan (Bastion)" Date: Sun, 22 May 2022 16:59:40 -0400 Subject: [PATCH] Sonar Cleanup - Phase 5 --- src/commands/apiCmd/allowBlock.ts | 10 +++++++--- src/commands/apiCmd/deleteGuild.ts | 4 +++- src/commands/apiCmd/showHideWarn.ts | 10 +++++++--- src/commands/apiCmd/status.ts | 4 +++- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/commands/apiCmd/allowBlock.ts b/src/commands/apiCmd/allowBlock.ts index be51dba..20d47e8 100644 --- a/src/commands/apiCmd/allowBlock.ts +++ b/src/commands/apiCmd/allowBlock.ts @@ -9,14 +9,17 @@ import { import { generateApiFailed, generateApiSuccess } from '../../constantCmds.ts'; export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => { + let errorOutInitial = false; const guildQuery = await dbClient.query(`SELECT guildid, channelid FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch((e0) => { log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`); message.send(generateApiFailed(apiArg)).catch((e1) => { log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); }); - return; + errorOutInitial = true; }); + if (errorOutInitial) return; + let errorOut = false; if (guildQuery.length === 0) { // Since guild is not in our DB, add it in await dbClient.execute(`INSERT INTO allowed_guilds(guildid,channelid,active) values(?,?,?)`, [message.guildId, message.channelId, (apiArg === 'allow' || apiArg === 'enable') ? 1 : 0]).catch( @@ -25,7 +28,7 @@ export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => message.send(generateApiFailed(apiArg)).catch((e1) => { log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); }); - return; + errorOut = true; }, ); } else { @@ -36,10 +39,11 @@ export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => message.send(generateApiFailed(apiArg)).catch((e1) => { log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); }); - return; + errorOut = true; }, ); } + if (errorOut) return; // We won't get here if there's any errors, so we know it has bee successful, so report as such message.send(generateApiSuccess(`${apiArg}ed`)).catch((e) => { diff --git a/src/commands/apiCmd/deleteGuild.ts b/src/commands/apiCmd/deleteGuild.ts index f6c99a1..bb0e5af 100644 --- a/src/commands/apiCmd/deleteGuild.ts +++ b/src/commands/apiCmd/deleteGuild.ts @@ -9,13 +9,15 @@ import { import { constantCmds } from '../../constantCmds.ts'; export const deleteGuild = async (message: DiscordenoMessage) => { + let errorOut = false; await dbClient.execute(`DELETE FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch((e0) => { log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`); message.send(constantCmds.apiDeleteFail).catch((e1) => { log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); }); - return; + errorOut = true; }); + if (errorOut) return; // We won't get here if there's any errors, so we know it has bee successful, so report as such message.send(constantCmds.apiRemoveGuild).catch((e) => { diff --git a/src/commands/apiCmd/showHideWarn.ts b/src/commands/apiCmd/showHideWarn.ts index 47e3abf..c0c087f 100644 --- a/src/commands/apiCmd/showHideWarn.ts +++ b/src/commands/apiCmd/showHideWarn.ts @@ -9,14 +9,17 @@ import { import { generateApiFailed, generateApiSuccess } from '../../constantCmds.ts'; export const showHideWarn = async (message: DiscordenoMessage, apiArg: string) => { + let errorOutInitial = false; const guildQuery = await dbClient.query(`SELECT guildid, channelid FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch((e0) => { log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`); message.send(generateApiFailed(`${apiArg} on`)).catch((e1) => { log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); }); - return; + errorOutInitial = true; }); + if (errorOutInitial) return; + let errorOut = false; if (guildQuery.length === 0) { // Since guild is not in our DB, add it in await dbClient.execute(`INSERT INTO allowed_guilds(guildid,channelid,hidewarn) values(?,?,?)`, [message.guildId, message.channelId, (apiArg === 'hide-warn') ? 1 : 0]).catch((e0) => { @@ -24,7 +27,7 @@ export const showHideWarn = async (message: DiscordenoMessage, apiArg: string) = message.send(generateApiFailed(`${apiArg} on`)).catch((e1) => { log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); }); - return; + errorOut = true; }); } else { // Since guild is in our DB, update it @@ -33,9 +36,10 @@ export const showHideWarn = async (message: DiscordenoMessage, apiArg: string) = message.send(generateApiFailed(`${apiArg} on`)).catch((e1) => { log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); }); - return; + errorOut = true; }); } + if (errorOut) return; // We won't get here if there's any errors, so we know it has bee successful, so report as such message.send(generateApiSuccess(apiArg)).catch((e) => { diff --git a/src/commands/apiCmd/status.ts b/src/commands/apiCmd/status.ts index f42836f..f999795 100644 --- a/src/commands/apiCmd/status.ts +++ b/src/commands/apiCmd/status.ts @@ -10,13 +10,15 @@ import { constantCmds, generateApiStatus } from '../../constantCmds.ts'; export const status = async (message: DiscordenoMessage) => { // Get status of guild from the db + let errorOut = false; const guildQuery = await dbClient.query(`SELECT active, banned FROM allowed_guilds WHERE guildid = ? AND channelid = ?`, [message.guildId, message.channelId]).catch((e0) => { log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e0)}`); message.send(constantCmds.apiStatusFail).catch((e1) => { log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); }); - return; + errorOut = true; }); + if (errorOut) return; // Check if we got an item back or not if (guildQuery.length > 0) {