From 88faa272782db128dc972404bc395975ba5aa59f Mon Sep 17 00:00:00 2001 From: "Ean Milligan (x1g5)" Date: Mon, 9 May 2022 17:43:15 -0400 Subject: [PATCH] Added hideWarn cmd file with template code --- src/commands/apiCmd.ts | 7 +++++ src/commands/apiCmd/_index.ts | 4 ++- src/commands/apiCmd/allowBlock.ts | 3 +- src/commands/apiCmd/showHideWarn.ts | 44 +++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/commands/apiCmd/showHideWarn.ts diff --git a/src/commands/apiCmd.ts b/src/commands/apiCmd.ts index 6538a32..3754aa5 100644 --- a/src/commands/apiCmd.ts +++ b/src/commands/apiCmd.ts @@ -51,6 +51,13 @@ export const api = async (message: DiscordenoMessage, args: string[]) => { else if (apiArg === "status") { apiCommands.status(message); } + + // [[api show-warn/hide-warn + // Lets a guild admin decide if the API warning should be shown on messages from the API + else if (apiArg === "show-warn" || apiArg === "hide-warn") { + apiCommands.showHideWarn(message, apiArg); + } + } else { message.send(constantCmds.apiPermError).catch(e => { log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); diff --git a/src/commands/apiCmd/_index.ts b/src/commands/apiCmd/_index.ts index 263dff4..81fc146 100644 --- a/src/commands/apiCmd/_index.ts +++ b/src/commands/apiCmd/_index.ts @@ -2,10 +2,12 @@ import { help } from "./apiHelp.ts"; import { allowBlock } from "./allowBlock.ts"; import { deleteGuild } from "./deleteGuild.ts"; import { status } from "./status.ts"; +import { showHideWarn } from "./showHideWarn.ts"; export default { help, allowBlock, deleteGuild, - status + status, + showHideWarn }; diff --git a/src/commands/apiCmd/allowBlock.ts b/src/commands/apiCmd/allowBlock.ts index a056645..841e9ab 100644 --- a/src/commands/apiCmd/allowBlock.ts +++ b/src/commands/apiCmd/allowBlock.ts @@ -19,7 +19,7 @@ export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => 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(e0 => { + await dbClient.execute(`INSERT INTO allowed_guilds(guildid,channelid,active) values(?,?,?)`, [message.guildId, message.channelId, ((apiArg === "allow" || apiArg === "enable") ? 1 : 0)]).catch(e0 => { log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e0)}`); message.send(generateApiFailed(apiArg)).catch(e1 => { log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); @@ -36,6 +36,7 @@ export const allowBlock = async (message: DiscordenoMessage, apiArg: string) => 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 => { log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); diff --git a/src/commands/apiCmd/showHideWarn.ts b/src/commands/apiCmd/showHideWarn.ts new file mode 100644 index 0000000..85445b4 --- /dev/null +++ b/src/commands/apiCmd/showHideWarn.ts @@ -0,0 +1,44 @@ +import { dbClient } from "../../db.ts"; +import { + // Discordeno deps + DiscordenoMessage, + + // Log4Deno deps + LT, log +} from "../../../deps.ts"; +import { generateApiFailed, generateApiSuccess } from "../../constantCmds.ts"; + +export const showHideWarn = async (message: DiscordenoMessage, apiArg: string) => { + 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; + }); + + 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 => { + log(LT.ERROR, `Failed to insert into DB: ${JSON.stringify(e0)}`); + message.send(generateApiFailed(apiArg)).catch(e1 => { + log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); + }); + return; + }); + } else { + // Since guild is in our DB, update it + await dbClient.execute(`UPDATE allowed_guilds SET hidewarn = ? WHERE guildid = ? AND channelid = ?`, [((apiArg === "hide-warn") ? 1 : 0), message.guildId, message.channelId]).catch(e0 => { + log(LT.ERROR, `Failed to update DB: ${JSON.stringify(e0)}`); + message.send(generateApiFailed(apiArg)).catch(e1 => { + log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e1)}`); + }); + 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 => { + log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`); + }); +};