Skip to content

Commit a44483c

Browse files
committed
feat: support tags filtering
Closes #55
1 parent 6b339d1 commit a44483c

File tree

9 files changed

+105
-15
lines changed

9 files changed

+105
-15
lines changed

src/main/kotlin/CommandConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import net.mamoe.mirai.console.data.value
1212
* @constructor Create a Command config instance <br> 实例化命令配置
1313
* @see net.mamoe.mirai.console.data.AutoSavePluginConfig
1414
*/
15-
object CommandConfig: AutoSavePluginConfig("CommandConfig") {
15+
object CommandConfig : AutoSavePluginConfig("CommandConfig") {
1616

1717
/**
1818
* Composite command secondary names

src/main/kotlin/ImageData.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ data class ImageData(
7070
fun toReadable(): String {
7171
return (
7272
"标题: ${title}\n" +
73-
"作者: $author (uid: ${uid})\n" +
74-
"标签: ${tags}\n" +
75-
"链接: https://pixiv.net/artworks/${pid}"
76-
)
73+
"作者: $author (uid: ${uid})\n" +
74+
"标签: ${tags}\n" +
75+
"链接: https://pixiv.net/artworks/${pid}"
76+
)
7777
}
7878
}

src/main/kotlin/Lolicon.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import java.net.URL
4242
* @constructor Create a CompositeCommand instance <br> 实例化命令
4343
* @see net.mamoe.mirai.console.command.CompositeCommand
4444
*/
45-
object Lolicon: CompositeCommand(
45+
object Lolicon : CompositeCommand(
4646
Main, primaryName = "lolicon",
4747
secondaryNames = CommandConfig.lolicon
4848
) {
@@ -99,8 +99,10 @@ object Lolicon: CompositeCommand(
9999
val (r18, recall, cooldown) = ExecutionConfig.create(subject)
100100
val tags = tagArgs.joinToString(" ")
101101
val body = if (tags.isNotEmpty())
102-
RequestBody(r18, 1, listOf(), "", Utils.processTags(tags),
103-
listOf(PluginConfig.size), PluginConfig.proxy)
102+
RequestBody(
103+
r18, 1, listOf(), "", Utils.processTags(tags),
104+
listOf(PluginConfig.size), PluginConfig.proxy
105+
)
104106
else RequestBody(r18, 1, listOf(), tags, listOf(), listOf(PluginConfig.size), PluginConfig.proxy)
105107
Main.logger.info(body.toString())
106108
val response: ResponseBody?
@@ -122,6 +124,10 @@ object Lolicon: CompositeCommand(
122124
}
123125
try {
124126
val imageData = response.data[0]
127+
if (!Utils.areTagsAllowed(imageData.tags)) {
128+
sendMessage(ReplyConfig.filteredTag)
129+
return
130+
}
125131
val url = imageData.urls[PluginConfig.size] ?: return
126132
val imgInfoReceipt =
127133
if (PluginConfig.verbose || subject == null) sendMessage(imageData.toReadable())
@@ -156,13 +162,14 @@ object Lolicon: CompositeCommand(
156162
GlobalScope.launch {
157163
Timer.cooldown(subject, cooldown)
158164
withContext(Dispatchers.Default) {
159-
Main.logger.info(imgReceipt.target.toString()+"命令已冷却")
165+
Main.logger.info(imgReceipt.target.toString() + "命令已冷却")
160166
}
161167
}
162168
}
163169
}
164170
} catch (e: Exception) {
165171
Main.logger.error(e)
172+
sendMessage(ReplyConfig.networkError)
166173
} finally {
167174
@Suppress("BlockingMethodInNonBlockingContext")
168175
stream?.close()
@@ -263,6 +270,7 @@ object Lolicon: CompositeCommand(
263270
imageInfoMsgBuilder.add("\n")
264271
} catch (e: Exception) {
265272
Main.logger.error(e)
273+
sendMessage(ReplyConfig.networkError)
266274
} finally {
267275
@Suppress("BlockingMethodInNonBlockingContext")
268276
stream?.close()
@@ -286,7 +294,7 @@ object Lolicon: CompositeCommand(
286294
GlobalScope.launch {
287295
Timer.cooldown(subject, cooldown)
288296
withContext(Dispatchers.Default) {
289-
Main.logger.info(imgReceipt.target.toString()+"命令已冷却")
297+
Main.logger.info(imgReceipt.target.toString() + "命令已冷却")
290298
}
291299
}
292300
}

src/main/kotlin/Main.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import java.net.Proxy
3838
* @constructor Create a KotlinPlugin instance <br> 实例化插件
3939
* @see net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
4040
*/
41-
object Main: KotlinPlugin(
41+
object Main : KotlinPlugin(
4242
JvmPluginDescription(
4343
id = "com.github.samarium150.mirai-console-lolicon",
4444
version = "4.2.0",

src/main/kotlin/PluginConfig.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,20 @@ object PluginConfig : AutoSavePluginConfig("Config") {
125125
*/
126126
@ValueDescription("默认的冷却时间(单位:s)")
127127
var cooldown: Int by value(60)
128+
129+
/**
130+
* Tag filter mode
131+
* <br>
132+
* 标签过滤模式
133+
*/
134+
@ValueDescription("标签过滤模式: none/whitelist/blacklist")
135+
val tagFilterMode: String by value("none")
136+
137+
/**
138+
* Tag filter
139+
* <br>
140+
* 标签过滤器
141+
*/
142+
@ValueDescription("标签过滤器,白名单模式只发送包含指定标签的图片,黑名单模式只发送不包含指定标签的图片。(仅Get命令,Adv命令中不生效)")
143+
val tagFilter: MutableSet<String> by value()
128144
}

src/main/kotlin/PluginData.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import net.mamoe.mirai.console.data.value
2828
* @constructor Create a plugin config instance <br> 实例化插件数据
2929
* @see net.mamoe.mirai.console.data.AutoSavePluginData
3030
*/
31-
object PluginData: AutoSavePluginData("Data") {
31+
object PluginData : AutoSavePluginData("Data") {
3232

3333
/**
3434
* User set

src/main/kotlin/ProxyConfig.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import net.mamoe.mirai.console.data.value
1212
* @constructor Create empty Proxy config
1313
* @see net.mamoe.mirai.console.data.AutoSavePluginConfig
1414
*/
15-
object ProxyConfig: AutoSavePluginConfig("ProxyConfig") {
15+
object ProxyConfig : AutoSavePluginConfig("ProxyConfig") {
1616

1717
/**
1818
* Type

src/main/kotlin/ReplyConfig.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import net.mamoe.mirai.console.data.value
1212
* @constructor Create a Reply config instance <br> 实例化回复配置
1313
* @see net.mamoe.mirai.console.data.AutoSavePluginConfig
1414
*/
15-
object ReplyConfig: AutoSavePluginConfig("ReplyConfig") {
15+
object ReplyConfig : AutoSavePluginConfig("ReplyConfig") {
16+
1617

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

42+
/**
43+
* Tag is filtered out
44+
* <br>
45+
* 标签被过滤
46+
*/
47+
val filteredTag: String by value("该图片标签已被过滤,请换个标签再试")
48+
4149
/**
4250
* Network error
4351
* <br>

src/main/kotlin/Utils.kt

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,64 @@ object Utils {
122122
return result.toList()
123123
}
124124

125+
/**
126+
* Is the given [tag] allowed
127+
*
128+
* @param tag
129+
* @return
130+
*/
131+
private fun isTagAllowed(tag: String): Boolean {
132+
return when (PluginConfig.tagFilterMode) {
133+
"none" -> true
134+
"whitelist" -> {
135+
for (filter in PluginConfig.tagFilter) {
136+
if (filter.toRegex(setOf(RegexOption.IGNORE_CASE)).matches(tag)) return true
137+
}
138+
false
139+
}
140+
"blacklist" -> {
141+
for (filter in PluginConfig.tagFilter) {
142+
if (filter.toRegex(setOf(RegexOption.IGNORE_CASE)).matches(tag)) return false
143+
}
144+
true
145+
}
146+
else -> true
147+
}
148+
}
149+
150+
/**
151+
* Are these given [tags] allowed
152+
*
153+
* @param tags
154+
* @return
155+
*/
156+
fun areTagsAllowed(tags: List<String>): Boolean {
157+
return when (PluginConfig.tagFilterMode) {
158+
"none" -> true
159+
"whitelist" -> {
160+
var flag = false
161+
for (tag in tags) {
162+
if (isTagAllowed(tag)) {
163+
flag = true
164+
break
165+
}
166+
}
167+
flag
168+
}
169+
"blacklist" -> {
170+
var flag = true
171+
for (tag in tags) {
172+
if (!isTagAllowed(tag)) {
173+
flag = false
174+
break
175+
}
176+
}
177+
flag
178+
}
179+
else -> true
180+
}
181+
}
182+
125183
private val sizeMap: Map<String, Int> = mapOf(
126184
"original" to 0,
127185
"regular" to 1,
@@ -131,7 +189,7 @@ object Utils {
131189
)
132190

133191
fun getUrl(urls: Map<String, String>): String? {
134-
return urls[urls.keys.sortedBy { sizeMap[it] } [0]]
192+
return urls[urls.keys.sortedBy { sizeMap[it] }[0]]
135193
}
136194

137195
/**

0 commit comments

Comments
 (0)