-
Notifications
You must be signed in to change notification settings - Fork 22
fix(Discord): Unfuck Discord rpc #279
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
base: 1.21.11
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,13 +38,13 @@ import dev.cbyrne.kdiscordipc.core.packet.inbound.impl.AuthenticatePacket | |
| import dev.cbyrne.kdiscordipc.data.activity.largeImage | ||
| import dev.cbyrne.kdiscordipc.data.activity.smallImage | ||
| import dev.cbyrne.kdiscordipc.data.activity.timestamps | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.delay | ||
|
|
||
| object Discord : Module( | ||
| name = "Discord", | ||
| description = "Discord Rich Presence configuration", | ||
| tag = ModuleTag.CLIENT, | ||
| enabledByDefault = true, | ||
| tag = ModuleTag.CLIENT | ||
| ) { | ||
| private val delay by setting("Update Delay", 5000L, 5000L..30000L, 100L, unit = "ms") | ||
| private val showTime by setting("Show Time", true, description = "Show how long you have been playing for.") | ||
|
|
@@ -53,7 +53,7 @@ object Discord : Module( | |
| private val line2Left by setting("Line 2 Left", LineInfo.Version) | ||
| private val line2Right by setting("Line 2 Right", LineInfo.Fps) | ||
|
|
||
| val rpc = KDiscordIPC(Lambda.APP_ID, scope = EventFlow.lambdaScope) | ||
| var rpc: KDiscordIPC? = null | ||
|
|
||
| private var startup = System.currentTimeMillis() | ||
|
|
||
|
|
@@ -67,36 +67,58 @@ object Discord : Module( | |
| return@listenOnce true | ||
| } | ||
|
|
||
| onEnable { runConcurrent { start(); handleLoop() } } | ||
| runConcurrent { start() } | ||
| onEnable { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the only place start() is called which will not run if the user has the module enabled on client startup as onEnable requires a SafeContext |
||
| runConcurrent { | ||
| if (start()) handleLoop() | ||
| } | ||
| } | ||
| onDisable { stop() } | ||
| } | ||
|
|
||
| private suspend fun start() { | ||
| if (rpc.connected) return | ||
|
|
||
| rpc.connect() | ||
| private suspend fun start(): Boolean { | ||
| if (rpc == null) { | ||
| rpc = KDiscordIPC(Lambda.APP_ID, scope = EventFlow.lambdaScope) | ||
| } | ||
| if (rpc!!.connected) return true | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we, if not never, almost never use !! as it voids kotlins null safety checks and opens space up for null pointer exceptions |
||
|
|
||
| try { | ||
| rpc!!.connect() | ||
| } catch (t: Throwable) { | ||
| if (t is CancellationException) throw t | ||
| LOG.warn(t) | ||
| warn("Failed to connect to Discord. Make sure Discord is open and can access temporary files.") | ||
| return false | ||
| } | ||
|
|
||
| val auth = rpc.applicationManager.authenticate() | ||
| val auth = try { | ||
| rpc!!.applicationManager.authenticate() | ||
| } catch (t: Throwable) { | ||
| if (t is CancellationException) throw t | ||
| LOG.warn(t) | ||
| warn("Connected to Discord but authentication failed.") | ||
| return false | ||
| } | ||
|
|
||
| linkDiscord(discordToken = auth.accessToken) | ||
| .onSuccess { updateToken(it); discordAuth = auth } | ||
| .onFailure { LOG.error(it); warn("Failed to link your discord account") } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| private fun stop() { | ||
| if (rpc.connected) rpc.disconnect() | ||
| if (rpc?.connected == true) rpc?.disconnect() | ||
| } | ||
|
|
||
| private suspend fun SafeContext.handleLoop() { | ||
| while (rpc.connected) { | ||
| while (rpc?.connected == true) { | ||
| update() | ||
| delay(delay) | ||
| } | ||
| } | ||
|
|
||
| private suspend fun SafeContext.update() { | ||
| rpc.activityManager.setActivity { | ||
| rpc?.activityManager?.setActivity { | ||
| details = "${line1Left.value(this@update)} | ${line1Right.value(this@update)}".take(128) | ||
| state = "${line2Left.value(this@update)} | ${line2Right.value(this@update)}".take(128) | ||
|
|
||
|
|
||
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.
could this be replaced with a lazy {}?