Skip to content

Commit

Permalink
feat(metrics): add metrics for added temporary/permanent roles
Browse files Browse the repository at this point in the history
  • Loading branch information
tippfehlr committed Mar 11, 2024
1 parent b300183 commit 59160f5
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/modules/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ client.on(Events.PresenceUpdate, async (oldMember, newMember) => {
const startTime = Date.now();
stats.presenceUpdates++;
let logTime = false;

// no activities changed
// if (oldMember?.activities.toString() === newMember?.activities.toString()) return;

Expand Down Expand Up @@ -264,11 +265,15 @@ client.on(Events.PresenceUpdate, async (oldMember, newMember) => {
roleID
);
prepare('DELETE FROM activityRoles WHERE guildID = ? AND roleID = ?').run(guildID, roleID);
prepare('DELETE FROM activeTemporaryRoles WHERE guildID = ? AND roleID = ?').run(guildID, roleID);
return;
}
if (!highestBotRolePosition || highestBotRolePosition <= role.position) return;
if (newMember.member?.roles.cache.has(role.id)) return;
if (!permanent) {
if (permanent) {
writeIntPoint('roles_added', 'permanent_roles_added', 1);
} else {
writeIntPoint('roles_added', 'temporary_roles_added', 1);
prepare(
'INSERT OR IGNORE INTO activeTemporaryRoles (userIDHash, guildID, roleID) VALUES (?, ?, ?)'
).run(userIDHash, guildID, roleID);
Expand All @@ -290,7 +295,7 @@ client.on(Events.PresenceUpdate, async (oldMember, newMember) => {
activeTemporaryRoles.forEach(activeTemporaryRole => {
if (!tempRoleIDsToBeAdded.has(activeTemporaryRole.roleID)) {
const role = newMember.guild?.roles.cache.get(activeTemporaryRole.roleID);
// this does not check if the user has the role on purpose:
// this purposefully does not check if the user has the role:
// trying to remove it when the user doesn’t have it does nothing
// and the local role cache *could* be invalid resulting in roles getting stuck
if (role) newMember.member?.roles.remove(role);
Expand Down Expand Up @@ -321,7 +326,9 @@ client.on(Events.PresenceUpdate, async (oldMember, newMember) => {
});
});
if (logTime) console.timeEnd('apply changes');
if (logTime) console.time('writeIntPoint');
writeIntPoint('presence_updates', 'took_time', Date.now() - startTime)
if (logTime) console.timeEnd('writeIntPoint');
});

client.on(Events.GuildCreate, guild => {
Expand Down

0 comments on commit 59160f5

Please sign in to comment.