Skip to content

Commit

Permalink
decrease frequency of automatical update of active members
Browse files Browse the repository at this point in the history
  • Loading branch information
GhomKrosmonaute committed Oct 12, 2023
1 parent ec0971e commit 56466d8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
23 changes: 20 additions & 3 deletions src/commands/active.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import * as app from "../app.js"

import active from "../tables/active.js"

let used = false
let interval: NodeJS.Timeout | undefined = undefined

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

export default new app.Command({
name: "active",
Expand Down Expand Up @@ -44,6 +47,8 @@ export default new app.Command({
async run(message) {
used = true

const config = await app.getGuild(message.guild, true)

const waiting = await message.send(
`${app.emote(message, "WAIT")} Fetching members...`
)
Expand All @@ -53,6 +58,7 @@ export default new app.Command({
period: message.args.period,
messageCount: message.args.messageCount,
onLog: (text) => waiting.edit(text),
guildConfig: config,
})

used = false
Expand All @@ -65,13 +71,24 @@ export default new app.Command({
)} Automated active list hourly update enabled.`
)

if (interval !== undefined) clearInterval(interval)
if (intervals[message.guild.id] !== undefined)
clearInterval(intervals[message.guild.id])

intervals[message.guild.id] = setInterval(async () => {
const activityLastHour = await active.query
.where("guild_id", config._id)
.where("created_timestamp", ">", Date.now() - 1000 * 60 * 60)
.select(app.db.raw("count(*) as messageCount"))
.limit(1)
.then((rows) => rows[0] as unknown as { messageCount: number })

if (activityLastHour.messageCount === 0) return

interval = setInterval(async () => {
const found = await app.updateActive(message.guild, {
force: false,
period: message.args.period,
messageCount: message.args.messageCount,
guildConfig: config,
})

await app.sendLog(
Expand Down
37 changes: 21 additions & 16 deletions src/namespaces/active.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import * as app from "../app.js"

import { Guild } from "../tables/guild.js"
import messages from "../tables/message.js"
import active from "../tables/active.js"

export async function isActive(
member: app.GuildMember,
period = 1000 * 60 * 60 * 24 * 7,
requiredMessageCount = 50
requiredMessageCount = 50,
guild?: Guild
): Promise<boolean> {
const user = await app.getUser(member, true)
const guild = await app.getGuild(member.guild, true)
guild ??= await app.getGuild(member.guild, true)

const data = await messages.query
.where("author_id", user._id)
Expand All @@ -29,10 +31,9 @@ export async function updateActive(
period: number
messageCount: number
onLog?: (text: string) => unknown | Promise<unknown>
guildConfig: Guild
}
): Promise<number> {
const config = await app.getGuild(guild, true)

guild.members.cache.clear()

const members = (await guild.members.fetch())
Expand All @@ -48,15 +49,16 @@ export async function updateActive(
const isActive = await app.isActive(
member,
options.period,
options.messageCount
options.messageCount,
options.guildConfig
)

if (isActive) activeMembers.push(member)
else inactiveMembers.push(member)
}

if (options.force) {
await active.query.delete().where("guild_id", config._id)
await active.query.delete().where("guild_id", options.guildConfig._id)

if (activeMembers.length === 0)
await active.query.insert(
Expand All @@ -66,7 +68,7 @@ export async function updateActive(

return {
user_id: user._id,
guild_id: config._id,
guild_id: options.guildConfig._id,
}
})
)
Expand All @@ -82,8 +84,8 @@ export async function updateActive(
for (const member of activeMembers) {
await member.fetch(true)

if (!member.roles.cache.has(config.active_role_id!))
await member.roles.add(config.active_role_id!)
if (!member.roles.cache.has(options.guildConfig.active_role_id!))
await member.roles.add(options.guildConfig.active_role_id!)

if (options.onLog)
await options.onLog(
Expand All @@ -99,8 +101,8 @@ export async function updateActive(
for (const member of inactiveMembers) {
await member.fetch(true)

if (member.roles.cache.has(config.active_role_id!))
await member.roles.remove(config.active_role_id!)
if (member.roles.cache.has(options.guildConfig.active_role_id!))
await member.roles.remove(options.guildConfig.active_role_id!)

if (options.onLog)
await options.onLog(
Expand All @@ -112,7 +114,10 @@ export async function updateActive(
} else {
// use the cache to update only the changed members

const activeMembersCache = await active.query.where("guild_id", config._id)
const activeMembersCache = await active.query.where(
"guild_id",
options.guildConfig._id
)

if (options.onLog)
await options.onLog(
Expand All @@ -125,10 +130,10 @@ export async function updateActive(
const user = await app.getUser(member, true)

if (!activeMembersCache.find((am) => am.user_id === user._id)) {
await member.roles.add(config.active_role_id!)
await member.roles.add(options.guildConfig.active_role_id!)
await active.query.insert({
user_id: user._id,
guild_id: config._id,
guild_id: options.guildConfig._id,
})
}
}
Expand All @@ -144,10 +149,10 @@ export async function updateActive(
const user = await app.getUser(member, true)

if (activeMembersCache.find((am) => am.user_id === user._id)) {
await member.roles.remove(config.active_role_id!)
await member.roles.remove(options.guildConfig.active_role_id!)
await active.query.delete().where({
user_id: user._id,
guild_id: config._id,
guild_id: options.guildConfig._id,
})
}
}
Expand Down

0 comments on commit 56466d8

Please sign in to comment.