Skip to content

Commit d8334ca

Browse files
committed
refactor: additional check & update delay
1 parent eb14fd8 commit d8334ca

File tree

1 file changed

+22
-7
lines changed
  • common/src/main/kotlin/com/lambda/module/modules/client

1 file changed

+22
-7
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ object DiscordRPC : Module(
5252
/* Technical settings */
5353
private var rpcServer by setting("RPC Server", "http://127.0.0.1:8080") { page == Page.Settings } // TODO: Change this in production
5454
private var apiVersion by setting("API Version", ApiVersion.V1) { page == Page.Settings }
55-
private val delay by setting("Update Delay", 5000, 5000..10000, 1, unit = "ms", visibility = { page == Page.Settings })
55+
private val delay by setting("Update Delay", 15000, 15000..30000, 100, unit = "ms", visibility = { page == Page.Settings })
5656

5757
/* Party settings */
5858
private val enableParty by setting("Enable Party", true, description = "Allows you to create parties.") { page == Page.Party }
@@ -64,6 +64,7 @@ object DiscordRPC : Module(
6464
private var startup = System.currentTimeMillis()
6565
private val dimensionRegex = Regex("""\b\w+_\w+\b""")
6666

67+
private var ready: ReadyEvent? = null
6768
private var discordAuth: AuthenticatePacket.Data? = null
6869
private var rpcAuth: Authentication? = null
6970
private var currentParty: AtomicReference<Party?> = AtomicReference(null)
@@ -157,6 +158,21 @@ object DiscordRPC : Module(
157158
rpc.disconnect()
158159
leaveParty(rpcServer, apiVersion.value, rpcAuth?.accessToken ?: return)
159160
}
161+
162+
ready = null
163+
discordAuth = null
164+
rpcAuth = null
165+
currentParty.lazySet(null)
166+
}
167+
168+
fun createParty() {
169+
if (!allowed) return
170+
171+
createParty(rpcServer, apiVersion.value, rpcAuth?.accessToken ?: return, maxPlayers, public)
172+
.also { response ->
173+
if (response.error != null) warn("Failed to create a party: ${response.error}")
174+
currentParty.lazySet(response.data)
175+
}
160176
}
161177

162178
fun join(id: String = rpc.activityManager.activity?.party?.id ?: "") {
@@ -203,8 +219,11 @@ object DiscordRPC : Module(
203219
}
204220

205221
private suspend fun KDiscordIPC.register(auth: ConnectionEvent.Connect.Login.Key) {
206-
// TODO: Check if the rpc is already ready
222+
if (rpc.connected && ready != null) return
223+
207224
on<ReadyEvent> {
225+
ready = this
226+
208227
// Party features
209228
subscribe(DiscordEvent.ActivityJoinRequest)
210229
subscribe(DiscordEvent.ActivityJoin)
@@ -236,11 +255,7 @@ object DiscordRPC : Module(
236255
rpcAuth = response.data
237256
}
238257

239-
if (createByDefault) createParty(rpcServer, apiVersion.value, rpcAuth?.accessToken ?: return@on, maxPlayers, public)
240-
.also { response ->
241-
if (response.error != null) warn("Failed to create a party: ${response.error}")
242-
currentParty.lazySet(response.data)
243-
}
258+
if (createByDefault) createParty()
244259
}
245260

246261
// Event when someone would like to join your party

0 commit comments

Comments
 (0)