Skip to content

Commit

Permalink
Adapt updating for 2.0-M2
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoii committed Dec 28, 2020
1 parent fb8d6df commit 231a27e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ class AuthedSession internal constructor(val bot: Bot, originKey: String, corout
internal var latestUsed = currentTimeSeconds()

init {
_listener = bot.subscribeAlways{
_listener = bot.eventChannel.subscribeAlways{
if (this.bot === this@AuthedSession.bot) {
this.run(messageQueue::add)
}
}
_cache = bot.subscribeAlways {
_cache = bot.eventChannel.subscribeAlways {
if (this.bot === this@AuthedSession.bot) {
cacheQueue.add(this.source)
}
Expand Down Expand Up @@ -167,7 +167,7 @@ class AuthedSession internal constructor(val bot: Bot, originKey: String, corout

fun disableWebSocket() {
if (_listener.isCompleted) {
_listener = bot.subscribeAlways{
_listener = bot.eventChannel.subscribeAlways{
if (this.bot === this@AuthedSession.bot) {
this.run(messageQueue::add)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ suspend fun BotEvent.toDTO() = when (this) {
authorId,
messageIds.firstOrNull() ?: 0,
messageTime.toLong() and 0xFFFF,
operator
operatorId
)
is BotGroupPermissionChangeEvent -> BotGroupPermissionChangeEventDTO(
origin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import net.mamoe.mirai.event.events.TempMessageEvent
import net.mamoe.mirai.message.*
import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.message.data.Image.Key.queryUrl
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage
import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsVoice
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.uploadImage
import java.net.URL

/*
Expand Down Expand Up @@ -155,7 +158,6 @@ suspend fun MessageEvent.toDTO() = when (this) {
}
}

@OptIn(ExperimentalMessageKey::class)
suspend inline fun MessageChain.toMessageChainDTO(filter: (MessageDTO) -> Boolean): MessageChainDTO =
// `foreachContent`会忽略`MessageSource`,手动添加
mutableListOf(this.getOrFail(MessageSource).toDTO()).apply {
Expand Down Expand Up @@ -192,43 +194,43 @@ suspend fun Message.toDTO() = when (this) {
else -> UnknownMessageDTO
}

@OptIn(MiraiInternalApi::class)
@OptIn(MiraiInternalApi::class, MiraiExperimentalApi::class)
suspend fun MessageDTO.toMessage(contact: Contact) = when (this) {
is AtDTO -> (contact as Group).getOrFail(target).at()
is AtAllDTO -> AtAll
is FaceDTO -> when {
faceId >= 0 -> Face(faceId)
name.isNotEmpty() -> Face(FaceMap[name])
else -> Face(Face.unknown)
else -> Face(255)
}
is PlainDTO -> PlainText(text)
is ImageDTO -> when {
!imageId.isNullOrBlank() -> Image(imageId)
!url.isNullOrBlank() -> contact.uploadImage(withContext(Dispatchers.IO) { URL(url).openStream() })
!url.isNullOrBlank() -> withContext(Dispatchers.IO) { URL(url).openStream().uploadAsImage(contact) }
!path.isNullOrBlank() -> with(HttpApiPluginBase.image(path)) {
if (exists()) {
contact.uploadImage(this)
uploadAsImage(contact)
} else throw NoSuchFileException(this)
}
else -> null
}
is FlashImageDTO -> when {
!imageId.isNullOrBlank() -> Image(imageId)
!url.isNullOrBlank() -> contact.uploadImage(withContext(Dispatchers.IO) { URL(url).openStream() })
!url.isNullOrBlank() -> withContext(Dispatchers.IO) { URL(url).openStream().uploadAsImage(contact) }
!path.isNullOrBlank() -> with(HttpApiPluginBase.image(path)) {
if (exists()) {
contact.uploadImage(this)
uploadAsImage(contact)
} else throw NoSuchFileException(this)
}
else -> null
}?.flash()
is VoiceDTO -> when {
contact !is Group -> null
!voiceId.isNullOrBlank() -> Voice(voiceId, voiceId.substringBefore(".").toHexArray(), 0, "")
!url.isNullOrBlank() -> contact.uploadVoice(withContext(Dispatchers.IO) { URL(url).openStream() })
!voiceId.isNullOrBlank() -> Voice(voiceId, voiceId.substringBefore(".").toHexArray(), 0, 0, "")
!url.isNullOrBlank() -> withContext(Dispatchers.IO) { URL(url).openStream().toExternalResource().uploadAsVoice(contact) }
!path.isNullOrBlank() -> with(HttpApiPluginBase.voice(path)) {
if (exists()) {
contact.uploadVoice(this.inputStream())
inputStream().toExternalResource().uploadAsVoice(contact)
} else throw NoSuchFileException(this)
}
else -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import net.mamoe.mirai.message.data.MessageSource.Key.quote
import net.mamoe.mirai.message.data.MessageSource.Key.recall
import net.mamoe.mirai.message.data.OnlineMessageSource.Incoming
import net.mamoe.mirai.message.data.OnlineMessageSource.Outgoing
import net.mamoe.mirai.utils.uploadImage
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage
import java.net.URL

/**
Expand Down Expand Up @@ -209,7 +210,7 @@ fun Application.messageModule() {
it.group != null -> bot.getGroupOrFail(it.group)
else -> throw IllegalParamException("target、qq、group不可全为null")
}
val ls = it.urls.map { url -> contact.uploadImage(URL(url).openStream()) }
val ls = it.urls.map { url -> URL(url).openStream().uploadAsImage(contact) }
val receipt = contact.sendMessage(buildMessageChain { addAll(ls) })

it.session.cacheQueue.add(receipt.source)
Expand All @@ -231,10 +232,10 @@ fun Application.messageModule() {
)

when (type) {
"group" -> session.bot.groups.firstOrNull()?.uploadImage(newFile.await())
"group" -> session.bot.groups.firstOrNull()?.uploadImage(newFile.await().toExternalResource())
"friend",
"temp"
-> session.bot.friends.firstOrNull()?.uploadImage(newFile.await())
-> session.bot.friends.firstOrNull()?.uploadImage(newFile.await().toExternalResource())
else -> null
}.apply {
// 使用apply不影响when返回
Expand Down Expand Up @@ -269,7 +270,7 @@ fun Application.messageModule() {
)

when (type) {
"group" -> session.bot.groups.firstOrNull()?.uploadVoice(newFile.await().inputStream())
"group" -> session.bot.groups.firstOrNull()?.uploadVoice(newFile.await().toExternalResource())
else -> null
}.apply {
// 使用apply不影响when返回
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun Application.websocketRouteModule() {
* 广播通知消息
*/
miraiWebsocket("/message") { session ->
val listener = session.bot.subscribeMessages {
val listener = session.bot.eventChannel.subscribeMessages {
content { bot === session.bot }.invoke {
this.toDTO().takeIf { dto -> dto != IgnoreEventDTO }?.apply {
outgoing.send(Frame.Text(this.toJson()))
Expand All @@ -57,7 +57,7 @@ fun Application.websocketRouteModule() {
* 广播通知事件
*/
miraiWebsocket("/event") { session ->
val listener = session.bot.subscribeAlways<BotEvent> {
val listener = session.bot.eventChannel.subscribeAlways<BotEvent> {
if (it.bot === session.bot && this !is MessageEvent) {
this.toDTO().takeIf { dto -> dto != IgnoreEventDTO }?.apply {
outgoing.send(Frame.Text(this.toJson()))
Expand All @@ -78,7 +78,7 @@ fun Application.websocketRouteModule() {
* 广播通知所有信息(消息,事件)
*/
miraiWebsocket("/all") { session ->
val listener = session.bot.subscribeAlways<BotEvent> {
val listener = session.bot.eventChannel.subscribeAlways<BotEvent> {
if (it.bot === session.bot) {
this.toDTO().takeIf { dto -> dto != IgnoreEventDTO }?.apply {
outgoing.send(Frame.Text(this.toJson()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import net.mamoe.mirai.api.http.service.MiraiApiHttpService
import net.mamoe.mirai.api.http.util.HttpClient
import net.mamoe.mirai.api.http.util.toJson
import net.mamoe.mirai.console.plugin.jvm.JvmPlugin
import net.mamoe.mirai.event.GlobalEventChannel
import net.mamoe.mirai.event.Listener
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.event.subscribeAlways
Expand Down Expand Up @@ -46,7 +47,7 @@ class ReportService(
}

override fun onEnable() {
subscription = console.subscribeAlways {
subscription = GlobalEventChannel.subscribeAlways {
this.takeIf { reportConfig.enable }
?.apply {
this.takeIf { reportConfig.eventMessage.report }
Expand Down

0 comments on commit 231a27e

Please sign in to comment.