### Basic KDiscordIPC Usage Example Source: https://github.com/caoimhebyrne/kdiscordipc/blob/main/README.md Initialize KDiscordIPC with your client ID, connect to Discord, set a custom activity with buttons and timestamps, and retrieve user information. This example demonstrates core functionalities like event handling and activity management. ```kotlin val ipc = KDiscordIPC("YOUR_CLIENT_ID") ipc.on { logger.info("Ready! (${data.user.username}#${data.user.discriminator})") ipc.activityManager.setActivity("Hello", "world") { button("Click me", "https://google.com") timestamps(System.currentTimeMillis(), System.currentTimeMillis() + 50000) } // Want to get the information of another user? Sure thing! val user = ipc.userManager.getUser("USER_ID") } ipc.connect() ``` -------------------------------- ### Install KDiscordIPC via Gradle Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Add KDiscordIPC to your Gradle project by including the Jitpack repository and the library dependency. ```kotlin // settings.gradle.kts — no change needed beyond standard setup // build.gradle.kts repositories { mavenCentral() maven(url = "https://jitpack.io") } dependencies { implementation("com.github.caoimhebyrne:KDiscordIPC:0.2.6") } ``` -------------------------------- ### Get and Set Discord Voice Settings Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Reads or mutates the user's Discord voice settings. Subscribe to `VoiceSettingsUpdate` to receive live changes. The `getVoiceSettings()` function returns a `VoiceSettings` object. ```kotlin ipc.on { // Read current settings val settings = ipc.voiceSettingsManager.getVoiceSettings() println("Input device : ${settings.input.deviceId}") println("Output device: ${settings.output.deviceId}") println("Muted : ${settings.mute}, Deafened: ${settings.deaf}") println("Noise suppression: ${settings.noiseSuppression}") // Subscribe to live updates ipc.voiceSettingsManager.subscribeToVoiceSettingsUpdate() } ipc.on { // data is a VoiceSettings snapshot val status = if (data.mute) "muted" else "unmuted" println("User is now $status") println("Available input devices: ${data.input.availableDevices?.map { it.name }}") } ``` -------------------------------- ### Authorize and Authenticate with Discord OAuth2 Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Initiates the OAuth2 flow to obtain a bearer token. Call `authorize` first to get an auth code, then `authenticate` with a token obtained from your backend. Tokens are valid for seven days and require a specific redirect URI in the Discord Developer Portal. ```kotlin ipc.on { // Step 1: authorize — prompts the user in Discord if necessary val authCode = ipc.applicationManager.authorize( scopes = arrayOf("rpc", "identify", "guilds"), clientId = "945428344806183003" ) println("Auth code: ${authCode.code}") // Step 2: exchange the code for a bearer token via your own backend, // then authenticate with the resulting token val tokenFromBackend = "user-bearer-token-here" val authData = ipc.applicationManager.authenticate(token = tokenFromBackend) println("Authenticated as: ${authData.user?.username}") println("Scopes granted : ${authData.scopes}") println("Token expires : ${authData.expires}") } ``` -------------------------------- ### Initialize and Connect KDiscordIPC Client Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Instantiate the KDiscordIPC client with your application's client ID and register event listeners before connecting. The `connect()` function blocks while listening. ```kotlin import dev.cbyrne.kdiscordipc.KDiscordIPC import dev.cbyrne.kdiscordipc.core.event.impl.ReadyEvent import dev.cbyrne.kdiscordipc.core.event.impl.DisconnectedEvent import dev.cbyrne.kdiscordipc.core.event.impl.ErrorEvent suspend fun main() { val ipc = KDiscordIPC("945428344806183003") // Fires once the handshake with Discord is complete ipc.on { println("Connected as ${data.user.username}#${data.user.discriminator}") // data.user: User(id, username, discriminator, avatar, bot, flags, premiumType) } ipc.on { println("Disconnected from Discord IPC") } ipc.on { println("IPC error (${data.code}): ${data.message}") } // Blocks while listening; connects to discord-ipc-0 by default ipc.connect() // suspend fun connect(index: Int = 0) // To disconnect later from another coroutine: // ipc.disconnect() } ``` -------------------------------- ### Add KDiscordIPC Dependency Source: https://github.com/caoimhebyrne/kdiscordipc/blob/main/README.md Add KDiscordIPC to your project by including the Jitpack repository and the implementation dependency. Ensure you use the correct version. ```kotlin repositories { mavenCentral() maven(url = "https://jitpack.io") } dependencies { implementation("com.github.caoimhebyrne:KDiscordIPC:0.2.2") } ``` -------------------------------- ### Subscribe to Discord Events Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Subscribe to specific Discord events after establishing a connection to receive real-time updates. This must be called within a `ReadyEvent` handler. ```kotlin import dev.cbyrne.kdiscordipc.core.event.DiscordEvent ipc.on { // Subscribe to multiple events ipc.subscribe(DiscordEvent.CurrentUserUpdate) ipc.subscribe(DiscordEvent.ActivityJoin) ipc.subscribe(DiscordEvent.ActivityJoinRequest) ipc.subscribe(DiscordEvent.ActivityInvite) ipc.subscribe(DiscordEvent.ActivitySpectate) ipc.subscribe(DiscordEvent.VoiceSettingsUpdate) ipc.subscribe(DiscordEvent.RelationshipUpdate) ipc.subscribe(DiscordEvent.SpeakingStart) ipc.subscribe(DiscordEvent.SpeakingStop) } ``` -------------------------------- ### Register KDiscordIPC Event Listeners Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Register typed listeners for various Discord IPC events using the `on` function. Each listener runs in its own coroutine. ```kotlin ipc.on { // data.secret: join secret string passed by Discord println("Joined a party with secret: ${data.secret}") } ipc.on { println("Invited by ${data.user.username} to party ${data.activity.party.id}") // Accept the invite directly val accepted = ipc.activityManager.acceptInvite(data) println("Invite accepted: $accepted") } ipc.on { println("Muted: ${data.mute}, Deafened: ${data.deaf}") } ipc.on { println("Current user profile changed: ${data.username}") } ``` -------------------------------- ### ApplicationManager.authorize and ApplicationManager.authenticate Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Obtains an OAuth2 bearer token for the connected Discord user. Call `authorize` to initiate the flow, then `authenticate` with the returned code or an existing token. Tokens are valid for seven days and require `http://127.0.0.1` as a redirect URI in the Discord Developer Portal. ```APIDOC ## `ApplicationManager.authorize` / `authenticate` — OAuth2 Bearer Token Obtains an OAuth2 bearer token for the connected Discord user. Call `authorize` to initiate the flow (Discord will prompt the user if not already launched from Discord), then `authenticate` with the returned code or an existing token. Tokens are valid for seven days. Requires `http://127.0.0.1` as a redirect URI in the Discord Developer Portal. ```kotlin ipc.on { // Step 1: authorize — prompts the user in Discord if necessary val authCode = ipc.applicationManager.authorize( scopes = arrayOf("rpc", "identify", "guilds"), clientId = "945428344806183003" ) println("Auth code: ${authCode.code}") // Step 2: exchange the code for a bearer token via your own backend, // then authenticate with the resulting token val tokenFromBackend = "user-bearer-token-here" val authData = ipc.applicationManager.authenticate(token = tokenFromBackend) println("Authenticated as: ${authData.user?.username}") println("Scopes granted : ${authData.scopes}") println("Token expires : ${authData.expires}") } ``` ``` -------------------------------- ### Fetch Discord Friend List Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Returns the full list of the current user's relationships (friends, blocked users, etc.) as a `List`. Each `Relationship` contains a type and a user object. ```kotlin ipc.on { val relationships = ipc.relationshipManager.getRelationships() println("Total relationships: ${relationships.size}") relationships.forEach { rel -> println("Type ${rel.type}: ${rel.user.username}#${rel.user.discriminator}") } } ``` -------------------------------- ### Set Discord Rich Presence using Kotlin DSL Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Sets the authenticated user's Discord rich presence using a Kotlin DSL builder. Supports various activity elements like images, timestamps, party info, secrets, and buttons. Rate-limited to 5 updates per 20 seconds. ```kotlin ipc.on { // DSL form — most ergonomic ipc.activityManager.setActivity(details = "In a match", state = "2 of 5 players") { largeImage("map_thumbnail", "Dust II") smallImage("rank_icon", "Gold Nova III") timestamps(start = System.currentTimeMillis(), end = System.currentTimeMillis() + 3_600_000) party(id = "lobby-abc123", currentSize = 2, maxSize = 5) secrets(join = "super-secret-join-token") button("View Profile", "https://example.com/profile") statusDisplayType(Activity.ActivityStatusDisplayType.STATE) } // Object form val activity = activity(details = "Idle", state = "Main Menu") { largeImage("logo", "My Game") timestamps(System.currentTimeMillis()) } ipc.activityManager.setActivity(activity) // Clear presence entirely ipc.activityManager.clearActivity() // Read back the last-set activity println(ipc.activityManager.activity) } ``` -------------------------------- ### VoiceSettingsManager.getVoiceSettings / setVoiceSettings Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Allows reading and modifying the user's Discord voice settings. It provides access to input/output devices, audio modes, and other voice-related configurations. Live changes can be tracked via `VoiceSettingsUpdate` events. ```APIDOC ## `VoiceSettingsManager.getVoiceSettings` / `setVoiceSettings` — Voice Settings Reads or mutates the user's Discord voice settings. `getVoiceSettings()` returns a `VoiceSettings` object with input/output device info, mode, AGC, noise suppression, echo cancellation, QoS, mute, and deaf state. Subscribe to `VoiceSettingsUpdate` to receive live changes. ### Method ```kotlin // Get current settings ipcs.voiceSettingsManager.getVoiceSettings() // Subscribe to updates ipcs.voiceSettingsManager.subscribeToVoiceSettingsUpdate() ``` ### Request Example (Get Settings) ```kotlin val settings = ipcs.voiceSettingsManager.getVoiceSettings() println("Input device : ${settings.input.deviceId}") println("Output device: ${settings.output.deviceId}") println("Muted : ${settings.mute}, Deafened: ${settings.deaf}") println("Noise suppression: ${settings.noiseSuppression}") ``` ### Request Example (Subscribe to Updates) ```kotlin ipcs.voiceSettingsManager.subscribeToVoiceSettingsUpdate() ipcs.on { // data is a VoiceSettings snapshot val status = if (data.mute) "muted" else "unmuted" println("User is now $status") println("Available input devices: ${data.input.availableDevices?.map { it.name }}") } ``` ### Response - `getVoiceSettings()` returns a `VoiceSettings` object containing: - `input` (VoiceSettings.DeviceConfig) - `output` (VoiceSettings.DeviceConfig) - `mode` (String) - `automaticGainControl` (Boolean) - `noiseSuppression` (String) - `echoCancellation` (Boolean) - `qualityOfService` (Boolean) - `mute` (Boolean) - `deaf` (Boolean) - `VoiceSettingsUpdateEvent.data` provides a snapshot of the `VoiceSettings` object when changes occur. ``` -------------------------------- ### ActivityManager.acceptInvite Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Accepts an incoming activity invite. This method is typically called in response to an `ActivityInviteEvent`. ```APIDOC ## `ActivityManager.acceptInvite` — Accept Activity Invite Accepts an incoming activity invite received via `ActivityInviteEvent`. Returns `true` if Discord acknowledged the acceptance successfully. ### Method ```kotlin ipcs.activityManager.acceptInvite(inviteData) ``` ### Parameters - `inviteData` (ActivityInviteEvent.Data) - Required - The data from the `ActivityInviteEvent` containing the invite details. ### Request Example ```kotlin ipcs.on { println("Invite from ${data.user.username} — party: ${data.activity.party.id}") val success = ipcs.activityManager.acceptInvite(data) if (success) { println("Joined party successfully") } else { println("Failed to join party") } } ``` ### Response - `success` (Boolean) - `true` if Discord acknowledged the acceptance, `false` otherwise. ``` -------------------------------- ### Accept Discord Activity Invite Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Accepts an incoming activity invite received via `ActivityInviteEvent`. Returns `true` if Discord acknowledged the acceptance successfully. Ensure you are handling the `ActivityInviteEvent` to use this function. ```kotlin ipc.on { println("Invite from ${data.user.username} — party: ${data.activity.party.id}") val success = ipc.activityManager.acceptInvite(data) if (success) { println("Joined party successfully") } else { println("Failed to join party") } } ``` -------------------------------- ### Fetch Discord User by ID Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Retrieves public profile information for any Discord user by their snowflake ID. Returns a `User` data class. Requires the user's ID as a string. ```kotlin ipc.on { val user = ipc.userManager.getUser("843135686173392946") println("Username : ${user.username}#${user.discriminator}") println("Avatar : ${user.avatar}") println("Bot : ${user.bot}") println("Nitro : ${user.premiumType}") } ``` -------------------------------- ### Subscribe to Current User Updates Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Subscribes to `CURRENT_USER_UPDATE` events to automatically keep `currentUser` up to date. Access `currentUser` only after the first `CurrentUserUpdateEvent` has fired. ```kotlin ipc.on { ipc.userManager.subscribeToUserUpdates() } ipc.on { val user = ipc.userManager.currentUser println("Profile refreshed: ${user?.username}#${user?.discriminator}") } ``` -------------------------------- ### ActivityManager.setActivity Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Sets the authenticated user's Discord rich presence. This can be done using an Activity object directly or a Kotlin DSL builder. Discord rate-limits this to 5 updates per 20 seconds. ```APIDOC ## `ActivityManager.setActivity` — Set Rich Presence Sets the authenticated user's Discord rich presence using either an `Activity` object directly or the Kotlin DSL builder. The DSL supports `button`, `timestamps`, `largeImage`, `smallImage`, `party`, `secrets`, and `statusDisplayType` extension functions. Rate-limited to 5 updates per 20 seconds by Discord. ### Method ```kotlin ipc.activityManager.setActivity(...) ``` ### Parameters - `activity` (Activity) - Optional - The activity object to set. - `details` (String) - Optional - The details field for the activity. - `state` (String) - Optional - The state field for the activity. - `dsl` (Activity.() -> Unit) - Optional - A lambda function to configure the activity using the DSL. ### Request Example (DSL) ```kotlin ipcs.activityManager.setActivity(details = "In a match", state = "2 of 5 players") { largeImage("map_thumbnail", "Dust II") smallImage("rank_icon", "Gold Nova III") timestamps(start = System.currentTimeMillis(), end = System.currentTimeMillis() + 3_600_000) party(id = "lobby-abc123", currentSize = 2, maxSize = 5) secrets(join = "super-secret-join-token") button("View Profile", "https://example.com/profile") statusDisplayType(Activity.ActivityStatusDisplayType.STATE) } ``` ### Request Example (Object) ```kotlin val activity = activity(details = "Idle", state = "Main Menu") { largeImage("logo", "My Game") timestamps(System.currentTimeMillis()) } ipcs.activityManager.setActivity(activity) ``` ### Request Example (Clear Presence) ```kotlin ipcs.activityManager.clearActivity() ``` ### Response This method does not return a value directly, but updates the user's presence. ``` -------------------------------- ### RelationshipManager.getRelationships Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Fetches the complete list of the current user's relationships, including friends and blocked users. Each relationship entry contains a type and associated user information. ```APIDOC ## `RelationshipManager.getRelationships` — Fetch Friend List Returns the full list of the current user's relationships (friends, blocked users, etc.) as a `List`. Each `Relationship` has a `type: Int` and a `user: User`. ### Method ```kotlin ipcs.relationshipManager.getRelationships() ``` ### Request Example ```kotlin val relationships = ipcs.relationshipManager.getRelationships() println("Total relationships: ${relationships.size}") relationships.forEach { rel -> println("Type ${rel.type}: ${rel.user.username}#${rel.user.discriminator}") } ``` ### Response - `List` - A list of relationship objects. Each `Relationship` object contains: - `type` (Int) - The type of relationship (e.g., friend, blocked). - `user` (User) - The user object associated with the relationship. ``` -------------------------------- ### UserManager.getUser Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Retrieves public profile information for a Discord user by their snowflake ID. The returned `User` object contains details like username, avatar, and bot status. ```APIDOC ## `UserManager.getUser` — Fetch User by ID Retrieves public profile information for any Discord user by their snowflake ID. Returns a `User` data class with `id`, `username`, `discriminator`, `avatar`, `bot`, `flags`, and `premiumType`. ### Method ```kotlin ipcs.userManager.getUser(userId) ``` ### Parameters - `userId` (String) - Required - The snowflake ID of the user to fetch. ### Request Example ```kotlin val user = ipcs.userManager.getUser("843135686173392946") println("Username : ${user.username}#${user.discriminator}") println("Avatar : ${user.avatar}") println("Bot : ${user.bot}") println("Nitro : ${user.premiumType}") ``` ### Response - `User` (Object) - An object containing the user's profile information: - `id` (String) - `username` (String) - `discriminator` (String) - `avatar` (String?) - `bot` (Boolean) - `flags` (Int?) - `premiumType` (Int?) ``` -------------------------------- ### UserManager.subscribeToUserUpdates / currentUser Source: https://context7.com/caoimhebyrne/kdiscordipc/llms.txt Subscribes to `CURRENT_USER_UPDATE` events to automatically track changes to the current user's profile. The `currentUser` property provides access to the latest user information after the first update event. ```APIDOC ## `UserManager.subscribeToUserUpdates` / `currentUser` — Track Current User Subscribes to `CURRENT_USER_UPDATE` events and keeps `currentUser` up to date automatically. Access `currentUser` only after the first `CurrentUserUpdateEvent` fires. ### Method ```kotlin ipcs.userManager.subscribeToUserUpdates() ``` ### Usage Call `subscribeToUserUpdates()` to begin receiving updates. The `currentUser` property can then be accessed. ### Request Example ```kotlin // Subscribe to updates ipcs.userManager.subscribeToUserUpdates() // Access current user info after an update event ipcs.on { val user = ipcs.userManager.currentUser println("Profile refreshed: ${user?.username}#${user?.discriminator}") } ``` ### Response - `currentUser` (User?) - The current user's profile information. This is `null` until the first `CurrentUserUpdateEvent` is received. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.