Skip to content

Commit

Permalink
feat: add content checking when comment
Browse files Browse the repository at this point in the history
Signed-off-by: Samarium <[email protected]>
  • Loading branch information
Samarium150 committed Feb 9, 2022
1 parent de20db8 commit 3126a02
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

group = "io.github.samarium150"
version = "1.6.1"
version = "1.7.0"

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ object MiraiConsoleDriftBottle : KotlinPlugin(
JvmPluginDescription(
id = "io.github.samarium150.mirai.plugin.mirai-console-drift-bottle",
name = "Drift Bottle",
version = "1.6.1",
version = "1.7.0",
) {
author("Samarium150")
info("简单的漂流瓶插件")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ package io.github.samarium150.mirai.plugin.driftbottle.command

import io.github.samarium150.mirai.plugin.driftbottle.MiraiConsoleDriftBottle
import io.github.samarium150.mirai.plugin.driftbottle.config.CommandConfig
import io.github.samarium150.mirai.plugin.driftbottle.config.GeneralConfig
import io.github.samarium150.mirai.plugin.driftbottle.config.ReplyConfig
import io.github.samarium150.mirai.plugin.driftbottle.data.CommentData
import io.github.samarium150.mirai.plugin.driftbottle.util.ContentCensor
import io.github.samarium150.mirai.plugin.driftbottle.util.indexOfBottle
import io.github.samarium150.mirai.plugin.driftbottle.util.isNotOutOfRange
import io.github.samarium150.mirai.plugin.driftbottle.util.randomDelay
Expand Down Expand Up @@ -60,6 +63,14 @@ object Comment : SimpleCommand(
return
}
val commentStr = comment.content
if (GeneralConfig.enableContentCensor) runCatching {
if (!ContentCensor.determine(commentStr)) {
sendMessage(ReplyConfig.invalid)
return
}
}.onFailure {
MiraiConsoleDriftBottle.logger.error(it)
}
val realIndex = index?.minus(1)
if (isNotOutOfRange(realIndex)) {
val nick = fromEvent.sender.nameCardOrNick
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ object ContentCensor {
ContentCensorToken.expiresIn = credential.expires_in
}

suspend fun determine(content: String): Boolean {
val response = client.submitForm<TextCensorResponseBody>(
url = "${URLS.TEXT_CENSOR.url}?access_token=${ContentCensorToken.accessToken}",
formParameters = parametersOf("text", content)
)
logger.info(response.toString())
return if (response.error_code != null) {
logger.error(response.error_msg)
true
} else response.conclusion != "不合规"
}

suspend fun determine(chain: MessageChain): Boolean {
if (Date().time >= ContentCensorToken.timestamp + ContentCensorToken.expiresIn * 1000
|| ContentCensorToken.accessToken.isEmpty()
Expand Down

0 comments on commit 3126a02

Please sign in to comment.