Skip to content

Commit b30258f

Browse files
committed
Revert "push discord fix"
This reverts commit 2aaccb6.
1 parent 2aaccb6 commit b30258f

File tree

2 files changed

+49
-35
lines changed

2 files changed

+49
-35
lines changed

build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ configurations.all {
7070
repositories {
7171
mavenLocal() // Allow the use of local repositories
7272
maven("https://maven.2b2t.vc/releases") // Baritone
73-
maven("https://maven.firstdark.dev/releases")
73+
maven("https://jitpack.io") // KDiscordIPC
7474
maven("https://api.modrinth.com/maven")
7575
mavenCentral()
7676

@@ -155,7 +155,7 @@ dependencies {
155155

156156
// Add dependencies on the required Kotlin modules.
157157
includeLib("io.github.classgraph:classgraph:${classGraphVersion}")
158-
includeLib("dev.firstdark.discordrpc:discord-rpc:1.0.4")
158+
includeLib("com.github.emyfops:KDiscordIPC:$discordIPCVersion")
159159
includeLib("com.pngencoder:pngencoder:$pngEncoderVersion")
160160

161161
includeLib("io.github.spair:imgui-java-binding:$spairVersion")

src/main/kotlin/com/lambda/module/modules/client/Discord.kt

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,27 @@
1818
package com.lambda.module.modules.client
1919

2020
import com.lambda.Lambda
21+
import com.lambda.Lambda.LOG
2122
import com.lambda.context.SafeContext
22-
import com.lambda.event.events.ClientEvent
23+
import com.lambda.event.EventFlow
2324
import com.lambda.event.events.WorldEvent
2425
import com.lambda.event.listener.SafeListener.Companion.listenOnce
25-
import com.lambda.event.listener.UnsafeListener.Companion.listenOnceUnsafe
2626
import com.lambda.module.Module
2727
import com.lambda.module.tag.ModuleTag
28+
import com.lambda.network.NetworkManager.updateToken
29+
import com.lambda.network.api.v1.endpoints.linkDiscord
2830
import com.lambda.threading.runConcurrent
31+
import com.lambda.util.Communication.warn
2932
import com.lambda.util.Nameable
3033
import com.lambda.util.extension.dimensionName
3134
import com.lambda.util.extension.fullHealth
3235
import com.lambda.util.extension.worldName
33-
import dev.firstdark.rpc.DiscordRpc
34-
import dev.firstdark.rpc.handlers.RPCEventHandler
35-
import dev.firstdark.rpc.models.DiscordRichPresence
36+
import dev.cbyrne.kdiscordipc.KDiscordIPC
37+
import dev.cbyrne.kdiscordipc.core.packet.inbound.impl.AuthenticatePacket
38+
import dev.cbyrne.kdiscordipc.data.activity.button
39+
import dev.cbyrne.kdiscordipc.data.activity.largeImage
40+
import dev.cbyrne.kdiscordipc.data.activity.smallImage
41+
import dev.cbyrne.kdiscordipc.data.activity.timestamps
3642
import kotlinx.coroutines.delay
3743

3844
object Discord : Module(
@@ -48,55 +54,63 @@ object Discord : Module(
4854
private val line2Left by setting("Line 2 Left", LineInfo.Version)
4955
private val line2Right by setting("Line 2 Right", LineInfo.Fps)
5056

51-
val rpc = DiscordRpc()
57+
val rpc = KDiscordIPC(Lambda.APP_ID, scope = EventFlow.lambdaScope)
58+
5259
private var startup = System.currentTimeMillis()
5360

54-
// var discordAuth: AuthenticatePacket.Data? = null; private set
61+
var discordAuth: AuthenticatePacket.Data? = null; private set
5562

5663
init {
5764
listenOnce<WorldEvent.Join> {
65+
if (rpc.connected) return@listenOnce false
66+
5867
runConcurrent {
5968
start()
60-
update()
69+
handleLoop()
6170
}
6271

6372
return@listenOnce true
6473
}
6574

66-
onEnable { runConcurrent { start(); update() } }
75+
onEnable { runConcurrent { start(); handleLoop() } }
6776
onDisable { stop() }
6877
}
6978

70-
private fun start() {
71-
rpc.init(Lambda.APP_ID, RPCEventHandler(), true)
72-
// val auth = rpc.applicationManager.authenticate()
73-
//
74-
// linkDiscord(discordToken = auth.accessToken)
75-
// .onSuccess { updateToken(it); discordAuth = auth }
76-
// .onFailure { LOG.error(it); warn("Failed to link your discord account") }
79+
private suspend fun start() {
80+
if (rpc.connected) return
81+
82+
runConcurrent { rpc.connect() }
83+
delay(1000)
84+
85+
val auth = rpc.applicationManager.authenticate()
86+
87+
linkDiscord(discordToken = auth.accessToken)
88+
.onSuccess { updateToken(it); discordAuth = auth }
89+
.onFailure { LOG.error(it); warn("Failed to link your discord account") }
7790
}
7891

7992
private fun stop() {
80-
rpc.shutdown()
93+
if (rpc.connected) rpc.disconnect()
94+
}
95+
96+
private suspend fun SafeContext.handleLoop() {
97+
while (rpc.connected) {
98+
update()
99+
delay(delay)
100+
}
81101
}
82102

83103
private suspend fun SafeContext.update() {
84-
val presence = DiscordRichPresence
85-
.builder()
86-
.details("${line1Left.value(this)} | ${line1Right.value(this)}".take(128))
87-
.state("${line2Left.value(this)} | ${line2Right.value(this)}".take(128))
88-
.largeImageKey("lambda")
89-
.largeImageText(Lambda.VERSION)
90-
.smallUrl("https://mc-heads.net/avatar/${mc.gameProfile.id}/nohelm")
91-
.smallImageText(mc.gameProfile.name)
92-
.button(DiscordRichPresence.RPCButton.of("Download", "https://github.com/lambda-client/lambda"))
93-
94-
if (showTime)
95-
presence.startTimestamp(startup)
96-
97-
rpc.updatePresence(presence.build())
98-
99-
delay(delay)
104+
rpc.activityManager.setActivity {
105+
details = "${line1Left.value(this@update)} | ${line1Right.value(this@update)}".take(128)
106+
state = "${line2Left.value(this@update)} | ${line2Right.value(this@update)}".take(128)
107+
108+
largeImage("lambda", Lambda.VERSION)
109+
smallImage("https://mc-heads.net/avatar/${mc.gameProfile.id}/nohelm", mc.gameProfile.name)
110+
button("Download", "https://github.com/lambda-client/lambda")
111+
112+
if (showTime) timestamps(startup)
113+
}
100114
}
101115

102116
private enum class LineInfo(val value: SafeContext.() -> String) : Nameable {

0 commit comments

Comments
 (0)