freyacodes / ukulele

Simple self-contained Discord music bot
MIT License
157 stars 75 forks source link

Automatically leave voice channel after some time spent idle #42

Open SriRamanujam opened 2 years ago

SriRamanujam commented 2 years ago

This PR enables the bot to leave a voice channel after some time spent idle. Upon joining a channel, a scheduled task will fire every minute that checks to see if the player is idle. If it is, the task will increment an internal timer. After some configurable amount of time (idleTimeMinutes in ukulele.yml, defaults to 10 minutes) the task will have the bot automatically leave the voice channel.

First time working with Kotlin and Spring, so please do point out any stylistic foibles. If there's a linter I should run, I can do that no problem.

Fixes #26

SriRamanujam commented 2 years ago

Okay, so in the process of reworking the PR with code review feedback I discovered a race condition of sorts. Basically, oftentimes the onVoiceJoin handler of the service would fire before the onTrackStart handler in Player, so the logic would determine see an empty queue and no playing track, which meant remainingDuration was 0, and so it would start the idle timeout task. If the video was under one minute long, that meant that the bot would leave early. Not the end of the world, but incorrect behavior nonetheless.

I tried a couple things to resolve the issue. I first tried implementing onTrackStart and onTrackEnd on LeaveOnIdleService itself, which didn't work for the same reason (couldn't guarantee the new handlers would be called after Player's handlers). The approach that does seem to work is what I've reworked this PR into: extending the event handlers on Player to call into LeaveOnIdleService. This neatly resolves the ordering issues, and so far has worked for me in my testing.

SriRamanujam commented 2 years ago

Hi, anything else I can do to move this along? I believed I've addressed all the comments from the last round of review.

Abi45x commented 2 years ago

Hi SriRamanujam, Freya is currently focusing her time on her bachelor project until the 4th of January, I would probably expect replies to be sparse until then