Skip to content

Commit

Permalink
feat: add white/blacklist and verbose mode
Browse files Browse the repository at this point in the history
Closes #47
Closes #50
  • Loading branch information
Samarium150 committed Sep 10, 2021
1 parent 6901fc4 commit 3567f16
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 58 deletions.
17 changes: 0 additions & 17 deletions .github/dependabot.yml

This file was deleted.

19 changes: 9 additions & 10 deletions Module.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# Module mirai-console-lolicon

[ACGPro](https://github.com/ShrBox/ACGPro) 启发而写
<br>

Social preview的图片来自 [AliceSoft](https://www.alicesoft.com) 制作的游戏 [ドーナドーナ いっしょにわるいことをしよう](https://www.alicesoft.com/dohnadohna)
<br>
在群内随机发送图片(默认30s自动撤回+60s冷却),支持关键词检索
<br>
适配 [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)
<br>
可以在 [Lolicon API](https://api.lolicon.app/#/setu) 申请apikey来增加调用额度
<br>
帮助文档已经移到了 [Wiki](https://github.com/Samarium150/mirai-console-lolicon/wiki) 页面
<br>

在群内随机发送来自 [Lolicon API v2](https://api.lolicon.app/#/setu) 的图片(默认30s自动撤回+60s冷却),支持标签检索和JSON高级检索

适配 [mirai-console](https://github.com/mamoe/mirai-console) v2.7.0

使用文档已经移到了 [Wiki](https://github.com/Samarium150/mirai-console-lolicon/wiki) 页面

本页面为Kotlin源文件的注释文档

# Package com.github.samarium150.mirai.plugin
Expand Down
5 changes: 2 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ plugins {
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion

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

group = "com.github.samarium150"
version = "4.0.0"
version = "4.1.0"

repositories {
mavenLocal()
Expand All @@ -31,7 +31,6 @@ tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().all {
tasks.withType<org.jetbrains.dokka.gradle.DokkaTask>().configureEach {
dokkaSourceSets {
configureEach {
outputDirectory.set(file("docs"))
includeNonPublic.set(true)
includes.from("Module.md")
sourceLink {
Expand Down
68 changes: 60 additions & 8 deletions src/main/kotlin/Lolicon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ object Lolicon: CompositeCommand(
*/
@OptIn(DelicateCoroutinesApi::class)
@SubCommand("get", "来一张")
@Description("(默认冷却时间60s)根据关键字发送涩图, 不提供关键字则随机发送一张")
@Description("根据标签发送涩图, 不提供则随机发送一张")
suspend fun CommandSender.get(tags: String = "") {
if (!Utils.isPermitted(subject)) {
val where = if (subject is Group) "@${(subject as Group).id}" else ""
Main.logger.info("当前模式为'${PluginConfig.mode}',${user?.id}${where}的命令已被无视")
return
}
if (!Timer.getCooldown(subject)) {
sendMessage(ReplyConfig.inCooldown)
return
Expand Down Expand Up @@ -117,8 +122,9 @@ object Lolicon: CompositeCommand(
try {
val imageData = response.data[0]
val url = imageData.urls[PluginConfig.size] ?: return
Main.logger.info("url: $url")
val imgInfoReceipt = sendMessage(imageData.toReadable()) ?: return
val imgInfoReceipt =
if (PluginConfig.verbose || subject == null) sendMessage(imageData.toReadable())
else null
var stream: InputStream? = null
try {
stream = if (PluginConfig.save && PluginConfig.cache) {
Expand Down Expand Up @@ -159,7 +165,7 @@ object Lolicon: CompositeCommand(
} finally {
@Suppress("BlockingMethodInNonBlockingContext")
stream?.close()
if (recall > 0 && PluginConfig.recallImgInfo)
if (PluginConfig.verbose && imgInfoReceipt != null && recall > 0 && PluginConfig.recallImgInfo)
GlobalScope.launch {
val result = imgInfoReceipt.recallIn((recall * 1000).toLong()).awaitIsSuccess()
withContext(Dispatchers.Default) {
Expand All @@ -173,12 +179,23 @@ object Lolicon: CompositeCommand(
}
}

/**
* Advanced get
* <br>
* 子命令adv,根据[json]获取图片
*
* @param json JSON字符串
*/
@Suppress("unused")
@OptIn(DelicateCoroutinesApi::class, kotlinx.serialization.ExperimentalSerializationApi::class)
@SubCommand("adv", "高级")
@Description("")
@Description("根据JSON字符串发送涩图")
suspend fun CommandSender.advanced(json: String) {
Main.logger.info(json)
if (!Utils.isPermitted(subject)) {
val where = if (subject is Group) "@${(subject as Group).id}" else ""
Main.logger.info("当前模式为'${PluginConfig.mode}',${user?.id}${where}的命令已被无视")
return
}
val (r18, recall, cooldown) = ExecutionConfig.create(subject)
val body: RequestBody?
try {
Expand Down Expand Up @@ -248,7 +265,9 @@ object Lolicon: CompositeCommand(
stream?.close()
}
}
val imgInfoReceipt = sendMessage(imageInfoMsgBuilder.asMessageChain()) ?: return
val imgInfoReceipt =
if (PluginConfig.verbose || subject == null) sendMessage(imageInfoMsgBuilder.asMessageChain())
else null
val imgReceipt = sendMessage(imageMsgBuilder.asMessageChain())
if (imgReceipt != null) {
if (recall > 0 && PluginConfig.recallImg)
Expand All @@ -269,7 +288,7 @@ object Lolicon: CompositeCommand(
}
}
}
if (recall > 0 && PluginConfig.recallImgInfo)
if (PluginConfig.verbose && imgInfoReceipt != null && recall > 0 && PluginConfig.recallImgInfo)
GlobalScope.launch {
val result = imgInfoReceipt.recallIn((recall * 1000).toLong()).awaitIsSuccess()
withContext(Dispatchers.Default) {
Expand Down Expand Up @@ -363,6 +382,39 @@ object Lolicon: CompositeCommand(
}
}

/**
* Add user id or group id to corresponding set
* <br>
* 添加用户id或群组id到对应的集合,用于黑白名单
*
* @param type
* @param id
*/
@Suppress("unused")
@SubCommand("add", "添加")
suspend fun CommandSender.add(type: String, id: Long) {
if (!Utils.checkMaster(user)) {
sendMessage(ReplyConfig.nonMasterPermissionDenied)
if (PluginConfig.master == 0L) sendMessage(ReplyConfig.noMasterID)
return
}
val success: Boolean = when (type) {
"user" -> {
PluginData.userSet.add(id)
true
}
"group" -> {
PluginData.groupSet.add(id)
true
}
else -> {
sendMessage("类型错误")
false
}
}
if (success) sendMessage("添加成功")
}

/**
* Subcommand trust, add user to trusted set
* <br>
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import java.net.Proxy
object Main: KotlinPlugin(
JvmPluginDescription(
id = "com.github.samarium150.mirai-console-lolicon",
version = "4.0.0",
version = "4.1.0",
name = "mirai-console-lolicon"
)
) {
Expand All @@ -66,6 +66,12 @@ object Main: KotlinPlugin(
ProxyConfig.reload()
PluginData.reload()

if (PluginConfig.master != 0L) {
PluginData.trustedUsers.add(PluginConfig.master)
PluginData.userSet.add(PluginConfig.master)
PluginData.reload()
} else logger.warning("请先在配置文件设置Bot所有者id")

client = HttpClient {
engine {
proxy = if (ProxyConfig.type != "DIRECT") Proxy(
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 @@ -38,6 +38,14 @@ object PluginConfig : AutoSavePluginConfig("Config") {
@ValueDescription("Bot所有者账号")
val master: Long by value()

/**
* mode for Get and adv command
* <br>
* Get命令和Adv命令的模式
*/
@ValueDescription("Get命令和Adv命令的模式:none/whitelist/blacklist")
val mode: String by value("none")

/**
* Enable image saving
* <br>
Expand All @@ -54,6 +62,14 @@ object PluginConfig : AutoSavePluginConfig("Config") {
@ValueDescription("是否使用已保存的图片作为缓存")
val cache: Boolean by value(false)

/**
* Sending image info
* <br>
* 是否发送图片信息
*/
@ValueDescription("是否发送图片信息")
val verbose: Boolean by value(true)

/**
* Enable flash image
* <br>
Expand Down
24 changes: 12 additions & 12 deletions src/main/kotlin/PluginData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@ import net.mamoe.mirai.console.data.value
object PluginData: AutoSavePluginData("Data") {

/**
* Trusted Users
* User set
* <br>
* 受信任的用户
* 用户列表
*/
@ValueDescription("受信任的用户")
val trustedUsers: MutableSet<Long> by value()
@ValueDescription("用户列表")
val userSet: MutableSet<Long> by value()

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

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

/**
* Users that enabled R18 option
Expand Down
34 changes: 31 additions & 3 deletions src/main/kotlin/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package com.github.samarium150.mirai.plugin

import com.github.samarium150.mirai.plugin.Lolicon.set
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.contact.MemberPermission
import net.mamoe.mirai.contact.User
import net.mamoe.mirai.contact.*
import org.jetbrains.annotations.Nullable
import java.net.Proxy

Expand Down Expand Up @@ -135,4 +133,34 @@ object Utils {
fun getUrl(urls: Map<String, String>): String? {
return urls[urls.keys.sortedBy { sizeMap[it] } [0]]
}

/**
* Is the subject permitted to use the bot
* <br>
* 是否能执行命令
*
* @param subject
* @return
*/
fun isPermitted(subject: Contact?): Boolean {
return when (PluginConfig.mode) {
"whitelist" -> {
when {
subject == null -> true
subject is User && PluginData.userSet.contains(subject.id) -> true
subject is Group && PluginData.groupSet.contains(subject.id) -> true
else -> false
}
}
"blacklist" -> {
when {
subject == null -> true
subject is User && !PluginData.userSet.contains(subject.id) -> true
subject is Group && !PluginData.groupSet.contains(subject.id) -> true
else -> true
}
}
else -> true
}
}
}
7 changes: 3 additions & 4 deletions src/main/resources/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

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

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

0 comments on commit 3567f16

Please sign in to comment.