Skip to content

Commit

Permalink
add avatarUrl overwrite for gensokyo
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Sep 7, 2024
1 parent b5c8f24 commit 636df16
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
18 changes: 18 additions & 0 deletions onebot/src/main/kotlin/client/core/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1475,4 +1475,22 @@ class Bot(
val result = actionHandler.action(this, action, params)
return result.withToken()
}

/**
* 获取用户头像地址
*
* Gensokyo
* @param groupId 群号,获取群成员头像时使用
* @param userId QQ号,获取其它用户头像时使用
* @return [ActionData] of [String]
*/
suspend fun extGetAvatar(groupId: Long?, userId: Long): ActionData<String> {
val action = ActionPathEnum.EXT_GET_AVATAR
val params = JsonObject().apply {
if (groupId != null) addProperty("group_id", groupId)
addProperty("user_id", userId)
}
val result = actionHandler.action(this, action, params)
return result.withToken()
}
}
7 changes: 7 additions & 0 deletions onebot/src/main/kotlin/sdk/enums/ActionPathEnum.kt
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ enum class ActionPathEnum(
*/
EXT_GET_FILE("get_file"),

/**
* 获取用户头像
*
* 属于 Gensokyo 扩展 API
*/
EXT_GET_AVATAR("get_file"),

/**
* 创建群文件夹
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ internal class BotWrapper private constructor(
return OnebotMessages.toMiraiMessage(data.isJsonMessage, data.message, this)
}

private val avatar: String? by lazy {
if (appName.lowercase() != "gensokyo") null
else runBlocking { impl.extGetAvatar(null, id).data }
}
override fun avatarUrl(spec: AvatarSpec): String {
return avatar ?: super.avatarUrl(spec)
}

override val onebotData: String
get() = gson.toJson(json)
override val id: Long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import cn.evolvefield.onebot.sdk.response.contact.FriendInfoResp
import cn.evolvefield.onebot.sdk.util.JsonHelper.gson
import com.google.gson.JsonElement
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.contact.AvatarSpec
import net.mamoe.mirai.contact.Friend
import net.mamoe.mirai.contact.friendgroup.FriendGroup
import net.mamoe.mirai.contact.roaming.RoamingMessages
Expand Down Expand Up @@ -51,6 +53,14 @@ internal class FriendWrapper(
bot.impl.deleteFriend(id)
}

private val avatar: String? by lazy {
if (bot.appName.lowercase() != "gensokyo") null
else runBlocking { bot.impl.extGetAvatar(null, id).data }
}
override fun avatarUrl(spec: AvatarSpec): String {
return avatar ?: super.avatarUrl(spec)
}

@OptIn(MiraiInternalApi::class)
override suspend fun sendMessage(message: Message): MessageReceipt<Friend> {
if (FriendMessagePreSendEvent(this, message).broadcast().isCancelled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import cn.evolvefield.onebot.sdk.util.JsonHelper.gson
import com.google.gson.JsonElement
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.contact.*
import net.mamoe.mirai.contact.active.MemberActive
import net.mamoe.mirai.event.broadcast
Expand Down Expand Up @@ -59,6 +60,14 @@ internal class MemberWrapper(
this.impl = impl
}

private val avatar: String? by lazy {
if (bot.appName.lowercase() != "gensokyo") null
else runBlocking { bot.impl.extGetAvatar(group.id, id).data }
}
override fun avatarUrl(spec: AvatarSpec): String {
return avatar ?: super.avatarUrl(spec)
}

override val active: MemberActiveWrapper = MemberActiveWrapper(this)
override val id: Long = impl.userId
override val joinTimestamp: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import cn.evolvefield.onebot.sdk.response.contact.StrangerInfoResp
import cn.evolvefield.onebot.sdk.util.JsonHelper.gson
import com.google.gson.JsonElement
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.runBlocking
import net.mamoe.mirai.contact.AvatarSpec
import net.mamoe.mirai.contact.Stranger
import net.mamoe.mirai.event.broadcast
import net.mamoe.mirai.event.events.EventCancelledException
Expand Down Expand Up @@ -49,6 +51,14 @@ internal class StrangerWrapper(
OverflowAPI.logger.warning("Onebot 未提供删除陌生人接口 ($id)")
}

private val avatar: String? by lazy {
if (bot.appName.lowercase() != "gensokyo") null
else runBlocking { bot.impl.extGetAvatar(null, id).data }
}
override fun avatarUrl(spec: AvatarSpec): String {
return avatar ?: super.avatarUrl(spec)
}

@OptIn(MiraiInternalApi::class)
override suspend fun sendMessage(message: Message): MessageReceipt<Stranger> {
if (StrangerMessagePreSendEvent(this, message).broadcast().isCancelled)
Expand Down

0 comments on commit 636df16

Please sign in to comment.