-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move “cron jobs” to their own files
- Loading branch information
Showing
5 changed files
with
134 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { checkGuilds } from './cron/checkRoles'; | ||
import { executeScheduledRoleActions } from './cron/scheduledRoleActions'; | ||
import { updateMemberCount } from './cron/updateMemberCount'; | ||
|
||
export function cron() { | ||
// 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); | ||
|
||
// scheduled role actions: | ||
// execute every 6 hours | ||
// check for roles to add or remove in scheduledRoleActions | ||
executeScheduledRoleActions(); | ||
setInterval(executeScheduledRoleActions, 6 * 60 * 60 * 1000); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { client } from '../bot'; | ||
import { checkRolesStandalone } from '../commands/checkRoles'; | ||
import { db } from '../db'; | ||
|
||
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); | ||
|
||
export async function checkGuilds() { | ||
const guildsToCheck = await db | ||
.selectFrom('guilds') | ||
.select('guildID') | ||
.where('lastCheckRoles', '<', new Date(Date.now() - 20 * 60 * 60 * 1000)) | ||
.orderBy('lastCheckRoles asc') | ||
.execute(); | ||
await client.guilds.fetch(); | ||
for (const { guildID } of guildsToCheck) { | ||
await delay(30 * 1000); | ||
const guild = client.guilds.cache.get(guildID); | ||
if (guild) await checkRolesStandalone({ guild }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { client } from '../bot'; | ||
import { db } from '../db'; | ||
import { log } from '../messages'; | ||
|
||
export async function executeScheduledRoleActions() { | ||
// ensure that only executed schedules get deleted. | ||
// otherwise, this could start at 23:59 and end after midnight, | ||
// deleting the schedules for the new day accidently. | ||
const date = new Date(); | ||
const actionsDue = await db | ||
.deleteFrom('scheduledRoleActions') | ||
.where('scheduledDate', '<=', date) | ||
.returning(['action', 'roleID', 'guildID', 'userID']) | ||
.execute(); | ||
let stats = { added: 0, removed: 0 }; | ||
|
||
for (const action of actionsDue) { | ||
if (action.action === 'add') { | ||
const guild = await client.guilds.fetch(action.guildID); | ||
if (!guild) continue; | ||
const user = await guild.members.fetch(action.userID); | ||
if (!user) continue; | ||
|
||
try { | ||
user.roles.add(action.roleID); | ||
stats.added += 1; | ||
} catch (error) { | ||
log.error( | ||
`scheduledRoleActions: should assign role ${action.roleID} to user ` + | ||
`${user.nickname} (${user.id}) on guild ${guild.name} (${guild.id}) ` + | ||
'but it (probably) doesn’t exist.', | ||
error, | ||
); | ||
} | ||
} else if (action.action === 'remove') { | ||
const guild = await client.guilds.fetch(action.guildID); | ||
if (1 || 2) { | ||
} | ||
if (!guild) continue; | ||
const user = await guild.members.fetch(action.userID); | ||
if (!user) continue; | ||
|
||
try { | ||
user.roles.remove(action.roleID); | ||
stats.removed += 1; | ||
} catch (error) { | ||
log.error( | ||
`scheduledRoleActions: should remove role ${action.roleID} to user ` + | ||
`${user.nickname} (${user.id}) on guild ${guild.name} (${guild.id}) ` + | ||
'but it (probably) doesn’t exist.', | ||
error, | ||
); | ||
} | ||
} | ||
} | ||
log.info(`scheduledRoleActions: added ${stats.added} and removed ${stats.removed} roles.`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { client } from '../bot'; | ||
import { db } from '../db'; | ||
|
||
export async function updateMemberCount() { | ||
const guildsToUpdate = await db | ||
.selectFrom('guilds') | ||
.select(['guildID']) | ||
.where(eb => | ||
eb.or([ | ||
eb('approxMemberCountLastUpdate', '<', new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)), | ||
eb('approxMemberCountLastUpdate', 'is', null), | ||
eb('approxMemberCount', 'is', null), | ||
]), | ||
) | ||
.execute(); | ||
await client.guilds.fetch(); | ||
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(); | ||
} | ||
} |