Skip to content

Commit df6e1b3

Browse files
committed
feat: implement the functionality of cool down
The get command now has a 60s cool down
1 parent e87033e commit df6e1b3

File tree

9 files changed

+136
-23
lines changed

9 files changed

+136
-23
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<br>
66
受启发 [ACGPro](https://github.com/ShrBox/ACGPro) 而写
77
<br>
8-
在群内随机发送图片(30s自动撤回),支持关键词检索
8+
在群内随机发送图片(30s自动撤回+60s冷却),支持关键词检索
99
<br>
1010
适配Mirai-console ![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/mamoe/mirai-console)
1111
<br>
@@ -30,7 +30,7 @@ customAPIKeyGroups: {}
3030
## Usage
3131
```text
3232
/lolicon help # 获取帮助信息
33-
/lolicon get [keyword] # 根据关键字发送涩图, 不提供关键字则随机发送一张
33+
/lolicon get [keyword] # (冷却时间60s)根据关键字发送涩图, 不提供关键字则随机发送一张
3434
/lolicon set <property> <value>
3535
# 设置属性
3636
# 可选属性:

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
}
88

99
group = "com.github.samarium150"
10-
version = "0.1.0"
10+
version = "1.0"
1111

1212
repositories {
1313
mavenLocal()

src/main/kotlin/ImageData.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ data class ImageData (
3030
val tags: ArrayList<String>
3131
) {
3232
/**
33-
* Returns the string representation
33+
* Return the string representation
3434
*
3535
* @return [String] Representation of the data class
3636
*/
@@ -44,7 +44,7 @@ data class ImageData (
4444
}
4545

4646
/**
47-
* Returns the readable information
47+
* Return the readable information
4848
*
4949
* @return [String] Useful and readable information for users
5050
*/

src/main/kotlin/Lolicon.kt

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,15 @@ object Lolicon: CompositeCommand(
5757
* @param keyword [String]
5858
*/
5959
@SubCommand("get")
60-
@Description("根据关键字发送涩图, 不提供关键字则随机发送一张")
60+
@Description("(冷却时间60s)根据关键字发送涩图, 不提供关键字则随机发送一张")
6161
suspend fun CommandSender.get(keyword: String = "") {
62-
val r18 = if (this.subject is User) false else PluginData.r18Groups.contains(this.subject?.id)
62+
if (!Timer.getCoolDown(subject)) {
63+
sendMessage("你怎么冲得到处都是")
64+
return
65+
}
66+
val r18 = if (subject is User) false else PluginData.r18Groups.contains(subject?.id)
6367
val key: String
64-
val id = this.subject?.id
68+
val id = subject?.id
6569
key = if (id == null) Config.key
6670
else if (this.subject is User && PluginData.customAPIKeyUsers.contains(id))
6771
PluginData.customAPIKeyUsers.getValue(id)
@@ -74,14 +78,21 @@ object Lolicon: CompositeCommand(
7478
val response: Response = RequestHandler.get(request)
7579
Main.logger.info(response.toReadable())
7680
for (imageData in response.data) {
77-
val receipt = this.subject?.sendImage(RequestHandler.download(imageData.url))
81+
val receipt = subject?.sendImage(RequestHandler.download(imageData.url))
7882
sendMessage(imageData.toReadable())
7983
if (receipt != null) {
84+
Timer.setCoolDown(subject)
8085
coroutineScope {
8186
val result = recallAsync(receipt).await()
8287
withContext(Dispatchers.Default) {
83-
if (!result) Main.logger.warning("撤回失败")
84-
else Main.logger.info("图片已撤回")
88+
if (!result) Main.logger.warning(receipt.target.toString()+"撤回失败")
89+
else Main.logger.info(receipt.target.toString()+"图片已撤回")
90+
}
91+
}
92+
coroutineScope {
93+
Timer.coolingDown(subject)
94+
withContext(Dispatchers.Default) {
95+
Main.logger.info(receipt.target.toString()+"命令已冷却")
8596
}
8697
}
8798
}
@@ -103,25 +114,25 @@ object Lolicon: CompositeCommand(
103114
suspend fun CommandSender.set(property: String, value: String) {
104115
when(property) {
105116
"r18" -> {
106-
if (this.subject is Group) {
117+
if (subject is Group) {
107118
if (value.toBoolean()) {
108-
PluginData.r18Groups.add((this.subject as Group).id)
119+
PluginData.r18Groups.add((subject as Group).id)
109120
sendMessage("设置成功")
110121
} else {
111-
if (PluginData.r18Groups.remove((this.subject as Group).id))
122+
if (PluginData.r18Groups.remove((subject as Group).id))
112123
sendMessage("设置成功")
113124
}
114125
}
115126
}
116127
"apikey" -> {
117-
when (this.subject) {
128+
when (subject) {
118129
is User -> {
119-
val id = (this.subject as User).id
130+
val id = (subject as User).id
120131
if (value.toLowerCase() == "default") PluginData.customAPIKeyUsers.remove(id)
121132
else PluginData.customAPIKeyUsers[id] = value
122133
}
123134
is Group -> {
124-
val id = (this.subject as Group).id
135+
val id = (subject as Group).id
125136
if (value.toLowerCase() == "default") PluginData.customAPIKeyGroups.remove(id)
126137
else PluginData.customAPIKeyGroups[id] = value
127138
}

src/main/kotlin/Request.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ data class Request(
2222
val size1200: Boolean = true
2323
) {
2424
/**
25-
* Returns the string representation of parameters
25+
* Return the string representation of parameters
2626
* for GET request
2727
*
2828
* @return [String] Request parameters
@@ -38,7 +38,7 @@ data class Request(
3838
}
3939

4040
/**
41-
* Returns the readable information
41+
* Return the readable information
4242
*
4343
* @return [String] Useful and readable information for logging
4444
*/

src/main/kotlin/RequestHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object RequestHandler {
2020
/**
2121
* The lambda function for making GET request
2222
*/
23-
private val getResponse: (String) -> (ResponseResultOf<String>) = { url -> Fuel.get(url).responseString() }
23+
private val getResponse: (String) -> ResponseResultOf<String> = { url -> Fuel.get(url).responseString() }
2424

2525
/**
2626
* Makes a GET request to Lolicon API

src/main/kotlin/Response.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ data class Response(
2222
val data: ArrayList<ImageData>
2323
) {
2424
/**
25-
* Returns the string representation of response body
25+
* Return the string representation of response body
2626
*
2727
* @return [String] Request parameters
2828
*/
@@ -36,7 +36,7 @@ data class Response(
3636
}
3737

3838
/**
39-
* Returns the readable information
39+
* Return the readable information
4040
*
4141
* @return [String] Useful and readable information for logging
4242
*/

src/main/kotlin/Timer.kt

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.github.samarium150.mirai.plugin
2+
3+
import kotlinx.coroutines.*
4+
import net.mamoe.mirai.contact.Contact
5+
import net.mamoe.mirai.contact.Group
6+
import net.mamoe.mirai.contact.User
7+
8+
/**
9+
* Object for handling cooling down feature
10+
*/
11+
object Timer {
12+
13+
/**
14+
* CoolDown map for users
15+
*/
16+
private val userCoolDown = mutableMapOf<Long, Boolean>()
17+
18+
/**
19+
* CoolDown map for groups
20+
*/
21+
private val groupCoolDown = mutableMapOf<Long, Boolean>()
22+
23+
/**
24+
* The lambda function for getting user's cool down status
25+
*/
26+
private val getUserCoolDown: (Long) -> Boolean = { key -> userCoolDown.getOrDefault(key, true) }
27+
28+
/**
29+
* The lambda function for getting group's cool down status
30+
*/
31+
private val getGroupCoolDown: (Long) -> Boolean = { key -> groupCoolDown.getOrDefault(key, true) }
32+
33+
/**
34+
* The lambda function for setting user's cool down status
35+
*/
36+
private val setUserCoolDown: (Long, Boolean) -> Unit = { key, value -> userCoolDown[key] = value }
37+
38+
/**
39+
* The lambda function for setting group's cool down status
40+
*/
41+
private val setGroupCoolDown: (Long, Boolean) -> Unit = { key, value -> groupCoolDown[key] = value }
42+
43+
/**
44+
* Asynchronously cool down for users
45+
*
46+
* @param key [Long] User's id
47+
* @return [Deferred]<[Unit]>
48+
*/
49+
private suspend fun userCoolingDownAsync(key: Long) = GlobalScope.async(start = CoroutineStart.LAZY) {
50+
delay(60000L)
51+
setUserCoolDown(key, true)
52+
}
53+
54+
/**
55+
* Asynchronously cool down for groups
56+
*
57+
* @param key [Long] Group's id
58+
* @return [Deferred]<[Unit]>
59+
*/
60+
private suspend fun groupCoolingDownAsync(key: Long) = GlobalScope.async(start = CoroutineStart.LAZY) {
61+
delay(60000L)
62+
setGroupCoolDown(key, true)
63+
}
64+
65+
/**
66+
* Get [subject]'s cool down status
67+
*
68+
* @param subject [Contact]?
69+
* @return [Boolean]
70+
*/
71+
fun getCoolDown(subject: Contact?): Boolean {
72+
return when (subject) {
73+
is User -> getUserCoolDown(subject.id)
74+
is Group -> getGroupCoolDown(subject.id)
75+
else -> true
76+
}
77+
}
78+
79+
/**
80+
* Set [subject]'s cool down status
81+
*
82+
* @param subject [Contact]?
83+
*/
84+
fun setCoolDown(subject: Contact?) {
85+
when (subject) {
86+
is User -> setUserCoolDown(subject.id, false)
87+
is Group -> setGroupCoolDown(subject.id, false)
88+
}
89+
}
90+
91+
/**
92+
* Asynchronously cool down
93+
*
94+
* @param subject [Contact]?
95+
*/
96+
suspend fun coolingDown(subject: Contact?) {
97+
when (subject) {
98+
is User -> userCoolingDownAsync(subject.id).await()
99+
is Group -> groupCoolingDownAsync(subject.id).await()
100+
}
101+
}
102+
}

src/main/resources/help.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ apikey可以在<https://api.lolicon.app/#/setu?id=apikey>申请
66

77
可用指令:
88
/lolicon help # 获取帮助信息
9-
/lolicon get [keyword] # 根据关键字发送涩图, 不提供关键字则随机发送一张
9+
/lolicon get [keyword] # (冷却时间60s)根据关键字发送涩图, 不提供关键字则随机发送一张
1010
/lolicon set <property> <value>
1111
# 设置属性
1212
# 可选属性:

0 commit comments

Comments
 (0)