Add handling for interactions in execution.ts
this in future will be used to handle interaction requests.
This commit is contained in:
parent
cb304f522b
commit
47e7c107c1
1 changed files with 39 additions and 14 deletions
|
@ -6,7 +6,17 @@ import config from "./config";
|
||||||
import toOpenAIMessages from "./toOpenAIMessages";
|
import toOpenAIMessages from "./toOpenAIMessages";
|
||||||
|
|
||||||
/** Stores the queue requests on the channels. */
|
/** Stores the queue requests on the channels. */
|
||||||
const channelsRunning: DiscordApi.Collection<string, DiscordApi.Message[]> = new DiscordApi.Collection();
|
const channelsRunning: DiscordApi.Collection<string, (DiscordApi.Message | DiscordApi.Interaction)[]> = 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)
|
* 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.
|
* 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) {
|
if (userLimit !== false && userLimit.remaining <= 0) {
|
||||||
message.react("🛑");
|
if (request instanceof DiscordApi.Message) {
|
||||||
message.author.dmChannel?.send({
|
request.react("🛑");
|
||||||
|
request.author.dmChannel?.send({
|
||||||
embeds: [{
|
embeds: [{
|
||||||
color: 0xff0000,
|
color: 0xff0000,
|
||||||
description: `You've used up your message limit for today, ${userLimit.limit} requrests in last 24 hours`
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const messagesForChannel = channelsRunning.ensure(
|
const messagesForChannel = channelsRunning.ensure(
|
||||||
message.channelId,
|
request.channelId,
|
||||||
() => { return [] as DiscordApi.Message[]; },
|
() => { return [] as DiscordApi.Message[]; },
|
||||||
);
|
);
|
||||||
const shouldStart = messagesForChannel.length == 0;
|
const shouldStart = messagesForChannel.length == 0;
|
||||||
messagesForChannel.push(message);
|
messagesForChannel.push(request);
|
||||||
if (shouldStart)
|
if (shouldStart)
|
||||||
executeFromQueue(message.channelId);
|
executeFromQueue(request.channelId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue