Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from copecone/main
Browse files Browse the repository at this point in the history
Add UpdatePresence Event
  • Loading branch information
copecone authored Oct 23, 2023
2 parents 510e815 + 95aa0de commit c97aaf1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 16 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
Unofficial Discord Kotlin API

## Example
Doesn't Work Yet
It Works!
```kotlin
fun main() {
bot {
command("ping") {
execute {
reply("Pong", mention = false)
}
execute { reply("Pong") }
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ kotlin {
val main by kotlin.jvm().compilations.getting

manifest {
attributes("Main-Class" to "$packageName.discordkt.TestBot")
attributes("Main-Class" to "$packageName.discordkt.TestBotKt")
}

// main.compileDependencyFiles,
from(
main.output.classesDirs, test.output,
main.runtimeDependencyFiles.files.filter { it.name.endsWith("jar") }.map { zipTree(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import io.github.forceload.discordkt.type.gateway.PresenceStatus
import io.github.forceload.discordkt.type.gateway.event.Heartbeat
import io.github.forceload.discordkt.type.gateway.event.Hello
import io.github.forceload.discordkt.type.gateway.event.Identify
import io.github.forceload.discordkt.type.gateway.event.UpdatePresence
import io.github.forceload.discordkt.type.gateway.event.dispatch.DiscordInteraction
import io.github.forceload.discordkt.type.gateway.event.dispatch.InteractionType
import io.github.forceload.discordkt.type.gateway.event.dispatch.interaction.ApplicationCommandData
Expand Down Expand Up @@ -242,7 +243,7 @@ class DiscordBot(debug: Boolean) {
}

private fun updatePresence() {
client.send(GatewayEvent(3, d = DiscordPresence(
client.send(GatewayEvent(3, d = UpdatePresence(
since = since, activities = arrayOf(), status = status, afk = afk
)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ abstract class GatewayEventType {
// Dispatch Handled by DispatchEventType
1 to Heartbeat::class.serializer(),
2 to Identify::class.serializer(),
3 to UpdatePresence::class.serializer(),
10 to Hello::class.serializer(),
11 to HeartbeatACK::class.serializer()
) as Map<Int, KSerializer<GatewayEventType>>)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.forceload.discordkt.type.gateway.event

import io.github.forceload.discordkt.type.gateway.DiscordActivity
import io.github.forceload.discordkt.type.gateway.PresenceStatus
import kotlinx.serialization.Serializable

@Serializable
class UpdatePresence(
val since: Int?, val activities: Array<DiscordActivity>,
val status: PresenceStatus, val afk: Boolean
): GatewayEventType(), ClientSideEvent {
override val opCode: Int = 3
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,16 @@ actual class WebSocketClient actual constructor(
session = this
while (isRunning) {
var i = 0

try {
if (!incoming.isEmpty) IOScope.launch {
loop@ while (!incoming.isEmpty) {
val message = incoming.receive() as? Frame.Text? ?: break@loop
val msgString = message.readText()
DebugLogger.log("Receive ${i++}: $msgString")
events.add(msgString)
}
loop@ while (!incoming.isEmpty) {
val message = incoming.receive() as? Frame.Text? ?: break@loop
val msgString = message.readText()
DebugLogger.log("Receive ${i++}: $msgString")
events.add(msgString)
}
} catch (err: ClosedReceiveChannelException) {
val reason = this.closeReason.await()!!
val reason = closeReason.await()!!
WarnLogger.log("Close Code: ${reason.code}\nMessage: ${reason.message}")
return@client
}
Expand Down
13 changes: 12 additions & 1 deletion src/jvmTest/kotlin/io/github/forceload/discordkt/TestBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ suspend fun main() {

command("direct_message") {
arguments(
"message" desc "Message to Send" to String.require
("message" desc "Message to Send" to String.require)
.localName(DiscordLocale.ko_KR to "메세지")
.localDesc(DiscordLocale.ko_KR to "보낼 메세지")
)

description = "Send Direct Message to User"
Expand Down Expand Up @@ -62,6 +64,15 @@ suspend fun main() {
}
}
}

command("switch_status") {
execute {
if (ADMIN_IDs.contains(this.user?.username)) {
this.reply("봇의 상태를 전환시킵니다...")
status = if (status == PresenceStatus.ONLINE) PresenceStatus.DO_NOT_DISTURB else PresenceStatus.ONLINE
}
}
}
}.runBlocking()
}

Expand Down

0 comments on commit c97aaf1

Please sign in to comment.