Skip to content

Commit 9528897

Browse files
author
Nep Nep
authored
Fix bug where DiscordRPC wouldn't start or would be stuck in "Main Menu" and attempt to reconnect to discord (#213)
* I think this fixes DiscordRPC? * Attempt to reconnect DiscordRPC when discord is reopened
1 parent e84e63e commit 9528897

File tree

1 file changed

+37
-14
lines changed
  • src/main/kotlin/com/lambda/client/module/modules/misc

1 file changed

+37
-14
lines changed

src/main/kotlin/com/lambda/client/module/modules/misc/DiscordRPC.kt

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,14 @@ object DiscordRPC : Module(
7777
}
7878

7979
private fun start() {
80-
BackgroundScope.launchLooping(job)
81-
8280
LambdaMod.LOG.info("Starting Discord RPC")
81+
8382
try {
8483
ipc.connect()
8584
rpcBuilder.setStartTimestamp(OffsetDateTime.now())
8685
val richPresence = rpcBuilder.build()
8786
ipc.sendRichPresence(richPresence)
87+
BackgroundScope.launchLooping(job)
8888

8989
LambdaMod.LOG.info("Discord RPC initialised successfully")
9090
} catch (e: NoDiscordClientException) {
@@ -94,11 +94,11 @@ object DiscordRPC : Module(
9494
}
9595

9696
private fun end() {
97-
if (!initialised || ipc.status != PipeStatus.CONNECTED) return
98-
BackgroundScope.cancel(job)
99-
10097
LambdaMod.LOG.info("Shutting down Discord RPC...")
101-
ipc.close()
98+
BackgroundScope.cancel(job)
99+
if (initialised && ipc.status == PipeStatus.CONNECTED) {
100+
ipc.close()
101+
}
102102
}
103103

104104
private fun showCoordsConfirm(): Boolean {
@@ -109,12 +109,34 @@ object DiscordRPC : Module(
109109
}
110110

111111
private fun updateRPC() {
112-
if (ipc.status == PipeStatus.CONNECTED) {
113-
val richPresence = rpcBuilder
114-
.setDetails(getLine(line1Left) + getSeparator(0) + getLine(line1Right))
115-
.setState(getLine(line2Left) + getSeparator(1) + getLine(line2Right))
116-
.build()
117-
ipc.sendRichPresence(richPresence)
112+
when (ipc.status) {
113+
PipeStatus.CONNECTED -> {
114+
val richPresence = rpcBuilder
115+
.setDetails(getLine(line1Left) + getSeparator(0) + getLine(line1Right))
116+
.setState(getLine(line2Left) + getSeparator(1) + getLine(line2Right))
117+
.build()
118+
ipc.sendRichPresence(richPresence)
119+
}
120+
121+
PipeStatus.UNINITIALIZED -> {
122+
tryConnect()
123+
}
124+
125+
PipeStatus.DISCONNECTED -> {
126+
tryConnect()
127+
}
128+
129+
else -> {
130+
// Why is this necessary now kotlin? WHY
131+
}
132+
}
133+
}
134+
135+
private fun tryConnect() {
136+
try {
137+
ipc.connect()
138+
} catch (e: NoDiscordClientException) {
139+
// Add something here if you want to spam the log i guess
118140
}
119141
}
120142

@@ -187,10 +209,11 @@ object DiscordRPC : Module(
187209
}
188210

189211
fun setCustomIcons(capeType: CapeType?) {
212+
// The nullability here is VERY important, DO NOT switch this for an empty string, it causes discord to break
190213
val text = when (capeType) {
191214
CapeType.CONTRIBUTOR -> "Contributor"
192-
else -> ""
215+
else -> null
193216
}
194-
rpcBuilder.setSmallImage(capeType?.imageKey ?: "", text)
217+
rpcBuilder.setSmallImage(capeType?.imageKey, text)
195218
}
196219
}

0 commit comments

Comments
 (0)