1818package com.lambda.module.modules.client
1919
2020import com.lambda.Lambda
21+ import com.lambda.Lambda.LOG
2122import com.lambda.context.SafeContext
22- import com.lambda.event.events.ClientEvent
23+ import com.lambda.event.EventFlow
2324import com.lambda.event.events.WorldEvent
2425import com.lambda.event.listener.SafeListener.Companion.listenOnce
25- import com.lambda.event.listener.UnsafeListener.Companion.listenOnceUnsafe
2626import com.lambda.module.Module
2727import com.lambda.module.tag.ModuleTag
28+ import com.lambda.network.NetworkManager.updateToken
29+ import com.lambda.network.api.v1.endpoints.linkDiscord
2830import com.lambda.threading.runConcurrent
31+ import com.lambda.util.Communication.warn
2932import com.lambda.util.Nameable
3033import com.lambda.util.extension.dimensionName
3134import com.lambda.util.extension.fullHealth
3235import 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
3642import kotlinx.coroutines.delay
3743
3844object 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