1818package com.lambda.module.modules.client
1919
2020import com.lambda.Lambda
21- import com.lambda.Lambda.LOG
2221import com.lambda.context.SafeContext
23- import com.lambda.event.EventFlow
22+ import com.lambda.event.events.ClientEvent
2423import com.lambda.event.events.WorldEvent
2524import 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
3028import com.lambda.threading.runConcurrent
31- import com.lambda.util.Communication.warn
3229import com.lambda.util.Nameable
3330import com.lambda.util.extension.dimensionName
3431import com.lambda.util.extension.fullHealth
3532import com.lambda.util.extension.worldName
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
33+ import dev.firstdark.rpc.DiscordRpc
34+ import dev.firstdark.rpc.handlers.RPCEventHandler
35+ import dev.firstdark.rpc.models.DiscordRichPresence
4236import kotlinx.coroutines.delay
4337
4438object Discord : Module(
@@ -54,63 +48,55 @@ object Discord : Module(
5448 private val line2Left by setting(" Line 2 Left" , LineInfo .Version )
5549 private val line2Right by setting(" Line 2 Right" , LineInfo .Fps )
5650
57- val rpc = KDiscordIPC (Lambda .APP_ID , scope = EventFlow .lambdaScope)
58-
51+ val rpc = DiscordRpc ()
5952 private var startup = System .currentTimeMillis()
6053
61- var discordAuth: AuthenticatePacket .Data ? = null ; private set
54+ // var discordAuth: AuthenticatePacket.Data? = null; private set
6255
6356 init {
6457 listenOnce<WorldEvent .Join > {
65- if (rpc.connected) return @listenOnce false
66-
6758 runConcurrent {
6859 start()
69- handleLoop ()
60+ update ()
7061 }
7162
7263 return @listenOnce true
7364 }
7465
75- onEnable { runConcurrent { start(); handleLoop () } }
66+ onEnable { runConcurrent { start(); update () } }
7667 onDisable { stop() }
7768 }
7869
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" ) }
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") }
9077 }
9178
9279 private fun stop () {
93- if (rpc.connected) rpc.disconnect()
94- }
95-
96- private suspend fun SafeContext.handleLoop () {
97- while (rpc.connected) {
98- update()
99- delay(delay)
100- }
80+ rpc.shutdown()
10181 }
10282
10383 private suspend fun SafeContext.update () {
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- }
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)
114100 }
115101
116102 private enum class LineInfo (val value : SafeContext .() -> String ) : Nameable {
0 commit comments