diff --git a/src/execution.ts b/src/execution.ts index 434a76c..e55e9b5 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -54,6 +54,32 @@ async function getUserLimit(user: string | {id: string}, requestTimestamp: Date) return {limit: userLimits.limit, remaining: userLimits.limit - usedLimit}; } +/** + * Replies to a request + * @param request the request to reply to + * @param message the message + * @param replyOptions additional options if the request is a message + * @param interactionOptions additional options if the request is an interaction + * @returns Promise of the done action + */ +function requestReply( + request: request, + message: DiscordApi.MessageReplyOptions & DiscordApi.InteractionReplyOptions, + // TODO: add support for these below + replyOptions: DiscordApi.MessageReplyOptions = {}, + interactionOptions: DiscordApi.InteractionReplyOptions = {}, +) { + if (request instanceof DiscordApi.Message) { + return request.reply(Object.assign(message, replyOptions)); + } + else { + if (!request.deferred) + return request.reply(Object.assign(message, interactionOptions)); + else + return request.editReply(Object.assign(message, interactionOptions)); + } +} + /** * Check and queues up the request and runs it if there is nothing in queue. * @param request the message to check and queue @@ -151,14 +177,10 @@ async function executeFromQueue(channel: string) { 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); + const response = requestReply(message, {content: answerContent}, {allowedMentions: { repliedUser: false }}); + + response.then(rval => Moderation.checkMessage(rval)); } else { if (message instanceof DiscordApi.Message) message.react("😶"); @@ -167,15 +189,17 @@ async function executeFromQueue(channel: string) { console.error(`Error ocurred while handling chat completion request (${(e as object).constructor.name}):`); console.error(e); - message.reply({ - embeds: [{ - color: 0xff0000, - description: "Something bad happened! :frowning:" - }], - allowedMentions: { - repliedUser: false, - } - }); + requestReply( + message, + { + embeds: [{ + color: 0xff0000, + description: "Something bad happened! :frowning:" + }], + }, + {allowedMentions: { repliedUser: false } }, + { ephemeral: true }, + ); } channelQueue.shift();