|
1 | 1 | package dev.slne.surf.chat.bukkit.processor.post |
2 | 2 |
|
| 3 | +import de.maxbossing.webhookbuilder.sendWebhook |
3 | 4 | import dev.slne.surf.chat.api.message.MessageContext |
4 | 5 | import dev.slne.surf.chat.api.processor.PostChatProcessor |
5 | 6 | import dev.slne.surf.chat.bukkit.ai.OpenAiService |
6 | 7 | import dev.slne.surf.chat.bukkit.ai.openAiService |
7 | 8 | import dev.slne.surf.chat.bukkit.config.aiModerationConfig |
| 9 | +import dev.slne.surf.chat.bukkit.plugin |
8 | 10 | import dev.slne.surf.chat.bukkit.redis.event.TeamMessageRedisEvent |
9 | 11 | import dev.slne.surf.chat.bukkit.redisApi |
10 | 12 | import dev.slne.surf.chat.bukkit.util.appendBotIcon |
11 | 13 | import dev.slne.surf.chat.core.service.historyService |
12 | 14 | import dev.slne.surf.surfapi.core.api.messages.Colors |
13 | 15 | import dev.slne.surf.surfapi.core.api.messages.adventure.buildText |
14 | | -import me.binarywriter.discordwebhooks.data.Image |
15 | | -import me.binarywriter.discordwebhooks.data.Webhook |
16 | 16 | import org.bukkit.Bukkit |
17 | 17 | import java.awt.Color |
| 18 | +import java.net.URI |
18 | 19 | import java.util.* |
19 | 20 |
|
20 | 21 | object AiModerationPostChatProcessor : PostChatProcessor { |
@@ -104,101 +105,106 @@ object AiModerationPostChatProcessor : PostChatProcessor { |
104 | 105 | classification: OpenAiService.ClassificationResult |
105 | 106 | ) { |
106 | 107 | 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 | | - } |
119 | 108 |
|
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") |
122 | 116 | } |
| 117 | + title("Chat Nachricht moderiert") |
123 | 118 |
|
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") |
128 | 122 |
|
129 | | - else -> Unit |
130 | | - } |
| 123 | + } |
131 | 124 |
|
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 | + } |
138 | 128 |
|
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 | + } |
144 | 132 |
|
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 |
158 | 134 | } |
159 | | - } |
160 | 135 |
|
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 | + } |
167 | 167 |
|
168 | | - val receiverUuid = messageContext.messageData.receiver?.uuid |
169 | | - if (receiverUuid != null) { |
170 | 168 | field { |
171 | | - name = "Receiver" |
172 | | - value = |
173 | | - "[${nameOrUuid(receiverUuid)}](${aiModerationConfig.userPanelPrefix}$receiverUuid)" |
| 169 | + name("Sender") |
| 170 | + value("[${nameOrUuid(senderUuid)}](${aiModerationConfig.userPanelPrefix}$senderUuid)") |
174 | 171 | inline = true |
175 | 172 | } |
176 | | - } |
177 | 173 |
|
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 | + } |
183 | 182 |
|
184 | | - val channel = messageContext.messageData.channel |
185 | | - if (channel != null) { |
186 | 183 | field { |
187 | | - name = "Channel" |
188 | | - value = channel |
| 184 | + name("Server") |
| 185 | + value(messageContext.messageData.server.internalName) |
189 | 186 | inline = true |
190 | 187 | } |
191 | | - } |
192 | 188 |
|
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 | + } |
197 | 203 | } |
198 | 204 | } |
| 205 | + }.onFailure { |
| 206 | + plugin.logger.warning("Failed to send webhook for AI moderation!") |
199 | 207 | } |
200 | | - |
201 | | - webhook.send(aiModerationConfig.webhookUrl) |
202 | 208 | } |
203 | 209 |
|
204 | 210 | private fun nameOrUuid(uuid: UUID): String { |
|
0 commit comments