ForwardMessageBuilder

class ForwardMessageBuilder : MutableList<ForwardMessage.INode>

转发消息 DSL 构建器.

总览

在 Java 使用一般方法添加消息构建转发:

ForwardMessageBuilder builder = new ForwardMessageBuilder(group);

builder.add(123456L, "群员A", new PlainText("消息"))
builder.add(user, new PlainText("msg"));
builder.add(user, (chain) -> {
chain.add(new At(user));
chain.add(new PlainText("Hello"))
}); // 使用 Java 8 lambda 语法, MessageChainBuilder 快速构建消息链
builder.add(event); // 可添加 MessageEvent

builder.setDisplayStrategy(new ForwardMessage.DisplayStrategy() {
}); // 可选, 修改显示策略

ForwardMessage forward = builder.build();
group.sendMessage(forward);

在 Kotlin 使用一般方法添加消息构建转发:

val forward: ForwardMessage = buildForwardMessage {
add(123456L, "群员A", PlainText("消息"))
add(user, PlainText("msg"))
add(user) {
// this: MessageChainBuilder
add(At(user))
add(PlainText("msg"))
};
add(event) // 可添加 MessageEvent
}

group.sendMessage(forward);
friend.sendMessage(forward);

在 Kotlin 也可以使用 DSL 构建一个转发:

buildForwardMessage {
123456789 named "鸽子 A" says "咕" // 意为 名为 "鸽子 A" 的用户 123456789 发送了一条内容为 "咕" 的消息
100200300 named "鸽子 C" at 1582315452 says "咕咕咕" // at 设置时间 (在 PC 端显示, 在手机端不影响顺序)
987654321 named "鸽子 B" says "咕" // 未指定时间, 则自动顺序安排时间
myFriend says "咕" // User.says
bot says { // 构造消息链, 同 `buildMessageChain`
add("发个图片试试")
add(Image("{90CCED1C-2D64-313B-5D66-46625CAB31D7}.jpg"))
}
val member: Member = ...
member says "我是幸运群员" // 使用 `User says` 则会同时设置发送人名称
}

DSL 语法在下文详细解释.

上述示例效果一样, 根据个人偏好选择.

Kotlin DSL 语法

下文中 S 代表消息发送人. 可接受: 发送人账号 id(LongInt) 或 User

下文中 M 代表消息内容. 可接受: String, Message, 或 构造消息链 的 DSL 代码块

陈述一条消息

使用 infix fun S.says(M)

语句 123456789 named "鸽子 A" says "咕" 创建并添加了一条名为 "鸽子 A" 的用户 123456789 发送的内容为 "咕" 的消息

陈述

一条 '陈述' 必须包含以下属性:

  • 发送人. 只可以作为 infix 函数的接收者 (receiver) 设置, 如 sender says M, sender named "xxx", sender at 123

  • 消息内容. 只可以通过 says 函数的参数设置, 即 says M.

组合陈述

现支持的可选属性为 named, at

最基础的陈述为 S says M. 可在 says 前按任意顺序添加组合属性:

S named "xxx" says M;

S at 123456 says M; 其中 123456 为发信时间

属性的顺序并不重要. 如下两句陈述效果相同.

S named "xxx" at 123456 says M;

S at 123456 named "xxx" says M;

重复属性

若属性有重复, 新属性会替换旧属性.

S named "name1" named "name2" says M 最终的发送人名称为 "name2"

Constructors

Link copied to clipboard
fun ForwardMessageBuilder(context: Contact)
Link copied to clipboard
fun ForwardMessageBuilder(context: Contact, initialSize: Int)

Types

Link copied to clipboard
inner class BuilderNode : ForwardMessage.INode

Functions

Link copied to clipboard
fun add(event: MessageEvent): ForwardMessageBuilder

从消息事件添加一个消息

open override fun add(element: ForwardMessage.INode): Boolean
open override fun add(index: Int, element: ForwardMessage.INode)

fun add(    sender: User,     message: Message,     time: Int = -1): ForwardMessageBuilder
fun add(    sender: UserOrBot,     message: Message,     time: Int = -1): ForwardMessageBuilder
fun add(    senderId: Long,     senderName: String,     message: Message,     time: Int = -1): ForwardMessageBuilder

添加一个消息. 若不指定 time, 将会使用 currentTime 自增 1 的时间.

inline fun add(    sender: UserOrBot,     time: Int = -1,     builderAction: MessageChainBuilder.() -> Unit): ForwardMessageBuilder
inline fun add(    senderId: Long,     senderName: String,     time: Int = -1,     builderAction: MessageChainBuilder.() -> Unit): ForwardMessageBuilder

构建并添加一个消息. 若不指定 time, 将会使用 currentTime 自增 1 的时间.

Link copied to clipboard
open override fun addAll(elements: Collection<ForwardMessage.INode>): Boolean
open override fun addAll(index: Int, elements: Collection<ForwardMessage.INode>): Boolean
Link copied to clipboard
infix fun Int.at(time: Int): ForwardMessageBuilder.BuilderNode
infix fun Long.at(time: Int): ForwardMessageBuilder.BuilderNode

为一条消息指定时间.

infix fun User.at(time: Int): ForwardMessageBuilder.BuilderNode
infix fun UserOrBot.at(time: Int): ForwardMessageBuilder.BuilderNode

为一条消息指定时间和发送人名称.

Link copied to clipboard
fun build(): ForwardMessage

使用 displayStrategy 渲染并构造可以发送的 ForwardMessage.

Link copied to clipboard
open override fun clear()
Link copied to clipboard
open operator override fun contains(element: ForwardMessage.INode): Boolean
Link copied to clipboard
open override fun containsAll(elements: Collection<ForwardMessage.INode>): Boolean
Link copied to clipboard
open operator override fun get(index: Int): ForwardMessage.INode
Link copied to clipboard
open override fun indexOf(element: ForwardMessage.INode): Int
Link copied to clipboard
open override fun isEmpty(): Boolean
Link copied to clipboard
open operator override fun iterator(): MutableIterator<ForwardMessage.INode>
Link copied to clipboard
open override fun lastIndexOf(element: ForwardMessage.INode): Int
Link copied to clipboard
open override fun listIterator(): MutableListIterator<ForwardMessage.INode>
open override fun listIterator(index: Int): MutableListIterator<ForwardMessage.INode>
Link copied to clipboard
infix fun Int.named(name: String): ForwardMessageBuilder.BuilderNode
infix fun Long.named(name: String): ForwardMessageBuilder.BuilderNode
infix fun User.named(name: String): ForwardMessageBuilder.BuilderNode
infix fun UserOrBot.named(name: String): ForwardMessageBuilder.BuilderNode

为一条消息指定发送人名称.

Link copied to clipboard
open override fun remove(element: ForwardMessage.INode): Boolean
Link copied to clipboard
open override fun removeAll(elements: Collection<ForwardMessage.INode>): Boolean
Link copied to clipboard
open override fun removeAt(index: Int): ForwardMessage.INode
Link copied to clipboard
open override fun retainAll(elements: Collection<ForwardMessage.INode>): Boolean
Link copied to clipboard
infix inline fun Int.says(chain: MessageChainBuilder.() -> Unit): ForwardMessageBuilder
infix fun Int.says(message: String): ForwardMessageBuilder
infix fun Int.says(message: Message): ForwardMessageBuilder
infix fun Long.says(message: String): ForwardMessageBuilder
infix fun Long.says(message: Message): ForwardMessageBuilder
infix fun Bot.says(message: String): ForwardMessageBuilder
infix fun Bot.says(message: Message): ForwardMessageBuilder
infix fun User.says(message: String): ForwardMessageBuilder
infix fun User.says(message: Message): ForwardMessageBuilder
infix fun UserOrBot.says(message: Message): ForwardMessageBuilder

添加一条消息, 自动按顺序调整时间

infix inline fun Long.says(chain: MessageChainBuilder.() -> Unit): ForwardMessageBuilder
infix inline fun Bot.says(chain: MessageChainBuilder.() -> Unit): ForwardMessageBuilder
infix inline fun User.says(chain: MessageChainBuilder.() -> Unit): ForwardMessageBuilder
infix inline fun UserOrBot.says(chain: MessageChainBuilder.() -> Unit): ForwardMessageBuilder

构造并添加一个 MessageChain, 自动按顺序调整时间

Link copied to clipboard
open operator override fun set(index: Int, element: ForwardMessage.INode): ForwardMessage.INode
Link copied to clipboard
open override fun subList(fromIndex: Int, toIndex: Int): MutableList<ForwardMessage.INode>
Link copied to clipboard
fun toRawForwardMessage(): RawForwardMessage

Properties

Link copied to clipboard
val context: Contact

消息语境. 可为 GroupUser. 用来确定某 ID 的用户的昵称.

Link copied to clipboard
var currentTime: Int

当前时间. 在使用 says 时若不指定时间, 则会使用 currentTime 自增 1 的时间.

Link copied to clipboard
var displayStrategy: ForwardMessage.DisplayStrategy
Link copied to clipboard
open override val size: Int