From e5b2c6b3777bf3a93a287d21e9e3c22ea439e314 Mon Sep 17 00:00:00 2001 From: GhomKrosmonaute Date: Mon, 28 Oct 2024 20:31:07 +0100 Subject: [PATCH] updated active member system and command --- src/app.ts | 2 + src/commands/active.ts | 15 ++++ src/commands/leaderboard.ts | 2 - src/listeners/activity.ready.ts | 62 ++------------- src/listeners/helping.footer.messageCreate.ts | 2 +- src/namespaces/active.ts | 79 ++++++++++++++++++- src/namespaces/caches.ts | 8 ++ 7 files changed, 110 insertions(+), 60 deletions(-) create mode 100644 src/namespaces/caches.ts diff --git a/src/app.ts b/src/app.ts index 162adcde..b0bdea4b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -17,6 +17,7 @@ export * from "./namespaces/emotes.ts" export * from "./namespaces/remind.ts" export * from "./namespaces/reply.ts" export * from "./namespaces/coins.ts" +export * from "./namespaces/caches.ts" export * as Discord from "discord.js" export * as ladder from "./namespaces/ladder.ts" @@ -35,3 +36,4 @@ export * as emotes from "./namespaces/emotes.ts" export * as remind from "./namespaces/remind.ts" export * as reply from "./namespaces/reply.ts" export * as coins from "./namespaces/coins.ts" +export * as caches from "./namespaces/caches.ts" diff --git a/src/commands/active.ts b/src/commands/active.ts index 776e7940..b1ff8b92 100644 --- a/src/commands/active.ts +++ b/src/commands/active.ts @@ -36,6 +36,15 @@ export default new app.Command({ validate: (value) => value > 0, validationErrorMessage: "The period must be greater than 0.", }), + app.option({ + name: "refreshInterval", + aliases: ["interval"], + description: "The interval to refresh the active list (in hours)", + type: "number", + validate: (value) => value > 0 && value < 24, + validationErrorMessage: + "The interval must be greater than 0 and less than 24.", + }), ], async run(message) { used = true @@ -64,6 +73,12 @@ export default new app.Command({ guildConfig: config, }) + await app.launchActiveInterval(message.guild, { + refreshInterval: +config.active_refresh_interval, + period: message.args.period ?? +config.active_period, + messageCount: message.args.messageCount ?? +config.active_message_count, + }) + used = false }, subs: [ diff --git a/src/commands/leaderboard.ts b/src/commands/leaderboard.ts index 428c28ce..0aa0bba8 100644 --- a/src/commands/leaderboard.ts +++ b/src/commands/leaderboard.ts @@ -1,7 +1,5 @@ import * as app from "#app" -import userTable from "#tables/user.ts" - export default new app.Command({ name: "leaderboard", aliases: ["lb", "ladder", "top", "rank"], diff --git a/src/listeners/activity.ready.ts b/src/listeners/activity.ready.ts index 66cb3f12..47258d74 100644 --- a/src/listeners/activity.ready.ts +++ b/src/listeners/activity.ready.ts @@ -1,7 +1,5 @@ import * as app from "#app" -const intervals: Record = {} - const listener: app.Listener<"ready"> = { event: "ready", description: "Start an interval to update the active list", @@ -13,63 +11,15 @@ const listener: app.Listener<"ready"> = { if (!config?.active_role_id) continue - if (intervals[guild.id] !== undefined) clearInterval(intervals[guild.id]) - - const interval = Number(config.active_refresh_interval) + const refreshInterval = Number(config.active_refresh_interval) const period = Number(config.active_period) const messageCount = Number(config.active_message_count) - intervals[guild.id] = setInterval( - async () => { - const realGuild = await guild.fetch() - - if (!(await app.hasActivity(config._id, interval))) return - - let found: number - - try { - found = await app.updateActive(realGuild, { - force: false, - period, - messageCount, - guildConfig: config, - }) - } catch (error: any) { - await app.sendLog( - realGuild, - `Failed to update the active list...${await app.code.stringify({ - content: error.message, - lang: "js", - })}`, - ) - - return - } - - const cacheId = `lastActiveCount.${guild.id}` - - const lastActiveCount = app.cache.ensure(cacheId, 0) - - if (found > lastActiveCount) { - await app.sendLog( - realGuild, - `Finished updating the active list, found **${ - found - lastActiveCount - }** active members.`, - ) - } else if (found < lastActiveCount) { - await app.sendLog( - realGuild, - `Finished updating the active list, **${ - lastActiveCount - found - }** members have been removed.`, - ) - } - - app.cache.set(cacheId, found) - }, - interval * 1000 * 60 * 60, - ) + await app.launchActiveInterval(guild, { + refreshInterval, + period, + messageCount, + }) } }, } diff --git a/src/listeners/helping.footer.messageCreate.ts b/src/listeners/helping.footer.messageCreate.ts index ced51dc6..d889713e 100644 --- a/src/listeners/helping.footer.messageCreate.ts +++ b/src/listeners/helping.footer.messageCreate.ts @@ -20,7 +20,7 @@ const listener: app.Listener<"messageCreate"> = { // Appeler la fonction refreshHelpingFooter ↓ 10 secondes après le dernier message (chaque message réinitialise le timer) // await app.refreshHelpingFooter(message.channel) - const cacheId = `helping.footer.timer.${message.channelId}` + const cacheId = app.helpingFooterCacheId(message.channel) const timer = app.cache.get(cacheId) diff --git a/src/namespaces/active.ts b/src/namespaces/active.ts index 83bacf90..a5711ba6 100644 --- a/src/namespaces/active.ts +++ b/src/namespaces/active.ts @@ -184,7 +184,7 @@ export async function updateActive( /** * @param guild_id - * @param period + * @param period in hours */ export async function hasActivity( guild_id: number, @@ -245,3 +245,80 @@ export const activeLadder = (guild_id: number) => )}\` msg - <@${line.target}>` }, }) + +export async function launchActiveInterval( + guild: app.OAuth2Guild | app.Guild, + options: { + period: number + messageCount: number + refreshInterval: number + }, +) { + const config = await app.getGuild(guild, { forceExists: true }) + + const intervalId = app.activeIntervalCacheId(guild) + const interval = app.cache.get(intervalId) + + if (interval !== undefined) clearInterval(interval) + + app.cache.set( + intervalId, + setInterval( + async () => { + const realGuild = await guild.fetch() + + if (!(await app.hasActivity(config._id, options.refreshInterval))) + return + + let found: number + + try { + found = await app.updateActive(realGuild, { + force: false, + period: options.period, + messageCount: options.messageCount, + guildConfig: config, + }) + } catch (error: any) { + await app.sendLog( + realGuild, + `Failed to update the active list...${await app.code.stringify({ + content: error.message, + lang: "js", + })}`, + ) + + return + } + + const cacheId = app.lastActiveCountCacheId(realGuild) + + const lastActiveCount = app.cache.ensure(cacheId, 0) + + if (found > lastActiveCount) { + await app.sendLog( + realGuild, + `Finished updating the active list, found **${ + found - lastActiveCount + }** active members.`, + ) + } else if (found < lastActiveCount) { + await app.sendLog( + realGuild, + `Finished updating the active list, **${ + lastActiveCount - found + }** members have been removed.`, + ) + } else { + await app.sendLog( + realGuild, + `Finished updating the active list, no changes were made.`, + ) + } + + app.cache.set(cacheId, found) + }, + options.refreshInterval * 1000 * 60 * 60, + ), + ) +} diff --git a/src/namespaces/caches.ts b/src/namespaces/caches.ts new file mode 100644 index 00000000..904e73e6 --- /dev/null +++ b/src/namespaces/caches.ts @@ -0,0 +1,8 @@ +export const activeIntervalCacheId = (guild: { id: string }) => + `${guild.id}/activeInterval` + +export const lastActiveCountCacheId = (guild: { id: string }) => + `${guild.id}/lastActiveCount` + +export const helpingFooterCacheId = (channel: { id: string }) => + `${channel.id}/helpingFooter`