Skip to content

Commit

Permalink
feat: youtube authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
Apehum committed Jul 19, 2024
1 parent e32cd76 commit 6bb7280
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
5 changes: 4 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
**Requires [pv-addon-lavaplayer-lib 1.0.11+](https://modrinth.com/plugin/pv-addon-lavaplayer-lib/version/1.0.11) to work**

Changelog:
- Fixed an issue where inserting a disc with shift-click caused it to start playing on 1.21
- Fixed an issue where inserting a disc with shift-click caused it to start playing on 1.21
- Reintroduced YouTube authorization
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
paperVersion=1.20.1-R0.1-SNAPSHOT
foliaVersion=1.20.1-R0.1-SNAPSHOT
plasmoVoiceVersion=2.0.3
lavaplayerLibVersion=1.0.8
lavaplayerLibVersion=1.0.11

# Version
mavenGroup=su.plo.voice.discs
Expand Down
21 changes: 21 additions & 0 deletions src/main/kotlin/su/plo/voice/discs/AddonConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ class AddonConfig {
@ConfigField
val httpSource = HttpSourceConfig()

@Config
class YouTubeSourceConfig {
@ConfigField(
comment = """
If you see a error like "Sign in to confirm you're not a bot",
you can try using YouTube oauth2 authorization.
On the first start with authorization enabled,
you will see "OAUTH INTEGRATION" in your console.
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.
"""
)
val useOauth2: Boolean = false
}

@ConfigField
val youtubeSource = YouTubeSourceConfig()

@Config
class BurnableTag {
@ConfigField(
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/su/plo/voice/discs/DiscsPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class DiscsPlugin : JavaPlugin() {
loadConfig()
}

@EventSubscribe
override fun onEnable() {
loadConfig()

Expand Down Expand Up @@ -100,6 +99,10 @@ class DiscsPlugin : JavaPlugin() {

override fun onDisable() {
PlasmoVoiceServer.getAddonsLoader().unload(this)

if (::audioPlayerManager.isInitialized) {
audioPlayerManager.save()
}
}

private fun loadConfig() {
Expand Down
25 changes: 24 additions & 1 deletion src/main/kotlin/su/plo/voice/discs/PlasmoAudioPlayerManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import su.plo.voice.lavaplayer.libs.com.sedmelluq.discord.lavaplayer.track.*
import su.plo.voice.lavaplayer.libs.dev.lavalink.youtube.YoutubeAudioSourceManager
import su.plo.voice.proto.packets.tcp.clientbound.SourceAudioEndPacket
import su.plo.voice.proto.packets.udp.clientbound.SourceAudioPacket
import java.io.File
import java.net.URI
import java.util.concurrent.CompletableFuture
import java.util.concurrent.TimeUnit
Expand All @@ -34,6 +35,17 @@ class PlasmoAudioPlayerManager(
registerSources()
}

fun save() {
lavaPlayerManager.sourceManagers
.filterIsInstance<YoutubeAudioSourceManager>()
.firstOrNull()
?.takeIf { it.oauth2RefreshToken != null }
?.let {
val refreshTokenFile = File(plugin.dataFolder, ".youtube-token")
refreshTokenFile.writeText(it.oauth2RefreshToken!!)
}
}

fun startTrackJob(track: AudioTrack, source: ServerStaticSource, distance: Short) = scope.launch {

val player = lavaPlayerManager.createPlayer()
Expand Down Expand Up @@ -136,7 +148,18 @@ class PlasmoAudioPlayerManager(
}

private fun registerSources() {
lavaPlayerManager.registerSourceManager(YoutubeAudioSourceManager(true))
lavaPlayerManager.registerSourceManager(
YoutubeAudioSourceManager(true)
.also { source ->
if (plugin.addonConfig.youtubeSource.useOauth2) {
val refreshToken = File(plugin.dataFolder, ".youtube-token")
.takeIf { it.isFile && it.exists() }
?.readText()
?.trim()
source.useOauth2(refreshToken, false)
}
}
)
lavaPlayerManager.registerSourceManager(SoundCloudAudioSourceManager.createDefault())
lavaPlayerManager.registerSourceManager(BandcampAudioSourceManager())
lavaPlayerManager.registerSourceManager(VimeoAudioSourceManager())
Expand Down

0 comments on commit 6bb7280

Please sign in to comment.