Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/components/anti-afk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { strict as assert } from "assert";

import * as Discord from "discord.js";

import { SECOND } from "../common.js";
import { BotComponent } from "../bot-component.js";
import { clear_timeout, set_timeout } from "../utils/node.js";

export default class AntiAFK extends BotComponent {
private countdown = new Map<string, NodeJS.Timeout>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I try to use self clearing containers for this sort of thing. If the bot loses connection or otherwise misses events some assumptions (like getting a on_voice_state_update call when a user disconnects) can be broken and lead to issues. It seems like storing timeouts like this can work but it makes me a little bit uneasy. I'm wondering whether there might be better ways of doing this. One option might be to use SleepList, possibly modifying it to better fit the use-case here.

@michael-kenzel michael-kenzel Oct 2, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I considered using a self-clearing container. The thing is though that this would then require us to periodically check the container. Just setting a timeout seems like a simpler and more natural approach. And if anything goes wrong, the timeout will fire regardless, and remove the entry from the Set. So there should be no issue with accumulating entries. And there should generally be at most a handful of these in flight at any moment anyways. The worst-case failure mode should be that someone is spuriously moved to AFK when they shouldn't have been because the bot missed their undeafening, but I'm not sure there's much we can do about that. Regarding SleepList: I was kinda assuming that Node.js timeouts would already be implemented like that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I’m happy to accept my fud and go with this :)


private check_voice_state(old_state: Discord.VoiceState, new_state: Discord.VoiceState) {
if ((!new_state.selfDeaf || new_state.channel != old_state.channel) && this.countdown.has(new_state.id)) {
clear_timeout(this.countdown.get(new_state.id));
this.countdown.delete(new_state.id);
}
if (
new_state.channel != null &&
new_state.channelId != this.wheatley.guild.afkChannelId &&
new_state.selfDeaf
) {
assert(new_state.member);
const member = new_state.member;
const timeout = set_timeout(
() => {
member.voice
.fetch()
.then(async current_state => {
if (current_state.selfDeaf && current_state.channelId != this.wheatley.guild.afkChannelId) {
await member.voice.setChannel(this.wheatley.guild.afkChannel);
}
})
.catch(this.wheatley.critical_error.bind(this.wheatley));
this.countdown.delete(new_state.id);
},
(this.wheatley.guild.afkTimeout + 30) * SECOND,
);
this.countdown.set(new_state.id, timeout);
}
}

override async on_voice_state_update(old_state: Discord.VoiceState, new_state: Discord.VoiceState) {
this.check_voice_state(old_state, new_state);
}

override async on_ready() {
await Promise.all(
this.wheatley.guild.channels.cache
.filter(c => c.isVoiceBased())
.map(async channel => {
await Promise.all(
channel.members.map(async member => {
const voice_state = await member.voice.fetch();
this.check_voice_state(voice_state, voice_state);
}),
);
}),
);
}
}