Skip to content

Commit

Permalink
updated active member system and command
Browse files Browse the repository at this point in the history
  • Loading branch information
GhomKrosmonaute committed Oct 28, 2024
1 parent a2dea19 commit e5b2c6b
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 60 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
15 changes: 15 additions & 0 deletions src/commands/active.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: [
Expand Down
2 changes: 0 additions & 2 deletions src/commands/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down
62 changes: 6 additions & 56 deletions src/listeners/activity.ready.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as app from "#app"

const intervals: Record<string, NodeJS.Timeout> = {}

const listener: app.Listener<"ready"> = {
event: "ready",
description: "Start an interval to update the active list",
Expand All @@ -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,
})
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion src/listeners/helping.footer.messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NodeJS.Timeout>(cacheId)

Expand Down
79 changes: 78 additions & 1 deletion src/namespaces/active.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export async function updateActive(

/**
* @param guild_id
* @param period
* @param period in hours
*/
export async function hasActivity(
guild_id: number,
Expand Down Expand Up @@ -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<NodeJS.Timeout>(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,
),
)
}
8 changes: 8 additions & 0 deletions src/namespaces/caches.ts
Original file line number Diff line number Diff line change
@@ -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`

0 comments on commit e5b2c6b

Please sign in to comment.