execution: factor out replying code to it's own fuction

This commit is contained in:
Wroclaw 2024-04-26 05:17:32 +02:00
parent 482f72a4d1
commit 2fab1b1b42

View file

@ -252,26 +252,7 @@ async function executeFromQueue(channel: string) {
const answerContent = answer.choices[0].message?.content; const answerContent = answer.choices[0].message?.content;
if (answerContent === null || answerContent === "") { await replyInMultiMessage(answerContent, message);
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));
}
}
} catch (e) { } catch (e) {
let errorText: string = ""; let errorText: string = "";
channelQueue.stopTyping(); channelQueue.stopTyping();
@ -326,6 +307,34 @@ async function executeFromQueue(channel: string) {
return executeFromQueue(channel); 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. * Executes the chat completion process.
* *