From e6d6764e341dd84d7659758fc0600eb3ac20d348 Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Wed, 13 Sep 2023 04:06:30 +0200 Subject: [PATCH] add user request limit to config, rename previous limit to read limit --- src/config_example.ts | 7 ++++++- src/execution.ts | 6 +++--- src/moderation.ts | 2 +- src/toOpenAIMessages.ts | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/config_example.ts b/src/config_example.ts index 6e1af96..ae7e88e 100644 --- a/src/config_example.ts +++ b/src/config_example.ts @@ -33,7 +33,7 @@ export default class config { }; /** limits for message selection */ - static readonly limits = { + static readonly readLimits = { /** maximum time in the past for messages (in miliseconds) */ time: 60*60*1000, /** maximum number of messages to select (maximum 100) */ @@ -41,4 +41,9 @@ export default class config { /** maximum total token usage for messages */ tokens: 2048, }; + /** default user limits */ + static readonly userLimits = { + /** how much requests can an user make if it's not overriden in database entry */ + requests: 25, + }; } diff --git a/src/execution.ts b/src/execution.ts index 8879b00..3cd0e8f 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -86,7 +86,7 @@ export async function getUserLimit(user: string | { id: string }, requestTimesta }, }))._all; - if (!userLimits || !userLimits.limit) return {limit: 25, remaining: 25 - usedLimit}; + if (!userLimits || !userLimits.limit) return {limit: config.userLimits.requests, remaining: config.userLimits.requests - usedLimit}; return {limit: userLimits.limit, remaining: userLimits.limit - usedLimit}; } @@ -261,9 +261,9 @@ async function executeFromQueue(channel: string) { if (!canReplyToRequest(message)) return; try { - let messages: DiscordApi.Collection = await message.channel.messages.fetch({ limit: config.limits.messages, cache: false }); + let messages: DiscordApi.Collection = await message.channel.messages.fetch({ limit: config.readLimits.messages, cache: false }); - messages = messages.filter(m => message.createdTimestamp - m.createdTimestamp < config.limits.time ); + messages = messages.filter(m => message.createdTimestamp - m.createdTimestamp < config.readLimits.time ); messages.forEach(m => { Moderation.checkMessageNoReturn(m); }); diff --git a/src/moderation.ts b/src/moderation.ts index c896ac8..d949b12 100644 --- a/src/moderation.ts +++ b/src/moderation.ts @@ -60,7 +60,7 @@ export default class Moderation { public static removeExpiredCacheEntries() { const now = Date.now(); for (const i of this.cache.keys()) { - if (now - SnowflakeUtil.timestampFrom(i) >= config.limits.time * 2) { + if (now - SnowflakeUtil.timestampFrom(i) >= config.readLimits.time * 2) { this.cache.delete(i); } } diff --git a/src/toOpenAIMessages.ts b/src/toOpenAIMessages.ts index e868b45..3f3c63c 100644 --- a/src/toOpenAIMessages.ts +++ b/src/toOpenAIMessages.ts @@ -107,7 +107,7 @@ export default function toOpenAIMessages( const content = formatMessage(message); // FIXME: tokens are not being counted properly (it's lower than it is) but it's enough for me for now. tokenCount += countTokens(content); - if (tokenCount > config.limits.tokens) break; + if (tokenCount > config.readLimits.tokens) break; rvalue.push({ role: message.author.id === message.client.user.id ? "assistant" : "user", content: content,