Skip to content

Commit

Permalink
feat: add column approxMemberCount to guilds
Browse files Browse the repository at this point in the history
add column approxMemberCount to keep track of (big) guilds using the bot
  • Loading branch information
tippfehlr committed Jun 27, 2024
1 parent 16b7ad6 commit 21bd8b3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/modules/bot.ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,17 @@ export function initClientReady() {
`The bot is currently on ${client.guilds.cache.size} guilds with ${await getUserCount()} users and manages ${await getRolesCount()} roles`,
);

// checkroles:
// execute every 30 minutes
// check if the last check was more than 20 hours ago
checkGuilds();
setInterval(checkGuilds, 30 * 60 * 1000);

// membercount:
// execute every 24 hours
// check if the last update was more than 7 days ago
updateMemberCount();
setInterval(updateMemberCount, 24 * 60 * 60 * 1000);
});
}

Expand All @@ -92,3 +101,31 @@ async function checkGuilds() {
if (guild) await checkRoles({ guild });
}
}

async function updateMemberCount() {
const guildsToUpdate = await db
.selectFrom('guilds')
.select(['guildID', 'approxMemberCount', 'approxMemberCountLastUpdate'])
.where(eb =>
eb.or([
eb('approxMemberCountLastUpdate', '<', new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)),
eb('approxMemberCountLastUpdate', '=', null),
eb('approxMemberCount', '=', null),
]),
)
.execute();

for (const dbGuild of guildsToUpdate) {
const guild = client.guilds.cache.get(dbGuild.guildID);
if (!guild) continue;
await guild.members.fetch();
await db
.updateTable('guilds')
.set({
approxMemberCount: guild.memberCount,
approxMemberCountLastUpdate: new Date(),
})
.where('guildID', '=', dbGuild.guildID)
.execute();
}
}
18 changes: 16 additions & 2 deletions src/modules/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { locales, log } from './messages';
import { DB, ActivityRoles, Guilds, StatusRoles, Users } from './db.types';
import config from './config';
import { writeIntPoint } from './metrics';
import { client } from './bot';

const pool = new Pool({ connectionString: config.DATABASE_URL, max: 10 });
const dialect = new PostgresDialect({ pool });
Expand Down Expand Up @@ -96,11 +97,24 @@ export async function getGuildConfig(guildID: string): Promise<Selectable<Guilds
.where('guildID', '=', guildID)
.executeTakeFirst();
if (guild) return guild;
await client.guilds.fetch(guildID);
let dcGuild = client.guilds.cache.get(guildID);
let approxMemberCountLastUpdate = dcGuild ? new Date() : null;
db.insertInto('guilds')
.values({ guildID })
.values({
guildID,
approxMemberCount: dcGuild?.memberCount,
approxMemberCountLastUpdate,
})
.onConflict(oc => oc.column('guildID').doNothing())
.execute();
return { guildID: guildID, requiredRoleID: null, lastCheckRoles: null };
return {
guildID,
requiredRoleID: null,
lastCheckRoles: null,
approxMemberCount: dcGuild ? dcGuild.memberCount : null,
approxMemberCountLastUpdate,
};
}

export async function getActivityRoles(guildID: string): Promise<Selectable<ActivityRoles>[]> {
Expand Down
2 changes: 2 additions & 0 deletions src/modules/db.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface ActivityStats {
}

export interface Guilds {
approxMemberCount: number | null;
approxMemberCountLastUpdate: Timestamp | null;
guildID: string;
lastCheckRoles: Timestamp | null;
requiredRoleID: string | null;
Expand Down
17 changes: 17 additions & 0 deletions src/modules/migrations/3_add_approxMemberCount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Kysely } from 'kysely';

export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('guilds')
.addColumn('approxMemberCount', 'integer')
.addColumn('approxMemberCountLastUpdate', 'timestamp')
.execute();
}

export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('guilds')
.dropColumn('approxMemberCount')
.dropColumn('approxMemberCountLastUpdate')
.execute();
}

0 comments on commit 21bd8b3

Please sign in to comment.