Skip to content

Commit

Permalink
perf: remove debug timings in presenceUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
tippfehlr committed Mar 12, 2024
1 parent de67928 commit c1523f7
Showing 1 changed file with 0 additions and 20 deletions.
20 changes: 0 additions & 20 deletions src/modules/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,10 @@ client.on(Events.ClientReady, () => {
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;

// write timings only for tippfehlr (me) in ASTRONEER, because it has many members
if (newMember.user?.username === 'tippfehlr' && newMember.guild?.name === 'ASTRONEER' && log.isLevelEnabled('debug')) {
log.debug(`PRESENCE UPDATE: User ${newMember.user?.username}, ${newMember.activities.toString()}, ${oldMember?.activities.toString() === newMember?.activities.toString()}`)
logTime = true;
}

if (logTime) console.time('pre member-fetch');
if (!newMember.guild) return;
const guildID = newMember.guild.id;
if (!newMember.guild.members.me?.permissions.has(PermissionsBitField.Flags.ManageRoles)) {
Expand All @@ -178,11 +170,9 @@ client.on(Events.PresenceUpdate, async (oldMember, newMember) => {
const highestBotRolePosition = newMember.guild.members.me?.roles.highest.position;
const userIDHash = createHash('sha256').update(newMember.user.id).digest('base64');
const guildConfig = getGuildConfig(guildID);
if (logTime) console.timeEnd('pre member-fetch');
// if (debug) console.time('fetch member ' + date);
// await newMember.member?.fetch(true);
// if (debug) console.timeEnd('fetch member ' + date);
if (logTime) console.time('roles');

if (
guildConfig.requiredRoleID !== null &&
Expand All @@ -205,7 +195,6 @@ client.on(Events.PresenceUpdate, async (oldMember, newMember) => {
prepare('SELECT * FROM activeTemporaryRoles WHERE userIDHash = ? AND guildID = ?')
.all(userIDHash, guildID) as DBActiveTemporaryRoles[];

if (logTime) console.timeEnd('roles');

// return if guild doesn’t have any roles
if (statusRoles.length === 0 && activityRoles.length === 0 && activeTemporaryRoles.length === 0) {
Expand All @@ -217,7 +206,6 @@ client.on(Events.PresenceUpdate, async (oldMember, newMember) => {

// if user is offline, skip checking for added activities
if (newMember.status !== 'offline') {
if (logTime) console.time('detect changes');

const addRole = ({ roleID, permanent }: { roleID: string, permanent: boolean }) => {
if (permanent) {
Expand Down Expand Up @@ -253,9 +241,6 @@ client.on(Events.PresenceUpdate, async (oldMember, newMember) => {
}
});

if (logTime) console.timeEnd('detect changes');
if (logTime) console.time('apply changes');

// ------------ “apply changes” ------------
const addDiscordRoleToMember = ({ roleID, permanent }: { roleID: string, permanent: boolean }) => {
const role = newMember.guild?.roles.cache.get(roleID);
Expand Down Expand Up @@ -286,8 +271,6 @@ client.on(Events.PresenceUpdate, async (oldMember, newMember) => {
tempRoleIDsToBeAdded.forEach(roleID => {
addDiscordRoleToMember({ roleID, permanent: false });
});
} else {
if (logTime) console.time('apply changes');
}

// remove temporary roles --- new activeTemporaryRoles
Expand Down Expand Up @@ -324,10 +307,7 @@ client.on(Events.PresenceUpdate, async (oldMember, newMember) => {
stats.rolesRemoved++;
});
});
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 c1523f7

Please sign in to comment.