Skip to content

Commit 82879b8

Browse files
committed
fix: event
1 parent fb8a4a0 commit 82879b8

File tree

2 files changed

+61
-62
lines changed

2 files changed

+61
-62
lines changed

src/main/kotlin/xyz/cssxsh/mirai/pixiv/PixivEventListener.kt

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
11
package xyz.cssxsh.mirai.pixiv
22

3+
import io.ktor.client.request.*
4+
import io.ktor.http.*
35
import net.mamoe.mirai.console.command.CommandSender.Companion.toCommandSender
46
import net.mamoe.mirai.console.command.UserCommandSender
57
import net.mamoe.mirai.console.permission.*
68
import net.mamoe.mirai.console.permission.PermissionService.Companion.testPermission
79
import net.mamoe.mirai.event.*
810
import net.mamoe.mirai.event.events.*
11+
import net.mamoe.mirai.message.data.*
912
import net.mamoe.mirai.utils.*
1013
import xyz.cssxsh.mirai.pixiv.event.*
14+
import xyz.cssxsh.mirai.pixiv.model.*
15+
import xyz.cssxsh.pixiv.apps.*
1116

1217
public object PixivEventListener : SimpleListenerHost() {
1318
private val logger by lazy { MiraiLogger.Factory.create(this::class, identity = "pixiv-event-listener") }
1419

1520
@EventHandler
1621
public fun PixivEvent.handle() {
1722
helper
18-
// TODO()
23+
// TODO: stop send
1924
}
2025

2126
public var paserPermission: Permission = Permission.getRootPermission()
2227

2328
@EventHandler
2429
public suspend fun MessageEvent.handle() {
30+
val content = message.findIsInstance<PlainText>()?.content ?: return
2531
if (this is MessageSyncEvent) return
2632
val context = toCommandSender() as UserCommandSender
2733
if (paserPermission.testPermission(context).not()) return
28-
val content = message.contentToString()
2934
URL_ARTWORK_REGEX.find(content)?.let { match ->
3035
logger.info { "匹配ARTWORK(${match.value})" }
3136
context.withHelper {
@@ -34,23 +39,38 @@ public object PixivEventListener : SimpleListenerHost() {
3439
}
3540
URL_USER_REGEX.find(content)?.let { match ->
3641
logger.info { "匹配USER(${match.value})" }
37-
// TODO("paser URL_USER_REGEX")
42+
context.withHelper {
43+
client.userDetail(uid = match.value.toLong())
44+
}
3845
}
3946
URL_PIXIV_ME_REGEX.find(content)?.let { match ->
4047
logger.info { "匹配USER(${match.value})" }
41-
// TODO("paser URL_PIXIV_ME_REGEX")
48+
context.withHelper {
49+
val uid = UserBaseInfo.get(account = match.value)?.uid ?: client.useHttpClient { http ->
50+
http.head("https://pixiv.me/${match.value}")
51+
.headers[HttpHeaders.Location]
52+
?.let { URL_USER_REGEX.find(it) }
53+
?.value
54+
?.toLong()
55+
}
56+
if (uid != null) {
57+
client.userDetail(uid = uid)
58+
} else {
59+
null
60+
}
61+
}
4262
}
4363
URL_PIXIVISION_ARTICLE.find(content)?.let { match ->
4464
logger.info { "匹配ARTICLE(${match.value})" }
45-
// TODO("paser URL_PIXIVISION_ARTICLE")
65+
// TODO: paser URL_PIXIVISION_ARTICLE
4666
}
4767
URL_FANBOX_CREATOR_REGEX.find(content)?.let { match ->
4868
logger.info { "匹配FANBOX(${match.value})" }
49-
// TODO("paser URL_FANBOX_CREATOR_REGEX")
69+
// TODO: paser URL_FANBOX_CREATOR_REGEX
5070
}
5171
URL_FANBOX_ID_REGEX.find(content)?.let { match ->
5272
logger.info { "匹配FANBOX(${match.value})" }
53-
// TODO("paser URL_FANBOX_ID_REGEX")
73+
// TODO: paser URL_FANBOX_ID_REGEX
5474
}
5575
}
5676
}
@@ -91,20 +111,6 @@ public object PixivEventListener : SimpleListenerHost() {
91111
// toCommandSender().takeIf { permission.testPermission(it) }?.sendCreatorInfo(uid = result.value.toLong())
92112
// }
93113
// }
94-
// "InitHelper" with subscribeAlways<BotOnlineEvent> {
95-
// if (PixivConfigData.default.isNotBlank()) {
96-
// bot.groups.maxByOrNull { it.members.size }?.helper?.info()
97-
// }
98-
// for ((id, _) in PixivConfigData.tokens) {
99-
// try {
100-
// @OptIn(ConsoleExperimentalApi::class)
101-
// bot.getContactOrNull(id)?.helper?.info()
102-
// } catch (e: Throwable) {
103-
// logger.warning { "init $id $e" }
104-
// }
105-
// }
106-
// // logger.info { "abilities: ${abilities.mapNotNull { it.uid }}" }
107-
// }
108114
// }
109115
//
110116
// internal fun stop() = synchronized(listeners) {

src/main/kotlin/xyz/cssxsh/mirai/pixiv/PixivUtils.kt

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ internal suspend fun UserCommandSender.withHelper(block: suspend PixivHelper.()
5555
is String -> quoteReply(message)
5656
is IllustInfo -> sendIllust(message)
5757
is ArtWorkInfo -> sendArtwork(message)
58+
is UserPreview -> quoteReply(buildUserPreviewMessage(message, subject))
59+
is UserDetail -> quoteReply(buildUserDetailMessage(message, subject))
5860
else -> quoteReply(message.toString())
5961
}
6062
} catch (exception: CancellationException) {
@@ -317,46 +319,37 @@ public suspend fun buildIllustMessage(illust: IllustInfo, contact: Contact): Mes
317319
}
318320
}
319321

320-
//
321-
//internal suspend fun PixivHelper.buildMessageByUser(preview: UserPreview) = buildMessageChain {
322-
// appendLine("NAME: ${preview.user.name}")
323-
// appendLine("UID: ${preview.user.id}")
324-
// appendLine("ACCOUNT: ${preview.user.account}")
325-
// appendLine("FOLLOWED: ${preview.user.isFollowed}")
326-
// appendLine("TWITTER: ${Twitter[preview.user.id].joinToString { it.screen }}")
327-
// try {
328-
// append(preview.user.getProfileImage().uploadAsImage(contact))
329-
// } catch (e: Throwable) {
330-
// logger.warning({ "User(${preview.user.id}) ProfileImage 下载失败" }, e)
331-
// }
332-
// for (illust in preview.illusts.apply { replicate() }.write()) {
333-
// if (illust.isEro().not()) continue
334-
// try {
335-
// if (illust.age == AgeLimit.ALL) {
336-
// illust.useImageResources { index, resource ->
337-
// if (index < 1) add(resource.uploadAsImage(contact))
338-
// }
339-
// }
340-
// } catch (e: Throwable) {
341-
// logger.warning({ "User(${preview.user.id}) PreviewImage 下载失败" }, e)
342-
// }
343-
// }
344-
//}
345-
//
346-
//internal suspend fun PixivHelper.buildMessageByUser(detail: UserDetail) = buildMessageChain {
347-
// appendLine("NAME: ${detail.user.name}")
348-
// appendLine("UID: ${detail.user.id}")
349-
// appendLine("ACCOUNT: ${detail.user.account}")
350-
// appendLine("FOLLOWED: ${detail.user.isFollowed}")
351-
// appendLine("TOTAL: ${detail.profile.totalArtwork}")
352-
// appendLine("TWITTER: ${detail.twitter()}")
353-
// try {
354-
// append(detail.user.getProfileImage().uploadAsImage(contact))
355-
// } catch (e: Throwable) {
356-
// logger.warning({ "User(${detail.user.id}) ProfileImage 下载失败" }, e)
357-
// }
358-
//}
359-
//
322+
public suspend fun buildUserPreviewMessage(preview: UserPreview, contact: Contact): MessageChain {
323+
return buildMessageChain {
324+
appendLine("NAME: ${preview.user.name}")
325+
appendLine("UID: ${preview.user.id}")
326+
appendLine("ACCOUNT: ${preview.user.account}")
327+
appendLine("FOLLOWED: ${preview.user.isFollowed}")
328+
appendLine("TWITTER: ${Twitter[preview.user.id].joinToString { it.screen }}")
329+
try {
330+
append(preview.user.profile().uploadAsImage(contact))
331+
} catch (cause: Exception) {
332+
logger.warning({ "User(${preview.user.id}) ProfileImage 下载失败" }, cause)
333+
}
334+
}
335+
}
336+
337+
public suspend fun buildUserDetailMessage(detail: UserDetail, contact: Contact): MessageChain {
338+
return buildMessageChain {
339+
appendLine("NAME: ${detail.user.name}")
340+
appendLine("UID: ${detail.user.id}")
341+
appendLine("ACCOUNT: ${detail.user.account}")
342+
appendLine("FOLLOWED: ${detail.user.isFollowed}")
343+
appendLine("TOTAL: ${detail.profile.totalArtwork}")
344+
appendLine("TWITTER: ${detail.twitter()}")
345+
try {
346+
append(detail.user.profile().uploadAsImage(contact))
347+
} catch (cause: Exception) {
348+
logger.warning({ "User(${detail.user.id}) ProfileImage 下载失败" }, cause)
349+
}
350+
}
351+
}
352+
360353
//internal suspend fun PixivHelper.buildMessageByUser(uid: Long) = buildMessageByUser(detail = userDetail(uid))
361354
//
362355
//internal suspend fun PixivHelper.buildMessageByCreator(creator: CreatorDetail) = buildMessageChain {
@@ -438,7 +431,7 @@ public suspend fun loadIllustInfo(
438431

439432
// region Download Image
440433

441-
internal suspend fun UserInfo.getProfileImage(): File {
434+
internal suspend fun UserInfo.profile(): File {
442435
val image = Url(profileImageUrls.values.lastOrNull() ?: NO_PROFILE_IMAGE)
443436
return ProfileFolder.resolve(image.filename).apply {
444437
if (exists().not()) {

0 commit comments

Comments
 (0)