-
Notifications
You must be signed in to change notification settings - Fork 25
Add anti AFK component #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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>(); | ||
|
|
||
| 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); | ||
| }), | ||
| ); | ||
| }), | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 :)