From 47e7c107c13da9cbc25e1d14f23ec3ca5848bdc5 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Mon, 8 May 2023 02:40:24 +0200 Subject: [PATCH] Add handling for interactions in execution.ts this in future will be used to handle interaction requests. --- src/execution.ts | 53 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/src/execution.ts b/src/execution.ts index 13cffa4..f8450cd 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -6,7 +6,17 @@ import config from "./config"; import toOpenAIMessages from "./toOpenAIMessages"; /** Stores the queue requests on the channels. */ -const channelsRunning: DiscordApi.Collection = new DiscordApi.Collection(); +const channelsRunning: DiscordApi.Collection = new DiscordApi.Collection(); + +/** + * Gets the user that requested the execution + * @param request The request to get the user from + * @returns the user or guild member + */ +export function getAuthor(request: DiscordApi.Message | DiscordApi.Interaction) { + if (request instanceof DiscordApi.Message) return request.member ?? request.author; + return request.user; +} /** * gets user remaining limit (or lack of it) @@ -42,31 +52,46 @@ async function getUserLimit(user: string | {id: string}, requestTimestamp: Date) /** * Check and queues up the request and runs it if there is nothing in queue. - * @param message the message to check and queue + * @param request the message to check and queue */ -export async function queueRequest(message: DiscordApi.Message) { +export async function queueRequest(request: DiscordApi.Message | DiscordApi.Interaction) { + if (!request.channelId) { + if (!(request instanceof DiscordApi.Message) && request.isRepliable()) + request.reply("request does not have channelId"); + console.log("There was incoming execution without channelId set, ignoring"); + console.log(request); + return; + } - const userLimit = await getUserLimit(message.author, message.createdAt); + const userLimit = await getUserLimit(getAuthor(request), request.createdAt); if (userLimit !== false && userLimit.remaining <= 0) { - message.react("🛑"); - message.author.dmChannel?.send({ - embeds: [{ - color: 0xff0000, - description: `You've used up your message limit for today, ${userLimit.limit} requrests in last 24 hours` - }] - }); + if (request instanceof DiscordApi.Message) { + request.react("🛑"); + request.author.dmChannel?.send({ + embeds: [{ + color: 0xff0000, + description: `You've used up your message limit for today, ${userLimit.limit} requests in last 24 hours`, + }] + }); + } + else if (request.isRepliable()) { + request.reply({ + content: `You've used up your message limit for today, ${userLimit.limit} requests in last 24 hours`, + ephemeral: true, + }); + } return; } const messagesForChannel = channelsRunning.ensure( - message.channelId, + request.channelId, () => { return [] as DiscordApi.Message[]; }, ); const shouldStart = messagesForChannel.length == 0; - messagesForChannel.push(message); + messagesForChannel.push(request); if (shouldStart) - executeFromQueue(message.channelId); + executeFromQueue(request.channelId); } /**