Skip to content

Commit c9c606c

Browse files
authored
fix Discord RPC (#279)
1 parent 4580d9d commit c9c606c

1 file changed

Lines changed: 92 additions & 75 deletions

File tree

  • src/main/kotlin/com/lambda/module/modules/client

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

Lines changed: 92 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -39,82 +39,99 @@ import dev.cbyrne.kdiscordipc.data.activity.largeImage
3939
import dev.cbyrne.kdiscordipc.data.activity.smallImage
4040
import dev.cbyrne.kdiscordipc.data.activity.timestamps
4141
import kotlinx.coroutines.delay
42+
import java.util.concurrent.atomic.AtomicBoolean
4243

4344
object Discord : Module(
44-
name = "Discord",
45-
description = "Discord Rich Presence configuration",
46-
tag = ModuleTag.CLIENT,
47-
enabledByDefault = true,
45+
name = "Discord",
46+
description = "Discord Rich Presence configuration",
47+
tag = ModuleTag.CLIENT,
48+
enabledByDefault = true,
4849
) {
49-
private val delay by setting("Update Delay", 5000L, 5000L..30000L, 100L, unit = "ms")
50-
private val showTime by setting("Show Time", true, description = "Show how long you have been playing for.")
51-
private val line1Left by setting("Line 1 Left", LineInfo.World)
52-
private val line1Right by setting("Line 1 Right", LineInfo.Username)
53-
private val line2Left by setting("Line 2 Left", LineInfo.Version)
54-
private val line2Right by setting("Line 2 Right", LineInfo.Fps)
55-
56-
val rpc = KDiscordIPC(Lambda.APP_ID, scope = EventFlow.lambdaScope)
57-
58-
private var startup = System.currentTimeMillis()
59-
60-
var discordAuth: AuthenticatePacket.Data? = null; private set
61-
62-
init {
63-
// ts is fucked frfr
64-
listenOnce<TickEvent.Pre> {
65-
runConcurrent { handleLoop() }
66-
67-
return@listenOnce true
68-
}
69-
70-
onEnable { runConcurrent { start(); handleLoop() } }
71-
runConcurrent { start() }
72-
onDisable { stop() }
73-
}
74-
75-
private suspend fun start() {
76-
if (rpc.connected) return
77-
78-
rpc.connect()
79-
80-
val auth = rpc.applicationManager.authenticate()
81-
82-
linkDiscord(discordToken = auth.accessToken)
83-
.onSuccess { updateToken(it); discordAuth = auth }
84-
.onFailure { LOG.error(it); warn("Failed to link your discord account") }
85-
}
86-
87-
private fun stop() {
88-
if (rpc.connected) rpc.disconnect()
89-
}
90-
91-
private suspend fun SafeContext.handleLoop() {
92-
while (rpc.connected) {
93-
update()
94-
delay(delay)
95-
}
96-
}
97-
98-
private suspend fun SafeContext.update() {
99-
rpc.activityManager.setActivity {
100-
details = "${line1Left.value(this@update)} | ${line1Right.value(this@update)}".take(128)
101-
state = "${line2Left.value(this@update)} | ${line2Right.value(this@update)}".take(128)
102-
103-
largeImage("lambda", Lambda.VERSION)
104-
smallImage("https://mc-heads.net/avatar/${mc.gameProfile.id}/nohelm", mc.gameProfile.name)
105-
//button("Download", "https://github.com/lambda-client/lambda")
106-
107-
if (showTime) timestamps(startup)
108-
}
109-
}
110-
111-
private enum class LineInfo(val value: SafeContext.() -> String) : Nameable {
112-
Version({ Lambda.VERSION }),
113-
World({ worldName }),
114-
Username({ mc.session.username }),
115-
Health({ "${player.fullHealth} HP" }),
116-
Hunger({ "${player.hungerManager.foodLevel} Hunger" }),
117-
Dimension({ world.dimensionName }),
118-
Fps({ "${mc.currentFps} FPS" });
119-
}
50+
private val delay by setting("Update Delay", 5000L, 5000L..30000L, 100L, unit = "ms")
51+
private val showTime by setting("Show Time", true, description = "Show how long you have been playing for.")
52+
private val line1Left by setting("Line 1 Left", LineInfo.World)
53+
private val line1Right by setting("Line 1 Right", LineInfo.Username)
54+
private val line2Left by setting("Line 2 Left", LineInfo.Version)
55+
private val line2Right by setting("Line 2 Right", LineInfo.Fps)
56+
57+
val rpc by lazy {
58+
KDiscordIPC(Lambda.APP_ID, scope = EventFlow.lambdaScope)
59+
}
60+
var connecting = AtomicBoolean(false)
61+
62+
private val startup = System.currentTimeMillis()
63+
64+
var discordAuth: AuthenticatePacket.Data? = null
65+
private set
66+
67+
init {
68+
listenOnce<TickEvent.Pre> {
69+
runConcurrent {
70+
if (start()) handleLoop()
71+
}
72+
73+
return@listenOnce true
74+
}
75+
76+
onEnable {
77+
runConcurrent {
78+
if (start()) handleLoop()
79+
}
80+
}
81+
onDisable { stop() }
82+
}
83+
84+
private suspend fun start(): Boolean {
85+
if (connecting.getAndSet(true)) return false
86+
87+
rpc.connect()
88+
89+
val auth = rpc.applicationManager.authenticate()
90+
91+
linkDiscord(discordToken = auth.accessToken)
92+
.onSuccess {
93+
updateToken(it)
94+
discordAuth = auth
95+
}
96+
.onFailure {
97+
LOG.error(it)
98+
warn("Failed to link your discord account")
99+
}
100+
return true
101+
}
102+
103+
private fun stop() {
104+
if (rpc.connected) rpc.disconnect()
105+
connecting.set(false)
106+
}
107+
108+
private suspend fun SafeContext.handleLoop() {
109+
while (rpc.connected) {
110+
update()
111+
delay(delay)
112+
}
113+
}
114+
115+
private suspend fun SafeContext.update() {
116+
rpc.activityManager.setActivity {
117+
details = "${line1Left.value(this@update)} | ${line1Right.value(this@update)}".take(128)
118+
state = "${line2Left.value(this@update)} | ${line2Right.value(this@update)}".take(128)
119+
120+
largeImage("lambda", Lambda.VERSION)
121+
smallImage("https://mc-heads.net/avatar/${mc.gameProfile.id}/nohelm", mc.gameProfile.name)
122+
//button("Download", "https://github.com/lambda-client/lambda")
123+
124+
if (showTime) timestamps(startup)
125+
}
126+
}
127+
128+
private enum class LineInfo(val value: SafeContext.() -> String) : Nameable {
129+
Version({ Lambda.VERSION }),
130+
World({ worldName }),
131+
Username({ mc.session.username }),
132+
Health({ "${player.fullHealth} HP" }),
133+
Hunger({ "${player.hungerManager.foodLevel} Hunger" }),
134+
Dimension({ world.dimensionName }),
135+
Fps({ "${mc.currentFps} FPS" });
136+
}
120137
}

0 commit comments

Comments
 (0)