From dffb13361c852457046c991cf750a278ae70db11 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Mon, 20 Mar 2023 06:08:42 +0100 Subject: [PATCH] Add try-catch in moderation when checking with moderation api Now it won't crash the bot when the moderation api is not availabe (somehow) --- src/moderation.ts | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/moderation.ts b/src/moderation.ts index 293753e..a7ce63c 100644 --- a/src/moderation.ts +++ b/src/moderation.ts @@ -17,15 +17,21 @@ export default class Moderation { this.cache.set(message.id, true); return true; } + + try{ + const answer = await openai.createModeration({ + input: formatMessage(message), + }); - const answer = await openai.createModeration({ - input: formatMessage(message), - }); + const flagged = answer.data.results[0].flagged; + this.cache.set(message.id, flagged); + if (flagged) message.react("⚠"); - const flagged = answer.data.results[0].flagged; - this.cache.set(message.id, flagged); - if (flagged) message.react("⚠"); - - return flagged; + return flagged; + } + catch (e) { + console.log(e); + return false; + } } }