From b4caf958e70b038dbb4eb1200132ae841549d435 Mon Sep 17 00:00:00 2001 From: Apehum <36326454+Apehum@users.noreply.github.com> Date: Fri, 6 Dec 2024 19:27:41 +0800 Subject: [PATCH] feat: save youtube refresh token using fixed timer if refresh token is null and oauth2 is enabled --- .../kotlin/su/plo/voice/discs/AddonConfig.kt | 2 +- .../voice/discs/PlasmoAudioPlayerManager.kt | 43 ++++++++++++++----- .../kotlin/su/plo/voice/discs/DiscsPlugin.kt | 6 ++- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/core/src/main/kotlin/su/plo/voice/discs/AddonConfig.kt b/core/src/main/kotlin/su/plo/voice/discs/AddonConfig.kt index bf5fc74..f7e7908 100644 --- a/core/src/main/kotlin/su/plo/voice/discs/AddonConfig.kt +++ b/core/src/main/kotlin/su/plo/voice/discs/AddonConfig.kt @@ -162,7 +162,7 @@ class AddonConfig { Follow the instructions in this prompt. If you do everything right, you will see "Token retrieved successfully" in your console. You only need to do this once; - the token will be stored in "pv-addon-discs/.youtube-token" on plugin shutdown. + the token will be stored in "pv-addon-discs/.youtube-token" after successful authorization. """ ) val useOauth2: Boolean = false diff --git a/core/src/main/kotlin/su/plo/voice/discs/PlasmoAudioPlayerManager.kt b/core/src/main/kotlin/su/plo/voice/discs/PlasmoAudioPlayerManager.kt index 15d6d5d..1f8aada 100644 --- a/core/src/main/kotlin/su/plo/voice/discs/PlasmoAudioPlayerManager.kt +++ b/core/src/main/kotlin/su/plo/voice/discs/PlasmoAudioPlayerManager.kt @@ -8,8 +8,8 @@ import su.plo.voice.api.server.PlasmoVoiceServer import su.plo.voice.api.server.audio.provider.AudioFrameProvider import su.plo.voice.api.server.audio.provider.AudioFrameResult import su.plo.voice.api.server.audio.source.ServerProximitySource -import su.plo.voice.discs.utils.PluginKoinComponent import su.plo.voice.discs.config.YoutubeClient +import su.plo.voice.discs.utils.PluginKoinComponent import su.plo.voice.discs.utils.extend.getValue import su.plo.voice.discs.utils.extend.getter import su.plo.voice.lavaplayer.libs.com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler @@ -39,6 +39,7 @@ import java.io.File import java.net.URI import java.util.concurrent.CompletableFuture import java.util.function.Consumer +import kotlin.concurrent.fixedRateTimer class PlasmoAudioPlayerManager : PluginKoinComponent { @@ -54,15 +55,8 @@ class PlasmoAudioPlayerManager : PluginKoinComponent { registerSources() } - fun save() { - lavaPlayerManager.sourceManagers - .filterIsInstance() - .firstOrNull() - ?.takeIf { it.oauth2RefreshToken != null } - ?.let { - val refreshTokenFile = File(plugin.dataFolder, ".youtube-token") - refreshTokenFile.writeText(it.oauth2RefreshToken!!) - } + fun shutdown() { + lavaPlayerManager.shutdown() } fun startTrackJob(track: AudioTrack, source: ServerProximitySource<*>, distance: Short): Job { @@ -186,6 +180,34 @@ class PlasmoAudioPlayerManager : PluginKoinComponent { return host to credentials } + private fun saveToken() { + lavaPlayerManager.sourceManagers + .filterIsInstance() + .firstOrNull() + ?.takeIf { it.oauth2RefreshToken != null } + ?.let { + val refreshTokenFile = File(plugin.dataFolder, ".youtube-token") + refreshTokenFile.writeText(it.oauth2RefreshToken!!) + plugin.slF4JLogger.info("YouTube oauth2 refresh token saved to .youtube-token") + } + } + + private fun listenForTokenChange(youtubeSourceManager: YoutubeAudioSourceManager) { + val currentToken = youtubeSourceManager.oauth2RefreshToken + fixedRateTimer( + "[pv-addon-discs] [youtube-refresh-token-listener]", + true, + 5000L, + 5000L, + ) { + val newToken = youtubeSourceManager.oauth2RefreshToken ?: return@fixedRateTimer + if (currentToken == newToken) return@fixedRateTimer + + saveToken() + cancel() + } + } + private fun proxyHttpBuilder(): Consumer? { val (host, credentials) = httpProxy() ?: return null @@ -234,6 +256,7 @@ class PlasmoAudioPlayerManager : PluginKoinComponent { ?.readText() ?.trim() source.useOauth2(refreshToken, false) + if (refreshToken == null) listenForTokenChange(source) } } ) diff --git a/plugin/src/main/kotlin/su/plo/voice/discs/DiscsPlugin.kt b/plugin/src/main/kotlin/su/plo/voice/discs/DiscsPlugin.kt index 13bd378..e6f5671 100644 --- a/plugin/src/main/kotlin/su/plo/voice/discs/DiscsPlugin.kt +++ b/plugin/src/main/kotlin/su/plo/voice/discs/DiscsPlugin.kt @@ -151,7 +151,7 @@ class DiscsPlugin : JavaPlugin() { PlasmoVoiceServer.getAddonsLoader().unload(this) if (::audioPlayerManager.isInitialized) { - audioPlayerManager.save() + audioPlayerManager.shutdown() } } @@ -170,6 +170,10 @@ class DiscsPlugin : JavaPlugin() { setDefaultVolume(addonConfig.defaultSourceLineVolume) }.build() + if (::audioPlayerManager.isInitialized) { + audioPlayerManager.shutdown() + } + audioPlayerManager = PlasmoAudioPlayerManager() } }