Skip to content

Commit 3567f16

Browse files
committed
feat: add white/blacklist and verbose mode
Closes #47 Closes #50
1 parent 6901fc4 commit 3567f16

File tree

9 files changed

+140
-58
lines changed

9 files changed

+140
-58
lines changed

.github/dependabot.yml

Lines changed: 0 additions & 17 deletions
This file was deleted.

Module.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
# Module mirai-console-lolicon
2+
23
[ACGPro](https://github.com/ShrBox/ACGPro) 启发而写
3-
<br>
4+
45
Social preview的图片来自 [AliceSoft](https://www.alicesoft.com) 制作的游戏 [ドーナドーナ いっしょにわるいことをしよう](https://www.alicesoft.com/dohnadohna)
5-
<br>
6-
在群内随机发送图片(默认30s自动撤回+60s冷却),支持关键词检索
7-
<br>
8-
适配 [mirai-console](https://github.com/mamoe/mirai-console) [![Version](https://img.shields.io/badge/version-2.6.5-blue)](https://github.com/mamoe/mirai/releases/tag/v2.6.5)
9-
<br>
10-
可以在 [Lolicon API](https://api.lolicon.app/#/setu) 申请apikey来增加调用额度
11-
<br>
12-
帮助文档已经移到了 [Wiki](https://github.com/Samarium150/mirai-console-lolicon/wiki) 页面
13-
<br>
6+
7+
在群内随机发送来自 [Lolicon API v2](https://api.lolicon.app/#/setu) 的图片(默认30s自动撤回+60s冷却),支持标签检索和JSON高级检索
8+
9+
适配 [mirai-console](https://github.com/mamoe/mirai-console) v2.7.0
10+
11+
使用文档已经移到了 [Wiki](https://github.com/Samarium150/mirai-console-lolicon/wiki) 页面
12+
1413
本页面为Kotlin源文件的注释文档
1514

1615
# Package com.github.samarium150.mirai.plugin

build.gradle.kts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ plugins {
55
kotlin("jvm") version kotlinVersion
66
kotlin("plugin.serialization") version kotlinVersion
77

8-
id("net.mamoe.mirai-console") version "2.7.0"
8+
id("net.mamoe.mirai-console") version "2.7.1-dev-1"
99
id("org.jetbrains.dokka") version "1.5.0"
1010
id("com.geoffgranum.gradle-conventional-changelog") version "0.3.1"
1111
}
1212

1313
group = "com.github.samarium150"
14-
version = "4.0.0"
14+
version = "4.1.0"
1515

1616
repositories {
1717
mavenLocal()
@@ -31,7 +31,6 @@ tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().all {
3131
tasks.withType<org.jetbrains.dokka.gradle.DokkaTask>().configureEach {
3232
dokkaSourceSets {
3333
configureEach {
34-
outputDirectory.set(file("docs"))
3534
includeNonPublic.set(true)
3635
includes.from("Module.md")
3736
sourceLink {

src/main/kotlin/Lolicon.kt

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ object Lolicon: CompositeCommand(
8585
*/
8686
@OptIn(DelicateCoroutinesApi::class)
8787
@SubCommand("get", "来一张")
88-
@Description("(默认冷却时间60s)根据关键字发送涩图, 不提供关键字则随机发送一张")
88+
@Description("根据标签发送涩图, 不提供则随机发送一张")
8989
suspend fun CommandSender.get(tags: String = "") {
90+
if (!Utils.isPermitted(subject)) {
91+
val where = if (subject is Group) "@${(subject as Group).id}" else ""
92+
Main.logger.info("当前模式为'${PluginConfig.mode}',${user?.id}${where}的命令已被无视")
93+
return
94+
}
9095
if (!Timer.getCooldown(subject)) {
9196
sendMessage(ReplyConfig.inCooldown)
9297
return
@@ -117,8 +122,9 @@ object Lolicon: CompositeCommand(
117122
try {
118123
val imageData = response.data[0]
119124
val url = imageData.urls[PluginConfig.size] ?: return
120-
Main.logger.info("url: $url")
121-
val imgInfoReceipt = sendMessage(imageData.toReadable()) ?: return
125+
val imgInfoReceipt =
126+
if (PluginConfig.verbose || subject == null) sendMessage(imageData.toReadable())
127+
else null
122128
var stream: InputStream? = null
123129
try {
124130
stream = if (PluginConfig.save && PluginConfig.cache) {
@@ -159,7 +165,7 @@ object Lolicon: CompositeCommand(
159165
} finally {
160166
@Suppress("BlockingMethodInNonBlockingContext")
161167
stream?.close()
162-
if (recall > 0 && PluginConfig.recallImgInfo)
168+
if (PluginConfig.verbose && imgInfoReceipt != null && recall > 0 && PluginConfig.recallImgInfo)
163169
GlobalScope.launch {
164170
val result = imgInfoReceipt.recallIn((recall * 1000).toLong()).awaitIsSuccess()
165171
withContext(Dispatchers.Default) {
@@ -173,12 +179,23 @@ object Lolicon: CompositeCommand(
173179
}
174180
}
175181

182+
/**
183+
* Advanced get
184+
* <br>
185+
* 子命令adv,根据[json]获取图片
186+
*
187+
* @param json JSON字符串
188+
*/
176189
@Suppress("unused")
177190
@OptIn(DelicateCoroutinesApi::class, kotlinx.serialization.ExperimentalSerializationApi::class)
178191
@SubCommand("adv", "高级")
179-
@Description("")
192+
@Description("根据JSON字符串发送涩图")
180193
suspend fun CommandSender.advanced(json: String) {
181-
Main.logger.info(json)
194+
if (!Utils.isPermitted(subject)) {
195+
val where = if (subject is Group) "@${(subject as Group).id}" else ""
196+
Main.logger.info("当前模式为'${PluginConfig.mode}',${user?.id}${where}的命令已被无视")
197+
return
198+
}
182199
val (r18, recall, cooldown) = ExecutionConfig.create(subject)
183200
val body: RequestBody?
184201
try {
@@ -248,7 +265,9 @@ object Lolicon: CompositeCommand(
248265
stream?.close()
249266
}
250267
}
251-
val imgInfoReceipt = sendMessage(imageInfoMsgBuilder.asMessageChain()) ?: return
268+
val imgInfoReceipt =
269+
if (PluginConfig.verbose || subject == null) sendMessage(imageInfoMsgBuilder.asMessageChain())
270+
else null
252271
val imgReceipt = sendMessage(imageMsgBuilder.asMessageChain())
253272
if (imgReceipt != null) {
254273
if (recall > 0 && PluginConfig.recallImg)
@@ -269,7 +288,7 @@ object Lolicon: CompositeCommand(
269288
}
270289
}
271290
}
272-
if (recall > 0 && PluginConfig.recallImgInfo)
291+
if (PluginConfig.verbose && imgInfoReceipt != null && recall > 0 && PluginConfig.recallImgInfo)
273292
GlobalScope.launch {
274293
val result = imgInfoReceipt.recallIn((recall * 1000).toLong()).awaitIsSuccess()
275294
withContext(Dispatchers.Default) {
@@ -363,6 +382,39 @@ object Lolicon: CompositeCommand(
363382
}
364383
}
365384

385+
/**
386+
* Add user id or group id to corresponding set
387+
* <br>
388+
* 添加用户id或群组id到对应的集合,用于黑白名单
389+
*
390+
* @param type
391+
* @param id
392+
*/
393+
@Suppress("unused")
394+
@SubCommand("add", "添加")
395+
suspend fun CommandSender.add(type: String, id: Long) {
396+
if (!Utils.checkMaster(user)) {
397+
sendMessage(ReplyConfig.nonMasterPermissionDenied)
398+
if (PluginConfig.master == 0L) sendMessage(ReplyConfig.noMasterID)
399+
return
400+
}
401+
val success: Boolean = when (type) {
402+
"user" -> {
403+
PluginData.userSet.add(id)
404+
true
405+
}
406+
"group" -> {
407+
PluginData.groupSet.add(id)
408+
true
409+
}
410+
else -> {
411+
sendMessage("类型错误")
412+
false
413+
}
414+
}
415+
if (success) sendMessage("添加成功")
416+
}
417+
366418
/**
367419
* Subcommand trust, add user to trusted set
368420
* <br>

src/main/kotlin/Main.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import java.net.Proxy
4141
object Main: KotlinPlugin(
4242
JvmPluginDescription(
4343
id = "com.github.samarium150.mirai-console-lolicon",
44-
version = "4.0.0",
44+
version = "4.1.0",
4545
name = "mirai-console-lolicon"
4646
)
4747
) {
@@ -66,6 +66,12 @@ object Main: KotlinPlugin(
6666
ProxyConfig.reload()
6767
PluginData.reload()
6868

69+
if (PluginConfig.master != 0L) {
70+
PluginData.trustedUsers.add(PluginConfig.master)
71+
PluginData.userSet.add(PluginConfig.master)
72+
PluginData.reload()
73+
} else logger.warning("请先在配置文件设置Bot所有者id")
74+
6975
client = HttpClient {
7076
engine {
7177
proxy = if (ProxyConfig.type != "DIRECT") Proxy(

src/main/kotlin/PluginConfig.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ object PluginConfig : AutoSavePluginConfig("Config") {
3838
@ValueDescription("Bot所有者账号")
3939
val master: Long by value()
4040

41+
/**
42+
* mode for Get and adv command
43+
* <br>
44+
* Get命令和Adv命令的模式
45+
*/
46+
@ValueDescription("Get命令和Adv命令的模式:none/whitelist/blacklist")
47+
val mode: String by value("none")
48+
4149
/**
4250
* Enable image saving
4351
* <br>
@@ -54,6 +62,14 @@ object PluginConfig : AutoSavePluginConfig("Config") {
5462
@ValueDescription("是否使用已保存的图片作为缓存")
5563
val cache: Boolean by value(false)
5664

65+
/**
66+
* Sending image info
67+
* <br>
68+
* 是否发送图片信息
69+
*/
70+
@ValueDescription("是否发送图片信息")
71+
val verbose: Boolean by value(true)
72+
5773
/**
5874
* Enable flash image
5975
* <br>

src/main/kotlin/PluginData.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,28 @@ import net.mamoe.mirai.console.data.value
3131
object PluginData: AutoSavePluginData("Data") {
3232

3333
/**
34-
* Trusted Users
34+
* User set
3535
* <br>
36-
* 受信任的用户
36+
* 用户列表
3737
*/
38-
@ValueDescription("受信任的用户")
39-
val trustedUsers: MutableSet<Long> by value()
38+
@ValueDescription("用户列表")
39+
val userSet: MutableSet<Long> by value()
4040

4141
/**
42-
* Users' id and their apikey mappings
42+
* Group set
4343
* <br>
44-
* 自定义了 apikey 的用户和对应的 apikey
44+
* 群组列表
4545
*/
46-
@ValueDescription("自定义了apikey的用户和对应的apikey")
47-
val customAPIKeyUsers: MutableMap<Long, String> by value()
46+
@ValueDescription("群组列表")
47+
val groupSet: MutableSet<Long> by value()
4848

4949
/**
50-
* Groups' id and their apikey mappings
50+
* Trusted Users
5151
* <br>
52-
* 自定义了 apikey 的群和对应的 apikey
52+
* 受信任的用户
5353
*/
54-
@ValueDescription("自定义了apikey的群和对应的apikey")
55-
val customAPIKeyGroups: MutableMap<Long, String> by value()
54+
@ValueDescription("受信任的用户")
55+
val trustedUsers: MutableSet<Long> by value()
5656

5757
/**
5858
* Users that enabled R18 option

src/main/kotlin/Utils.kt

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
package com.github.samarium150.mirai.plugin
1818

1919
import com.github.samarium150.mirai.plugin.Lolicon.set
20-
import net.mamoe.mirai.contact.Member
21-
import net.mamoe.mirai.contact.MemberPermission
22-
import net.mamoe.mirai.contact.User
20+
import net.mamoe.mirai.contact.*
2321
import org.jetbrains.annotations.Nullable
2422
import java.net.Proxy
2523

@@ -135,4 +133,34 @@ object Utils {
135133
fun getUrl(urls: Map<String, String>): String? {
136134
return urls[urls.keys.sortedBy { sizeMap[it] } [0]]
137135
}
136+
137+
/**
138+
* Is the subject permitted to use the bot
139+
* <br>
140+
* 是否能执行命令
141+
*
142+
* @param subject
143+
* @return
144+
*/
145+
fun isPermitted(subject: Contact?): Boolean {
146+
return when (PluginConfig.mode) {
147+
"whitelist" -> {
148+
when {
149+
subject == null -> true
150+
subject is User && PluginData.userSet.contains(subject.id) -> true
151+
subject is Group && PluginData.groupSet.contains(subject.id) -> true
152+
else -> false
153+
}
154+
}
155+
"blacklist" -> {
156+
when {
157+
subject == null -> true
158+
subject is User && !PluginData.userSet.contains(subject.id) -> true
159+
subject is Group && !PluginData.groupSet.contains(subject.id) -> true
160+
else -> true
161+
}
162+
}
163+
else -> true
164+
}
165+
}
138166
}

src/main/resources/help.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
简介:
44
在私聊/群内随机发送图片, 支持关键词检索
5-
apikey可以在<https://api.lolicon.app/#/setu?id=apikey>申请
65

76
可用指令:
87
/lolicon help # 获取帮助信息
9-
/lolicon get [keyword] # (冷却时间60s)根据关键字发送涩图, 不提供关键字则随机发送一张
8+
/lolicon get [tag] # 根据标签发送涩图, 不提供关键字则随机发送一张
9+
/lolicon adv <json> # 根据JSON获取涩图
10+
/lolicon add <type="group"|"user"> <id> # 将群或用户添加到列表(黑白名单用)
1011
/lolicon set <property> <value>
1112
# 设置属性, 群聊模式仅能由群主和管理员设置
1213
# 可选属性:
13-
# apikey, 对应值: default/正确的apikey, 效果: 重置apikey或设置apikey的值,私聊模式也可以更改
14-
# apikey设置后, 调用get时将使用设置的值, 而不是bot的默认值
1514
# r18, 对应值: 0/1/2, 效果: 将模式设置为non-R18/R18/mixed
1615
# 私聊模式仅限受信任清单中的用户设置
1716
# recall, 对应值: 以秒为单位的小于120的非负整数, 效果: 设置自动撤回的时间, 默认为30s, 0则不撤回

0 commit comments

Comments
 (0)