execution: factor out replying code to it's own fuction
This commit is contained in:
parent
482f72a4d1
commit
2fab1b1b42
1 changed files with 29 additions and 20 deletions
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue