cpp-mirai-client  v2.6.1
cpp client for mirai-api-http
ForwardMessageNode.hpp
浏览该文件的文档.
1// Copyright (C) 2022 Numendacil and contributors
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16#ifndef MIRAI_FORWARD_MESSAGE_NODE_HPP_
17#define MIRAI_FORWARD_MESSAGE_NODE_HPP_
18
19#include <optional>
20#include "ForwardMessage.hpp"
21#include "MessageChain.hpp"
22
23namespace Mirai
24{
25
26/**
27* @brief 转发消息节点,代表一条转发消息
28*
29* 接收消息时会传入 `MessageChain`,消息id一般为 `null`。
30* 发送时可以选择使用消息链手动构造转发消息或传入消息id自动引用历史消息,
31* 优先级为 `MessageId > MessageChain`
32*
33* Member Variable | Default Value
34* --------------- | -------------
35* `Node::SenderId_` | `0_qq`
36* `Node::time_` | `0`
37* `Node::SenderName_` | `""`
38* `Node::message_` | `MessageChain{}`
39* `Node::MessageId_` | `std::nullopt`
40* `Node::ref_` | `std::nullopt`
41*/
43{
44protected:
46 std::time_t time_ = 0;
47 std::string SenderName_{};
49 std::optional<MessageId_t> MessageId_ = std::nullopt;
50
52 {
54 int64_t target;
55 };
56 std::optional<MessageRef> ref_ = std::nullopt;
57
58public:
59 Node() = default;
60 ~Node() = default;
61
62 bool valid() const
63 {
64 return this->MessageId_.has_value() || (!this->SenderName_.empty() && this->message_.valid());
65 }
66
67 /// 获取发送者QQ
68 QQ_t GetSenderId() const { return this->SenderId_; }
69 /// 获取发送时间
70 std::time_t GetTimestamp() const { return this->time_; }
71 /// 获取发送者名称
72 std::string GetSenderName() const { return this->SenderName_; }
73 /// 获取消息链
74 MessageChain GetMessageChain() const { return this->message_; }
75 /// 是否含有消息id
76 bool hasMessageId() const { return this->MessageId_.has_value() || this->ref_.has_value(); }
77
78 /// 获取消息id
79 std::optional<MessageId_t> GetMessageId() const
80 {
81 return this->ref_ ? this->ref_->MessageId : this->MessageId_;
82 }
83
84 /// 设置发送者
86 {
87 this->SenderId_ = SenderId;
88 return *this;
89 }
90 /// 设置发送时间戳
91 Node& SetTimestamp(std::time_t time)
92 {
93 this->time_ = time;
94 return *this;
95 }
96 /// 设置发送者名称
97 Node& SetSenderName(const std::string& SenderName)
98 {
99 this->SenderName_ = SenderName;
100 return *this;
101 }
102 /// 设置消息内容
104 {
105 this->MessageId_ = std::nullopt;
106 this->message_ = message;
107 return *this;
108 }
109 /// 设置消息内容
111 {
112 this->MessageId_ = std::nullopt;
113 this->message_ = std::move(message);
114 return *this;
115 }
116 /// 设置消息id,限定为当前会话内的消息
118 {
119 this->MessageId_ = MessageId;
120 return *this;
121 }
122 /// 设置消息来源,需要指定消息上下文
124 {
125 this->ref_ = MessageRef{MessageId, (int64_t)target};
126 return *this;
127 }
128
129 struct Serializable;
130};
131
132} // namespace Mirai
133
134#endif
转发消息节点,代表一条转发消息
Node & SetSenderName(const std::string &SenderName)
设置发送者名称
Node & SetMessageChain(const MessageChain &message)
设置消息内容
std::optional< MessageId_t > GetMessageId() const
获取消息id
std::time_t GetTimestamp() const
获取发送时间
bool hasMessageId() const
是否含有消息id
MessageChain GetMessageChain() const
获取消息链
Node & SetMessageRef(MessageId_t MessageId, UID_t target)
设置消息来源,需要指定消息上下文
Node & SetSenderId(QQ_t SenderId)
设置发送者
std::string GetSenderName() const
获取发送者名称
Node & SetMessageChain(MessageChain &&message)
设置消息内容
Node & SetMessageId(MessageId_t MessageId)
设置消息id,限定为当前会话内的消息
std::optional< MessageId_t > MessageId_
std::optional< MessageRef > ref_
Node & SetTimestamp(std::time_t time)
设置发送时间戳
QQ_t GetSenderId() const
获取发送者QQ
消息链对象,由一系列消息元素组成
bool valid() const
检查消息链是否有效
QQ号码类型
Definition: BasicTypes.hpp:71
Base class for QQ_t and GID_t
Definition: BasicTypes.hpp:43
所有mirai相关的对象的命名空间
int64_t MessageId_t
消息id类型,用于撤回消息和引用消息
Definition: BasicTypes.hpp:35