Skip to content

Commit

Permalink
Support PokeMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
ryoii committed Mar 31, 2020
1 parent acbdf3a commit be9741d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
19 changes: 19 additions & 0 deletions MessageType.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,22 @@
| -------- | ------ | ------- |
| content | String | 内容 |

#### Poke

```json5
{
"type": "Poke",
"name": "SixSixSix"
}
```

| 名字 | 类型 | 说明 |
| ---- | ------ | ------------ |
| name | String | 戳一戳的类型 |

1. "Poke": 戳一戳
2. "ShowLove": 比心
3. "Like": 点赞
4. "Heartbroken": 心碎
5. "SixSixSix": 666
6. "FangDaZhao": 放大招
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.api.http.HttpApiPluginBase
import net.mamoe.mirai.api.http.util.FaceMap
import net.mamoe.mirai.api.http.util.PokeMap
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.message.FriendMessage
Expand Down Expand Up @@ -86,6 +87,12 @@ data class QuoteDTO(
val origin: MessageChainDTO
) : MessageDTO()

@Serializable
@SerialName("Poke")
data class PokeMessageDTO(
val name: String
) : MessageDTO()

@Serializable
@SerialName("Unknown")
object UnknownMessageDTO : MessageDTO()
Expand Down Expand Up @@ -145,6 +152,7 @@ suspend fun Message.toDTO() = when (this) {
is QuoteReply -> QuoteDTO(source.id, source.groupId, source.senderId,
// 避免套娃
source.originalMessage.toMessageChainDTO { it != UnknownMessageDTO && it !is QuoteDTO })
is PokeMessage -> PokeMessageDTO(PokeMap[type])
else -> UnknownMessageDTO
}

Expand All @@ -170,9 +178,11 @@ suspend fun MessageDTO.toMessage(contact: Contact) = when (this) {
is XmlDTO -> XmlMessage(xml)
is JsonDTO -> JsonMessage(json)
is AppDTO -> LightApp(content)
is PokeMessageDTO -> PokeMap[name]
// ignore
is QuoteDTO,
is MessageSourceDTO,
is UnknownMessageDTO -> null
is UnknownMessageDTO,
-> null
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.mamoe.mirai.api.http.util

import net.mamoe.mirai.message.data.PokeMessage
import net.mamoe.mirai.message.data.PokeMessage.Types.Poke
import kotlin.reflect.full.memberProperties

class PokeMap {

companion object {
private val type2name = mutableMapOf<Int, String>()
private val name2Poke = mutableMapOf<String, PokeMessage>()

init {
PokeMessage.Types::class.memberProperties.forEach {
val n = it.name
val p = it.get(PokeMessage.Types) as PokeMessage
type2name[p.type] = n
name2Poke[n] = p
}
}

operator fun get(type: Int) = type2name[type] ?: "未知戳一戳"
operator fun get(name: String) = name2Poke[name] ?: Poke
}
}

0 comments on commit be9741d

Please sign in to comment.