From 2fab1b1b428fa9fcd9b8c02e18cdb3423e2abd51 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Fri, 26 Apr 2024 05:17:32 +0200 Subject: [PATCH] execution: factor out replying code to it's own fuction --- src/execution.ts | 49 ++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/execution.ts b/src/execution.ts index 4010b51..a78131f 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -252,26 +252,7 @@ async function executeFromQueue(channel: string) { const answerContent = answer.choices[0].message?.content; - if (answerContent === null || answerContent === "") { - if (message instanceof DiscordApi.Message) message.react("😶").catch(() => {/* GRACEFAIL: It's okay if the bot won't reply */}); - } - else { - const answerMessagesContent :string[] = [""]; - for (const i of answerContent.split(/\n\n/)) { - if (answerMessagesContent[answerMessagesContent.length-1].length + i.length < 2000) { - answerMessagesContent[answerMessagesContent.length-1] += "\n\n" + i; - } - else { - answerMessagesContent.push(i); - } - } - - for (const i of answerMessagesContent) { - const response = requestReply(message, {content: i}, {allowedMentions: { repliedUser: false }}); - - await response.then(rval => Moderation.checkMessageNoReturn(rval)); - } - } + await replyInMultiMessage(answerContent, message); } catch (e) { let errorText: string = ""; channelQueue.stopTyping(); @@ -326,6 +307,34 @@ async function executeFromQueue(channel: string) { return executeFromQueue(channel); } +/** + * Replies to a message and splits to multiple messages if needed. + * @param answerContent - The content of the answer. + * @param message - The request message to reply to. + */ +async function replyInMultiMessage(answerContent: string | null, message: RequestMessage) { + if (answerContent === null || answerContent === "") { + if (message instanceof DiscordApi.Message) message.react("😶").catch(() => { }); + } + else { + const answerMessagesContent: string[] = [""]; + for (const i of answerContent.split(/\n\n/)) { + if (answerMessagesContent[answerMessagesContent.length - 1].length + i.length < 2000) { + answerMessagesContent[answerMessagesContent.length - 1] += "\n\n" + i; + } + else { + answerMessagesContent.push(i); + } + } + + for (const i of answerMessagesContent) { + const response = requestReply(message, { content: i }, { allowedMentions: { repliedUser: false } }); + + await response.then(rval => Moderation.checkMessageNoReturn(rval)); + } + } +} + /** * Executes the chat completion process. *