From aafefc3ad0715764daec1e6e5841ba7b51059469 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Sun, 19 Mar 2023 02:26:28 +0100 Subject: [PATCH] Fix DiscordAPIError on receiving empty content. Previously, when OpenAI returned a message with an empty string, it would try to send that empty message, which throwed an error. Now it will react with an emoji to the message that triggered the request. --- src/index.ts | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 63f4424..0117606 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,20 +39,27 @@ discord.on("messageCreate", async message => { max_tokens: 168 }); - const response = message.reply({ - content: answer.data.choices[0].message?.content, - allowedMentions: { - repliedUser: false, - } - }); - const usage = answer.data.usage; if (usage != undefined) { const channelName: string = message.inGuild() ? `${message.channel.name} (${message.guild.name})` : `@${message.author.tag}`; console.log(`Used ${usage.total_tokens} (${usage.prompt_tokens} + ${usage.completion_tokens}) tokens for ${message.author.tag} (${message.author.id}) in #${channelName}`); } - Moderation.checkMessage(await response); + const answerContent = answer.data.choices[0].message?.content; + + if (answerContent != undefined && answerContent != "") { + const response = message.reply({ + content: answerContent, + allowedMentions: { + repliedUser: false, + } + }); + + Moderation.checkMessage(await response); + } + else { + message.react("😶"); + } } catch (e) { console.error(e);