add user request limit to config, rename previous limit to read limit

This commit is contained in:
Wroclaw 2023-09-13 04:06:30 +02:00
parent 0931fe103d
commit e6d6764e34
4 changed files with 11 additions and 6 deletions

View file

@ -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,
};
}

View file

@ -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<string, DiscordApi.Message> = await message.channel.messages.fetch({ limit: config.limits.messages, cache: false });
let messages: DiscordApi.Collection<string, DiscordApi.Message> = 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); });

View file

@ -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);
}
}

View file

@ -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,