Skip to content

Commit

Permalink
feat: support tags filtering
Browse files Browse the repository at this point in the history
Closes #55
  • Loading branch information
Samarium150 committed Dec 6, 2021
1 parent 6b339d1 commit a44483c
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/CommandConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import net.mamoe.mirai.console.data.value
* @constructor Create a Command config instance <br> 实例化命令配置
* @see net.mamoe.mirai.console.data.AutoSavePluginConfig
*/
object CommandConfig: AutoSavePluginConfig("CommandConfig") {
object CommandConfig : AutoSavePluginConfig("CommandConfig") {

/**
* Composite command secondary names
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/ImageData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ data class ImageData(
fun toReadable(): String {
return (
"标题: ${title}\n" +
"作者: $author (uid: ${uid})\n" +
"标签: ${tags}\n" +
"链接: https://pixiv.net/artworks/${pid}"
)
"作者: $author (uid: ${uid})\n" +
"标签: ${tags}\n" +
"链接: https://pixiv.net/artworks/${pid}"
)
}
}
18 changes: 13 additions & 5 deletions src/main/kotlin/Lolicon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import java.net.URL
* @constructor Create a CompositeCommand instance <br> 实例化命令
* @see net.mamoe.mirai.console.command.CompositeCommand
*/
object Lolicon: CompositeCommand(
object Lolicon : CompositeCommand(
Main, primaryName = "lolicon",
secondaryNames = CommandConfig.lolicon
) {
Expand Down Expand Up @@ -99,8 +99,10 @@ object Lolicon: CompositeCommand(
val (r18, recall, cooldown) = ExecutionConfig.create(subject)
val tags = tagArgs.joinToString(" ")
val body = if (tags.isNotEmpty())
RequestBody(r18, 1, listOf(), "", Utils.processTags(tags),
listOf(PluginConfig.size), PluginConfig.proxy)
RequestBody(
r18, 1, listOf(), "", Utils.processTags(tags),
listOf(PluginConfig.size), PluginConfig.proxy
)
else RequestBody(r18, 1, listOf(), tags, listOf(), listOf(PluginConfig.size), PluginConfig.proxy)
Main.logger.info(body.toString())
val response: ResponseBody?
Expand All @@ -122,6 +124,10 @@ object Lolicon: CompositeCommand(
}
try {
val imageData = response.data[0]
if (!Utils.areTagsAllowed(imageData.tags)) {
sendMessage(ReplyConfig.filteredTag)
return
}
val url = imageData.urls[PluginConfig.size] ?: return
val imgInfoReceipt =
if (PluginConfig.verbose || subject == null) sendMessage(imageData.toReadable())
Expand Down Expand Up @@ -156,13 +162,14 @@ object Lolicon: CompositeCommand(
GlobalScope.launch {
Timer.cooldown(subject, cooldown)
withContext(Dispatchers.Default) {
Main.logger.info(imgReceipt.target.toString()+"命令已冷却")
Main.logger.info(imgReceipt.target.toString() + "命令已冷却")
}
}
}
}
} catch (e: Exception) {
Main.logger.error(e)
sendMessage(ReplyConfig.networkError)
} finally {
@Suppress("BlockingMethodInNonBlockingContext")
stream?.close()
Expand Down Expand Up @@ -263,6 +270,7 @@ object Lolicon: CompositeCommand(
imageInfoMsgBuilder.add("\n")
} catch (e: Exception) {
Main.logger.error(e)
sendMessage(ReplyConfig.networkError)
} finally {
@Suppress("BlockingMethodInNonBlockingContext")
stream?.close()
Expand All @@ -286,7 +294,7 @@ object Lolicon: CompositeCommand(
GlobalScope.launch {
Timer.cooldown(subject, cooldown)
withContext(Dispatchers.Default) {
Main.logger.info(imgReceipt.target.toString()+"命令已冷却")
Main.logger.info(imgReceipt.target.toString() + "命令已冷却")
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import java.net.Proxy
* @constructor Create a KotlinPlugin instance <br> 实例化插件
* @see net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
*/
object Main: KotlinPlugin(
object Main : KotlinPlugin(
JvmPluginDescription(
id = "com.github.samarium150.mirai-console-lolicon",
version = "4.2.0",
Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/PluginConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,20 @@ object PluginConfig : AutoSavePluginConfig("Config") {
*/
@ValueDescription("默认的冷却时间(单位:s)")
var cooldown: Int by value(60)

/**
* Tag filter mode
* <br>
* 标签过滤模式
*/
@ValueDescription("标签过滤模式: none/whitelist/blacklist")
val tagFilterMode: String by value("none")

/**
* Tag filter
* <br>
* 标签过滤器
*/
@ValueDescription("标签过滤器,白名单模式只发送包含指定标签的图片,黑名单模式只发送不包含指定标签的图片。(仅Get命令,Adv命令中不生效)")
val tagFilter: MutableSet<String> by value()
}
2 changes: 1 addition & 1 deletion src/main/kotlin/PluginData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import net.mamoe.mirai.console.data.value
* @constructor Create a plugin config instance <br> 实例化插件数据
* @see net.mamoe.mirai.console.data.AutoSavePluginData
*/
object PluginData: AutoSavePluginData("Data") {
object PluginData : AutoSavePluginData("Data") {

/**
* User set
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/ProxyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import net.mamoe.mirai.console.data.value
* @constructor Create empty Proxy config
* @see net.mamoe.mirai.console.data.AutoSavePluginConfig
*/
object ProxyConfig: AutoSavePluginConfig("ProxyConfig") {
object ProxyConfig : AutoSavePluginConfig("ProxyConfig") {

/**
* Type
Expand Down
10 changes: 9 additions & 1 deletion src/main/kotlin/ReplyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import net.mamoe.mirai.console.data.value
* @constructor Create a Reply config instance <br> 实例化回复配置
* @see net.mamoe.mirai.console.data.AutoSavePluginConfig
*/
object ReplyConfig: AutoSavePluginConfig("ReplyConfig") {
object ReplyConfig : AutoSavePluginConfig("ReplyConfig") {


/**
* API returns errors
Expand All @@ -38,6 +39,13 @@ object ReplyConfig: AutoSavePluginConfig("ReplyConfig") {
@ValueDescription("指令未冷却")
val inCooldown: String by value("你怎么冲得到处都是")

/**
* Tag is filtered out
* <br>
* 标签被过滤
*/
val filteredTag: String by value("该图片标签已被过滤,请换个标签再试")

/**
* Network error
* <br>
Expand Down
60 changes: 59 additions & 1 deletion src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,64 @@ object Utils {
return result.toList()
}

/**
* Is the given [tag] allowed
*
* @param tag
* @return
*/
private fun isTagAllowed(tag: String): Boolean {
return when (PluginConfig.tagFilterMode) {
"none" -> true
"whitelist" -> {
for (filter in PluginConfig.tagFilter) {
if (filter.toRegex(setOf(RegexOption.IGNORE_CASE)).matches(tag)) return true
}
false
}
"blacklist" -> {
for (filter in PluginConfig.tagFilter) {
if (filter.toRegex(setOf(RegexOption.IGNORE_CASE)).matches(tag)) return false
}
true
}
else -> true
}
}

/**
* Are these given [tags] allowed
*
* @param tags
* @return
*/
fun areTagsAllowed(tags: List<String>): Boolean {
return when (PluginConfig.tagFilterMode) {
"none" -> true
"whitelist" -> {
var flag = false
for (tag in tags) {
if (isTagAllowed(tag)) {
flag = true
break
}
}
flag
}
"blacklist" -> {
var flag = true
for (tag in tags) {
if (!isTagAllowed(tag)) {
flag = false
break
}
}
flag
}
else -> true
}
}

private val sizeMap: Map<String, Int> = mapOf(
"original" to 0,
"regular" to 1,
Expand All @@ -131,7 +189,7 @@ object Utils {
)

fun getUrl(urls: Map<String, String>): String? {
return urls[urls.keys.sortedBy { sizeMap[it] } [0]]
return urls[urls.keys.sortedBy { sizeMap[it] }[0]]
}

/**
Expand Down

0 comments on commit a44483c

Please sign in to comment.