Skip to content

Commit 7d602d4

Browse files
committed
fix: resolve dead locks
Closes #67
1 parent 02c3135 commit 7d602d4

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

build.gradle.kts

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

1515
group = "io.github.samarium150"
16-
version = "5.0.0-beta.3"
16+
version = "5.0.0-beta.4"
1717

1818
repositories {
1919
mavenLocal()

src/main/kotlin/io/github/samarium150/mirai/plugin/MiraiConsoleLolicon.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import java.net.Proxy
4747
object MiraiConsoleLolicon : KotlinPlugin(
4848
JvmPluginDescription(
4949
id = "io.github.samarium150.mirai.plugin.mirai-console-lolicon",
50-
version = "5.0.0-beta.3",
50+
version = "5.0.0-beta.4",
5151
name = "Lolicon"
5252
) {
5353
author("Samarium150")

src/main/kotlin/io/github/samarium150/mirai/plugin/command/Lolicon.kt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import io.github.samarium150.mirai.plugin.config.PluginConfig
2323
import io.github.samarium150.mirai.plugin.config.ReplyConfig
2424
import io.github.samarium150.mirai.plugin.data.PluginData
2525
import io.github.samarium150.mirai.plugin.data.RequestBody
26-
import io.github.samarium150.mirai.plugin.data.ResponseBody
2726
import io.github.samarium150.mirai.plugin.util.CooldownUtil
2827
import io.github.samarium150.mirai.plugin.util.ThrottleUtil
2928
import io.github.samarium150.mirai.plugin.util.GeneralUtil
@@ -97,14 +96,22 @@ object Lolicon : CompositeCommand(
9796
)
9897
else RequestBody(r18, 1, listOf(), tags, listOf(), listOf(PluginConfig.size), PluginConfig.proxy)
9998
logger.info("request body: $body")
100-
val response = GeneralUtil.processRequest(this, body) ?: return
99+
val response = GeneralUtil.processRequest(this, body)
100+
if (response == null) {
101+
ThrottleUtil.unlock(subject)
102+
return
103+
}
101104
try {
102105
val imageData = response.data[0]
103106
if (!GeneralUtil.areTagsAllowed(imageData.tags)) {
104107
sendMessage(ReplyConfig.filteredTag)
105108
return
106109
}
107-
val url = imageData.urls[PluginConfig.size] ?: return
110+
val url = imageData.urls[PluginConfig.size]
111+
if (url == null) {
112+
ThrottleUtil.unlock(subject)
113+
return
114+
}
108115
val imgInfoReceipt =
109116
if (PluginConfig.verbose || subject == null) sendMessage(imageData.toReadable())
110117
else null
@@ -113,10 +120,11 @@ object Lolicon : CompositeCommand(
113120
stream = GeneralUtil.getImageInputStream(url)
114121
val img = subject?.uploadImage(stream)
115122
if (img != null) {
116-
val imgReceipt = (
117-
if (PluginConfig.flash) sendMessage(FlashImage(img)) else sendMessage(img)
118-
) ?: return
119-
if (recall > 0 && PluginConfig.recallImg)
123+
val imgReceipt = if (PluginConfig.flash) sendMessage(FlashImage(img)) else sendMessage(img)
124+
if (imgReceipt == null) {
125+
ThrottleUtil.unlock(subject)
126+
return
127+
} else if (recall > 0 && PluginConfig.recallImg)
120128
GeneralUtil.recall(GeneralUtil.RecallType.IMAGE, imgReceipt, recall)
121129
if (cooldown > 0)
122130
CooldownUtil.cooldown(subject, cooldown)
@@ -158,6 +166,7 @@ object Lolicon : CompositeCommand(
158166
try {
159167
body = Json.decodeFromString<RequestBody>(json)
160168
} catch (e: Exception) {
169+
ThrottleUtil.unlock(subject)
161170
sendMessage(ReplyConfig.invalidJson)
162171
logger.warning(e)
163172
return
@@ -166,14 +175,20 @@ object Lolicon : CompositeCommand(
166175
if (body.r18 != r18) {
167176
if (subject is Group && !GeneralUtil.checkMemberPerm(user)) {
168177
sendMessage(ReplyConfig.nonAdminPermissionDenied)
178+
ThrottleUtil.unlock(subject)
169179
return
170180
}
171181
if (subject is User && !GeneralUtil.checkUserPerm(user)) {
172182
sendMessage(ReplyConfig.untrusted)
183+
ThrottleUtil.unlock(subject)
173184
return
174185
}
175186
}
176-
val response: ResponseBody = GeneralUtil.processRequest(this, body) ?: return
187+
val response = GeneralUtil.processRequest(this, body)
188+
if (response == null) {
189+
ThrottleUtil.unlock(subject)
190+
return
191+
}
177192
try {
178193
val imageInfoMsgBuilder = MessageChainBuilder()
179194
val imageMsgBuilder = MessageChainBuilder()
@@ -202,8 +217,11 @@ object Lolicon : CompositeCommand(
202217
val imgInfoReceipt =
203218
if (PluginConfig.verbose || subject == null) sendMessage(imageInfoMsgBuilder.asMessageChain())
204219
else null
205-
val imgReceipt = sendMessage(imageMsgBuilder.asMessageChain()) ?: return
206-
if (recall > 0 && PluginConfig.recallImg)
220+
val imgReceipt = sendMessage(imageMsgBuilder.asMessageChain())
221+
if (imgReceipt == null) {
222+
ThrottleUtil.unlock(subject)
223+
return
224+
} else if (recall > 0 && PluginConfig.recallImg)
207225
GeneralUtil.recall(GeneralUtil.RecallType.IMAGE, imgReceipt, recall)
208226
if (cooldown > 0)
209227
CooldownUtil.cooldown(subject, cooldown)

0 commit comments

Comments
 (0)