跳到主要内容

Segments - 消息片段

消息片段是构成消息链的基本单元,每种片段类型代表不同的消息内容。

基类 Segment

所有消息片段的抽象基类。

declare abstract class Segment {
abstract AsString(): string; // 转换为字符串表示
}

文本片段 (TextSegment)

表示纯文本类型的消息片段。

declare class TextSegment extends Segment {
Text: string; // 文本内容
AsString(): string; // 返回原始文本
}

属性

  • Text: string - 文本内容

方法

  • AsString(): 返回未修改的文本字符串

示例:

const textSegment = new TextSegment();
textSegment.Text = "Hello World";
logger.info(textSegment.AsString()); // "Hello World"

@用户片段 (AtSegment)

表示@用户类型的消息片段。

declare class AtSegment extends Segment {
Id: string; // 被@的用户ID
AsString(): string; // 返回"@用户ID"格式
}

属性

  • Id: string - 被@的用户ID

方法

  • AsString(): 生成格式为"@用户ID"的字符串

示例:

const atSegment = new AtSegment();
atSegment.Id = "123456789";
logger.info(atSegment.AsString()); // "@123456789"

图片片段 (ImageSegment)

表示图片类型的消息片段。

declare class ImageSegment extends Segment {
Uri: string; // 图片URI地址
AsString(): string; // 返回"[图片]"
}

属性

  • Uri: string - 图片资源的URI地址

方法

  • AsString(): 生成格式为"[图片]"的字符串

示例:

const imageSegment = new ImageSegment();
imageSegment.Uri = "https://example.com/image.jpg";
logger.info(imageSegment.AsString()); // "[图片]"

表情片段 (FaceSegment)

表示表情类型的消息片段。

declare class FaceSegment extends Segment {
id: string; // 表情ID (注意小写)
AsString(): string; // 返回"[表情ID]"
}

属性

  • id: string - 表情的唯一标识符(注意属性名为小写)

方法

  • AsString(): 生成格式为"[表情ID]"的字符串

示例:

const faceSegment = new FaceSegment();
faceSegment.id = "1";
logger.info(faceSegment.AsString()); // "[1]"

回复片段 (ReplySegment)

表示回复引用类型的消息片段。

declare class ReplySegment extends Segment {
Id: string; // 被回复的消息ID
AsString(): string; // 返回"[回复消息ID]"
}

属性

  • Id: string - 被引用消息的ID

方法

  • AsString(): 生成格式为"[回复消息ID]"的字符串

示例:

const replySegment = new ReplySegment();
replySegment.Id = "msg123";
logger.info(replySegment.AsString()); // "[回复msg123]"

合并转发节点片段 (NodeSegment)

v0.4.0 新增

表示合并转发消息中的一个节点,用于 SendForwardMessage 方法。

declare class NodeSegment extends Segment {
Id: string; // 节点标识
Name: string; // 发送者名称
UserId: string; // 发送者用户ID
Chain: MessageChain; // 节点内的消息链
AsString(): string; // 返回"[合并转发节点]"
}

属性

  • Id: string - 节点唯一标识
  • Name: string - 发送者的显示名称
  • UserId: string - 发送者的用户ID
  • Chain: MessageChain - 该节点包含的消息链

方法

  • AsString(): 生成格式为"[合并转发节点]"的字符串

通过 MessageChain.Node() 流式构建

const chain = new MessageChain()
.Node("node1", "用户A", "123456", new MessageChain().Text("第一段消息"))
.Node("node2", "用户B", "789012", new MessageChain().Text("第二段消息").Image("https://example.com/pic.jpg"));

未支持片段 (UnsupportedSegment)

表示无法识别的消息片段类型。

declare class UnsupportedSegment extends Segment {
Type: string; // 原始片段类型
Data: string; // 原始片段数据
AsString(): string; // 返回"[未知类型]"
}

属性

  • Type: string - 原始片段类型标识
  • Data: string - 原始片段数据

方法

  • AsString(): 生成格式为"[未知类型]"的字符串

示例:

const unsupportedSegment = new UnsupportedSegment();
unsupportedSegment.Type = "custom";
unsupportedSegment.Data = "some data";
logger.info(unsupportedSegment.AsString()); // "[未知类型]"

使用示例

创建不同类型的片段

// 文本片段
const text = new TextSegment();
text.Text = "Hello";

// @用户片段
const at = new AtSegment();
at.Id = "123456";

// 图片片段
const image = new ImageSegment();
image.Uri = "https://example.com/pic.jpg";

// 表情片段
const face = new FaceSegment();
face.id = "1";

// 回复片段
const reply = new ReplySegment();
reply.Id = "msg123";

添加到消息链

const chain = new MessageChain();
chain.Add(text);
chain.Add(at);
chain.Add(image);
chain.Add(face);
chain.Add(reply);

logger.info(chain.AsString());

注意事项

  1. 类型识别: 每种片段类型都有特定的用途和格式
  2. 字符串转换: AsString() 方法用于调试和日志输出
  3. 属性命名: 注意 FaceSegment 的 id 属性是小写
  4. 扩展性: UnsupportedSegment 用于处理未来可能的新片段类型
  5. 继承关系: 所有片段都继承自 Segment 基类

遇到麻烦了?

有偿服务

我们提供有偿代安装服务,解决您的环境配置烦恼。

了解详情
Miku

少年,买服务器吗?

持证经营

专注高性价比游戏云VPS,铂金 / I7 / R9 / 物理机

💰 最低六元起、买不了吃亏买不了上当,快来看看吧~

快来看看