forked from mamoe/mirai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.kt
137 lines (122 loc) · 4.44 KB
/
User.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright 2019-2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
@file:Suppress("EXPERIMENTAL_API_USAGE", "unused")
package net.mamoe.mirai.contact
import kotlinx.coroutines.CoroutineScope
import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.Bot
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.message.MessageReceipt
import net.mamoe.mirai.message.MessageReceipt.Companion.recall
import net.mamoe.mirai.message.action.Nudge
import net.mamoe.mirai.message.action.UserNudge
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.isContentEmpty
import net.mamoe.mirai.message.data.toPlainText
import net.mamoe.mirai.utils.ExternalImage
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.OverFileSizeMaxException
/**
* 代表一个 **用户**.
*
* 其子类有 [群成员][Member] 和 [好友][Friend].
* 虽然群成员也可能是好友, 但他们仍是不同的两个类型.
*
* 注意: 一个 [User] 实例并不是独立的, 它属于一个 [Bot].
*
* 对于同一个 [Bot] 任何一个人的 [User] 实例都是单一的.
*/
public interface User : Contact, UserOrBot, CoroutineScope {
/**
* QQ 号码
*/
public override val id: Long
/**
* 昵称
*/
public val nick: String
/**
* 备注信息
*
* 仅 [Bot] 与 [User] 存在好友关系的时候才可能存在备注
*
* [Bot] 与 [User] 没有好友关系时永远为空[字符串][String] ("")
*
* @see [User.remarkOrNick]
*/
public val remark: String
/**
* 头像下载链接
*/
public val avatarUrl: String
get() = "http://q1.qlogo.cn/g?b=qq&nk=$id&s=640"
/**
* 向这个对象发送消息.
*
* 单条消息最大可发送 4500 字符或 50 张图片.
*
* @see UserMessagePreSendEvent 发送消息前事件
* @see UserMessagePostSendEvent 发送消息后事件
*
* @throws EventCancelledException 当发送消息事件被取消时抛出
* @throws BotIsBeingMutedException 发送群消息时若 [Bot] 被禁言抛出
* @throws MessageTooLargeException 当消息过长时抛出
* @throws IllegalArgumentException 当消息内容为空时抛出 (详见 [Message.isContentEmpty])
*
* @return 消息回执. 可进行撤回 ([MessageReceipt.recall])
*/
@JvmBlockingBridge
public override suspend fun sendMessage(message: Message): MessageReceipt<User>
/**
* 发送纯文本消息
* @see sendMessage
*/
@JvmBlockingBridge
public override suspend fun sendMessage(message: String): MessageReceipt<User> =
this.sendMessage(message.toPlainText())
/**
* 创建一个 "戳一戳" 消息
*
* @see Nudge.sendTo 发送这个戳一戳消息
*/
@MiraiExperimentalApi
public override fun nudge(): UserNudge
/**
* 上传一个图片以备发送.
*
* @see Image 查看有关图片的更多信息, 如上传图片
*
* @see BeforeImageUploadEvent 图片发送前事件, cancellable
* @see ImageUploadEvent 图片发送完成事件
*
* @throws EventCancelledException 当发送消息事件被取消
* @throws OverFileSizeMaxException 当图片文件过大而被服务器拒绝上传时. (最大大小约为 20 MB)
*/
@JvmBlockingBridge
public override suspend fun uploadImage(image: ExternalImage): Image
}
/**
* 获取非空备注或昵称.
*
* 若 [备注][User.remark] 不为空则返回备注, 为空则返回 [User.nick]
*/
public val User.remarkOrNick: String get() = this.remark.takeIf { it.isNotEmpty() } ?: this.nick
/**
* 获取非空备注或群名片.
*
* 若 [备注][User.remark] 不为空则返回备注, 为空则返回 [Member.nameCard]
*/
public val Member.remarkOrNameCard: String get() = this.remark.takeIf { it.isNotEmpty() } ?: this.nameCard
/**
* 获取非空备注或群名片或昵称.
*
* 若 [备注][User.remark] 不为空则返回备注, 为空则返回 [Member.nameCardOrNick]
*/
public val Member.remarkOrNameCardOrNick: String get() = this.remark.takeIf { it.isNotEmpty() } ?: this.nameCardOrNick