Skip to content

Commit 834cc21

Browse files
committed
feat: update to mirai-console 2.0-M2
1 parent df6e1b3 commit 834cc21

File tree

5 files changed

+36
-31
lines changed

5 files changed

+36
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<br>
88
在群内随机发送图片(30s自动撤回+60s冷却),支持关键词检索
99
<br>
10-
适配Mirai-console ![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/mamoe/mirai-console)
10+
适配Mirai-console ![GitHub tag](https://img.shields.io/badge/version-2.0--M2-blue)
1111
<br>
1212
可以在 [Lolicon API](https://api.lolicon.app/#/setu) 申请apikey来增加调用额度
1313
## Configuration and Plugin Data

build.gradle.kts

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

6-
id("net.mamoe.mirai-console") version "2.0-M1-1"
6+
id("net.mamoe.mirai-console") version "2.0-M2"
77
}
88

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

1212
repositories {
1313
mavenLocal()

src/main/kotlin/Lolicon.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.github.samarium150.mirai.plugin
22

33
import kotlinx.coroutines.*
4+
import kotlinx.io.errors.IOException
45
import net.mamoe.mirai.Mirai
56
import net.mamoe.mirai.console.command.CommandSender
67
import net.mamoe.mirai.console.command.CompositeCommand
78
import net.mamoe.mirai.contact.Contact
9+
import net.mamoe.mirai.contact.Contact.Companion.sendImage
810
import net.mamoe.mirai.contact.Group
911
import net.mamoe.mirai.contact.User
1012
import net.mamoe.mirai.message.MessageReceipt
11-
import net.mamoe.mirai.utils.sendImage
1213
import java.net.URL
1314

1415
/**
@@ -78,7 +79,8 @@ object Lolicon: CompositeCommand(
7879
val response: Response = RequestHandler.get(request)
7980
Main.logger.info(response.toReadable())
8081
for (imageData in response.data) {
81-
val receipt = subject?.sendImage(RequestHandler.download(imageData.url))
82+
val stream = RequestHandler.download(imageData.url)
83+
val receipt = subject?.sendImage(stream)
8284
sendMessage(imageData.toReadable())
8385
if (receipt != null) {
8486
Timer.setCoolDown(subject)
@@ -96,6 +98,8 @@ object Lolicon: CompositeCommand(
9698
}
9799
}
98100
}
101+
@Suppress("BlockingMethodInNonBlockingContext")
102+
withContext(Dispatchers.IO) { try { stream.close() } catch (e: IOException) {} }
99103
}
100104
} catch (e: Exception) {
101105
Main.logger.error(e)

src/main/kotlin/Main.kt

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
1717
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
1818
import net.mamoe.mirai.event.Listener
1919
import net.mamoe.mirai.event.events.MessageEvent
20+
import net.mamoe.mirai.event.globalEventChannel
2021
import net.mamoe.mirai.event.subscribeAlways
2122
import net.mamoe.mirai.utils.info
2223

@@ -50,35 +51,35 @@ object Main: KotlinPlugin(
5051
/**
5152
* Subscribe events
5253
*/
53-
commandListener = subscribeAlways(
54-
coroutineContext = CoroutineExceptionHandler { _, throwable ->
54+
commandListener =
55+
globalEventChannel().subscribeAlways(MessageEvent::class, CoroutineExceptionHandler { _, throwable ->
5556
logger.error(throwable)
5657
},
57-
concurrency = Listener.ConcurrencyKind.CONCURRENT,
58-
priority = Listener.EventPriority.NORMAL
59-
) call@{
60-
if (!enabled) return@call
61-
val sender = this.toCommandSender()
62-
when (val result = CommandManager.executeCommand(sender, message)) {
63-
is CommandExecuteResult.IllegalArgument -> {
64-
result.exception.message?.let { sender.sendMessage(it) }
65-
// intercept()
66-
}
67-
is CommandExecuteResult.ExecutionFailed -> {
68-
val owner = result.command.owner
69-
val (logger, printOwner) = when (owner) {
70-
is JvmPlugin -> owner.logger to false
71-
else -> MiraiConsole.mainLogger to true
58+
Listener.ConcurrencyKind.CONCURRENT,
59+
Listener.EventPriority.NORMAL
60+
) call@ {
61+
if (!enabled) return@call
62+
val sender = this.toCommandSender()
63+
when (val result = CommandManager.executeCommand(sender, message)) {
64+
is CommandExecuteResult.IllegalArgument -> {
65+
result.exception.message?.let { sender.sendMessage(it) }
66+
// intercept()
67+
}
68+
is CommandExecuteResult.ExecutionFailed -> {
69+
val owner = result.command.owner
70+
val (logger, printOwner) = when (owner) {
71+
is JvmPlugin -> owner.logger to false
72+
else -> MiraiConsole.mainLogger to true
73+
}
74+
logger.warning(
75+
"Exception in executing command `$message`" +
76+
if (printOwner) ", command owned by $owner" else "",
77+
result.exception
78+
)
79+
// intercept()
7280
}
73-
logger.warning(
74-
"Exception in executing command `$message`" +
75-
if (printOwner) ", command owned by $owner" else "",
76-
result.exception
77-
)
78-
// intercept()
7981
}
8082
}
81-
}
8283

8384
/**
8485
* Register commands

src/main/kotlin/Timer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ object Timer {
4444
* Asynchronously cool down for users
4545
*
4646
* @param key [Long] User's id
47-
* @return [Deferred]<[Unit]>
47+
* @return [Deferred]
4848
*/
4949
private suspend fun userCoolingDownAsync(key: Long) = GlobalScope.async(start = CoroutineStart.LAZY) {
5050
delay(60000L)
@@ -55,7 +55,7 @@ object Timer {
5555
* Asynchronously cool down for groups
5656
*
5757
* @param key [Long] Group's id
58-
* @return [Deferred]<[Unit]>
58+
* @return [Deferred]
5959
*/
6060
private suspend fun groupCoolingDownAsync(key: Long) = GlobalScope.async(start = CoroutineStart.LAZY) {
6161
delay(60000L)

0 commit comments

Comments
 (0)