Skip to content

Commit 5de218e

Browse files
authored
Merge pull request #98 from SLNE-Development/feat/new-discord-webhook-api
Feat/new discord webhook api
2 parents 86ffb9b + e63d81b commit 5de218e

File tree

5 files changed

+86
-81
lines changed

5 files changed

+86
-81
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
kotlin.code.style=official
22
kotlin.stdlib.default.dependency=false
33
org.gradle.parallel=true
4-
version=1.21.10-4.0.4-SNAPSHOT
4+
version=1.21.10-4.0.5-SNAPSHOT

surf-chat-api/src/main/kotlin/dev/slne/surf/chat/api/entry/HistoryFilter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ data class HistoryFilter(
3434
) {
3535
companion object {
3636
fun empty() = HistoryFilter(
37-
null, null, null, null, null, null, null, null, null, null, null
37+
null, null, null, null, null, null, null, null, null, null, 50
3838
)
3939
}
4040
}

surf-chat-bukkit/build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ dependencies {
1616

1717
runtimeOnly(project(":surf-chat-fallback"))
1818
implementation("dev.slne.surf:surf-redis:1.0.0-SNAPSHOT")
19-
implementation("com.github.BinaryWriter:discord-webhooks:1.0") {
20-
exclude(group = "org.jetbrains.kotlin", module = "kotlin-stdlib-jdk8")
21-
exclude(group = "org.jetbrains.kotlinx", module = "kotlinx-serialization-json")
19+
implementation("de.maxbossing:kotlin-discord-webhook:1") {
20+
exclude("org.jetbrains.kotlin")
2221
}
2322
implementation("com.openai:openai-java:4.11.0") {
2423
exclude(group = "org.apache.httpcomponents.client5")

surf-chat-bukkit/src/main/kotlin/dev/slne/surf/chat/bukkit/command/surfchat/SurfChatLookupCommand.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ private suspend fun Map<String, String>.parseFilters(): HistoryFilter {
208208
},
209209
channel = this["--channel"],
210210
deletedBy = this["--deletedBy"],
211-
limit = this["--limit"]?.toIntOrNull(),
211+
limit = this["--limit"]?.toIntOrNull() ?: 50,
212212
type = this["--type"]?.let { runCatching { MessageType.valueOf(it.uppercase()) }.getOrNull() }
213213
)
214214
}

surf-chat-bukkit/src/main/kotlin/dev/slne/surf/chat/bukkit/processor/post/AiModerationPostChatProcessor.kt

Lines changed: 81 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package dev.slne.surf.chat.bukkit.processor.post
22

3+
import de.maxbossing.webhookbuilder.sendWebhook
34
import dev.slne.surf.chat.api.message.MessageContext
45
import dev.slne.surf.chat.api.processor.PostChatProcessor
56
import dev.slne.surf.chat.bukkit.ai.OpenAiService
67
import dev.slne.surf.chat.bukkit.ai.openAiService
78
import dev.slne.surf.chat.bukkit.config.aiModerationConfig
9+
import dev.slne.surf.chat.bukkit.plugin
810
import dev.slne.surf.chat.bukkit.redis.event.TeamMessageRedisEvent
911
import dev.slne.surf.chat.bukkit.redisApi
1012
import dev.slne.surf.chat.bukkit.util.appendBotIcon
1113
import dev.slne.surf.chat.core.service.historyService
1214
import dev.slne.surf.surfapi.core.api.messages.Colors
1315
import dev.slne.surf.surfapi.core.api.messages.adventure.buildText
14-
import me.binarywriter.discordwebhooks.data.Image
15-
import me.binarywriter.discordwebhooks.data.Webhook
1616
import org.bukkit.Bukkit
1717
import java.awt.Color
18+
import java.net.URI
1819
import java.util.*
1920

2021
object AiModerationPostChatProcessor : PostChatProcessor {
@@ -104,101 +105,106 @@ object AiModerationPostChatProcessor : PostChatProcessor {
104105
classification: OpenAiService.ClassificationResult
105106
) {
106107
val senderUuid = messageContext.messageData.sender.uuid
107-
val webhook = Webhook {
108-
username = "Arty AI Moderation"
109-
avatarUrl = aiModerationConfig.webhookAvatarUrl
110-
embed {
111-
image = Image("https://mc-heads.net/avatar/$senderUuid")
112-
113-
title = "Chat Nachricht moderiert"
114-
when (classification.action) {
115-
OpenAiService.ClassificationAction.SILENT_FLAG -> {
116-
description =
117-
"Nachricht wurde als unangemessen markiert — bitte überprüfen und ggf. handeln"
118-
}
119108

120-
OpenAiService.ClassificationAction.DELETE -> {
121-
description = "Die Chat Nachricht wurde gelöscht."
109+
runCatching {
110+
sendWebhook(URI.create(aiModerationConfig.webhookUrl).toURL()) {
111+
name("Arty AI Moderation")
112+
avatar(aiModerationConfig.webhookAvatarUrl)
113+
embed {
114+
thumbnail {
115+
url("https://mc-heads.net/avatar/$senderUuid")
122116
}
117+
title("Chat Nachricht moderiert")
123118

124-
OpenAiService.ClassificationAction.MUTE -> {
125-
description =
126-
"Die Chat Nachricht wurde gelöscht und der Absender wurde für 7 Tage stumm geschaltet — bitte überprüfen"
127-
}
119+
when (classification.action) {
120+
OpenAiService.ClassificationAction.SILENT_FLAG -> {
121+
content("Nachricht wurde als unangemessen markiert — bitte überprüfen und ggf. handeln")
128122

129-
else -> Unit
130-
}
123+
}
131124

132-
color = when (classification.action) {
133-
OpenAiService.ClassificationAction.SILENT_FLAG -> Color.YELLOW
134-
OpenAiService.ClassificationAction.DELETE -> Color.RED
135-
OpenAiService.ClassificationAction.MUTE -> Color.MAGENTA
136-
else -> Color.WHITE
137-
}
125+
OpenAiService.ClassificationAction.DELETE -> {
126+
content("Die Chat Nachricht wurde gelöscht.")
127+
}
138128

139-
field {
140-
name = "Nachricht"
141-
value = messageContext.messageData.plainMessage
142-
inline = false
143-
}
129+
OpenAiService.ClassificationAction.MUTE -> {
130+
content("Die Chat Nachricht wurde gelöscht und der Absender wurde für 7 Tage stumm geschaltet — bitte überprüfen")
131+
}
144132

145-
field {
146-
name = "Kategorien"
147-
value = buildString {
148-
classification.flaggedScores.object2DoubleEntrySet()
149-
.sortedByDescending { it.doubleValue }
150-
.forEachIndexed { index, entry ->
151-
val category = entry.key
152-
val scorePercent = entry.doubleValue * 100
153-
append("- ${category.name} (${"%.2f".format(scorePercent)} %)")
154-
if (index != classification.flaggedScores.size - 1) {
155-
append("\n")
156-
}
157-
}
133+
else -> Unit
158134
}
159-
}
160135

161-
field {
162-
name = "Sender"
163-
value =
164-
"[${nameOrUuid(senderUuid)}](${aiModerationConfig.userPanelPrefix}$senderUuid)"
165-
inline = true
166-
}
136+
color(
137+
when (classification.action) {
138+
OpenAiService.ClassificationAction.SILENT_FLAG -> Color.YELLOW
139+
OpenAiService.ClassificationAction.DELETE -> Color.RED
140+
OpenAiService.ClassificationAction.MUTE -> Color.MAGENTA
141+
else -> Color.WHITE
142+
}
143+
)
144+
145+
field {
146+
name("Nachricht")
147+
value(messageContext.messageData.plainMessage)
148+
inline = false
149+
}
150+
151+
field {
152+
name("Kategorien")
153+
value(buildString {
154+
classification.flaggedScores.object2DoubleEntrySet()
155+
.sortedByDescending { it.doubleValue }
156+
.forEachIndexed { index, entry ->
157+
val category = entry.key
158+
val scorePercent = entry.doubleValue * 100
159+
append("- ${category.name} (${"%.2f".format(scorePercent)} %)")
160+
if (index != classification.flaggedScores.size - 1) {
161+
append("\n")
162+
}
163+
}
164+
})
165+
inline = false
166+
}
167167

168-
val receiverUuid = messageContext.messageData.receiver?.uuid
169-
if (receiverUuid != null) {
170168
field {
171-
name = "Receiver"
172-
value =
173-
"[${nameOrUuid(receiverUuid)}](${aiModerationConfig.userPanelPrefix}$receiverUuid)"
169+
name("Sender")
170+
value("[${nameOrUuid(senderUuid)}](${aiModerationConfig.userPanelPrefix}$senderUuid)")
174171
inline = true
175172
}
176-
}
177173

178-
field {
179-
name = "Server"
180-
value = messageContext.messageData.server.internalName
181-
inline = true
182-
}
174+
val receiverUuid = messageContext.messageData.receiver?.uuid
175+
if (receiverUuid != null) {
176+
field {
177+
name("Receiver")
178+
value("[${nameOrUuid(receiverUuid)}](${aiModerationConfig.userPanelPrefix}$receiverUuid)")
179+
inline = true
180+
}
181+
}
183182

184-
val channel = messageContext.messageData.channel
185-
if (channel != null) {
186183
field {
187-
name = "Channel"
188-
value = channel
184+
name("Server")
185+
value(messageContext.messageData.server.internalName)
189186
inline = true
190187
}
191-
}
192188

193-
field {
194-
name = "Type"
195-
value = messageContext.messageData.type.name
196-
inline = true
189+
val channel = messageContext.messageData.channel
190+
if (channel != null) {
191+
field {
192+
name("Channel")
193+
value(channel)
194+
inline = true
195+
}
196+
}
197+
198+
field {
199+
name("Type")
200+
value(messageContext.messageData.type.name)
201+
inline = true
202+
}
197203
}
198204
}
205+
}.onFailure {
206+
plugin.logger.warning("Failed to send webhook for AI moderation!")
199207
}
200-
201-
webhook.send(aiModerationConfig.webhookUrl)
202208
}
203209

204210
private fun nameOrUuid(uuid: UUID): String {

0 commit comments

Comments
 (0)