Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.

Commit 75e2d23

Browse files
committed
修改文档;
1 parent 8b95f10 commit 75e2d23

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,9 @@
66

77
本项目为 mirai-http-api 的 C++ 封装,方便使用 C++ 开发 mirai-http-api 插件
88

9-
## 项目依赖
10-
11-
> 本项目所有依赖都为 Header-Only 库,已经内置到本项目的 mirai-third-party 文件夹中。
12-
13-
1. [**yhirose/cpp-httplib**](https://github.com/yhirose/cpp-httplib) A C++ header-only HTTP/HTTPS server and client library.
14-
15-
2. [**nlohmann/json**](https://github.com/nlohmann/json) JSON for Modern C++.
16-
17-
3. [**progschj/ThreadPool**](https://github.com/progschj/ThreadPool) A simple C++11 Thread Pool implementation.
18-
19-
4. [**dhbaird/EasyWSClient**](https://github.com/dhbaird/easywsclient) A short and sweet WebSocket client for C++.
20-
219
## 项目文档
2210

23-
> 项目文档没有详细介绍各 API 函数的用法,mirai_bot.hpp 中的注释已经比较详细,如有疑惑,建议浏览。
11+
> 推荐浏览顺序:项目文档 --> examples --> mirai_bot.hpp 的注释
2412
2513
项目文档: [使用说明](doc/使用说明.md)
2614

@@ -43,7 +31,6 @@
4331
| RecallEvent.cpp | 处理其他人撤回消息的事件 |
4432
| Mute.cpp | 和禁言有关的操作 |
4533
| RichMessage.cpp | 发送 JSON、闪照等类型的消息 |
46-
| CompareMessage.cpp | 对比 MessageChain 是否相同 |
4734
| FetchEventsViaHTTP.cpp| 设置通过 HTTP 短轮询获取事件和消息 |
4835
| GroupMemberInfo.cpp | 获取/设置群成员的群名片与群头衔 |
4936

@@ -113,9 +100,19 @@
113100

114101
然后就可以使用 `./vcpkg install mirai-cpp` 安装 mirai-cpp。
115102

116-
#### (2) 使用 CMake-GUI 编译
117103

118-
未完待续……
104+
## 项目依赖
105+
106+
> 本项目所有依赖都为 Header-Only 库,已经内置到本项目的 mirai-third-party 文件夹中。
107+
108+
1. [**yhirose/cpp-httplib**](https://github.com/yhirose/cpp-httplib) A C++ header-only HTTP/HTTPS server and client library.
109+
110+
2. [**nlohmann/json**](https://github.com/nlohmann/json) JSON for Modern C++.
111+
112+
3. [**progschj/ThreadPool**](https://github.com/progschj/ThreadPool) A simple C++11 Thread Pool implementation.
113+
114+
4. [**dhbaird/EasyWSClient**](https://github.com/dhbaird/easywsclient) A short and sweet WebSocket client for C++.
115+
119116

120117
## 代码风格
121118

doc/使用说明.md

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
mirai-cpp 的所有源文件使用 UTF-8 编码保存。
55

6-
此外,强烈推荐**你的机器人项目****源文件**也使用 UTF-8 编码。
6+
强烈推荐**你的机器人项目****源文件**也使用 UTF-8 编码。
77

88
因为没有运行时的编码转换,如果传入给 mirai-cpp 的文本不为 UTF-8 编码,可能会导致异常。
99

@@ -56,15 +56,15 @@ GID_t gid = 123456_gid;
5656
```C++
5757
QQ_t qq = 123456_qq;
5858
// 方法1
59-
int64_t qq = qq.QQ;
59+
int64_t qq = qq.ToInt64();
6060
// 方法2
6161
int64_t qq = int64_t(qq);
6262
// GID 同理
6363
```
6464

6565
# 如何接收、处理事件
6666

67-
MiraiBot 类提供了 On 方法和 OnEventReceived 方法,这两个方法是等价的。mirai-cpp 做了许多工作,让你可以轻松地接收某个事件。这些事件的名称可以在 [mirai-api-http 的文档](https://github.com/mamoe/mirai-api-http/blob/master/EventType.md) 里找到。你只需要知道这个事件的名称,即可使用 On 方法接收此事件。(因为时间关系,当前版本[0.4.4-alpha]有一些事件还没有实现)
67+
MiraiBot 类提供了 On 方法和 OnEventReceived 方法,这两个方法是等价的。mirai-cpp 做了许多工作,让你可以轻松地接收某个事件。这些事件的名称可以在 [mirai-api-http 的文档](https://github.com/mamoe/mirai-api-http/blob/master/EventType.md) 里找到。你只需要知道这个事件的名称,即可使用 On 方法接收此事件。
6868

6969
## 监听消息和事件(以 GroupMessage 为例)
7070

@@ -189,14 +189,14 @@ MessageChain msg = MessageChain().Plain("你好!");
189189
bot.SendMessage(123456_qq, msg);
190190
// 群组消息
191191
bot.SendMessage(123456_gid, msg);
192-
// 临时消息(不能主动发送,但是可以在接收到 TempMessage 事件后发送)
192+
// 临时消息(mirai限制了不能主动发送临时消息,但是可以在接收到 TempMessage 事件后发送)
193193
bot.SendMessage(123456_gid,123456_qq,msg);
194194
```
195195

196196
SendMessage 会返回 MessageId。MessageId可以用于引用回复和撤回消息。
197197

198198
```C++
199-
MessageId msg_id = bot.SendMessage(123456_gid, msg);
199+
MessageId_t msg_id = bot.SendMessage(123456_gid, msg);
200200
// 在群组发送引用回复(好友、临时消息同理)
201201
bot.SendMessage(123456_gid, msg, msg_id);
202202
// 撤回消息
@@ -237,7 +237,9 @@ bot.On<GroupMessage>(GroupMsgProcessor);
237237
238238
# 关于 MessageChain
239239
240-
// MessageChain 目前的使用方法还不够人性化,计划改为类似 STL 容器的用法且支持遍历各类型消息
240+
MessageChain 是 IMessage 的容器。IMessage 的派生类有 PlainMessage、ImageMessage、FlashImageMessage、AtMessage、AtAllMessage等,分别对应文本消息、图片消息、闪照消息、At消息和At全体成员消息。
241+
242+
MessageChain 为了和其他方法保持风格一致,没有使用 stl 风格,你可以使用 `ToVector()` 方法来进行转换,得到类型为 `std::vector<std::shared_ptr<IMessage>>` 的容器。(注意:ToVector() 返回的是引用,对该引用的修改会影响MessageChain的内容)
241243
242244
## 构建一条消息链
243245
@@ -252,11 +254,23 @@ MessageChain msg = MessageChain()
252254
.AtAll();
253255
```
254256

257+
你也可以使用 Add 方法增加成员:
258+
259+
```C++
260+
// 方法1 (等价于.Plain("Hello"))
261+
MessageChain().Add<PlainMessage>("Hello");
262+
// 方法2 加入已存在的 IMessage 派生类
263+
auto plain_msg = PlainMessage("Hello");
264+
MessageChain().Add(plain_msg);
265+
// 方法3 (不推荐,效率可能不如方法1)
266+
MessageChain().Add(PlainMessage("Hello"));
267+
```
268+
255269
## 获得 ImageId 以发送图片
256270
257271
使用 UploadFriendImage 、UploadGroupImage 、 UploadTempImage 方法上传图片,其返回值分别为 FriendImage 、 GroupImage 、TempImage 类型的图片。(FriendImage 、 GroupImage 、TempImage 都是 MiraiImage 的别称)
258272
259-
> 注意: 三种图片的 ID 的格式互不相同,所有不要将各类型的图片混用,比如将 FriendImage 发送给某个群组。虽然 mirai-cpp 没有限制你这样做,但是这一定达不到预期的结果,并且很可能会引发异常。
273+
> 注意: 三种图片的 ID 的格式互不相同,所有不要将各类型的图片混用,比如将 FriendImage 发送给某个群组。虽然 mirai-cpp 没有限制你这样做,但是这一定达不到预期的结果,并且很可能会引发异常。(mirai有计划支持三种图片互相转换,mirai-cpp就等待mirai的好消息了!)
260274
261275
```C++
262276
GroupImage img = UploadGroupImage("D:\\test.png");
@@ -285,20 +299,20 @@ string msg_str = msg.GetPlainText();
285299

286300
## 获取其他消息类型的内容
287301

288-
MessageChain 提供了 GetXXX 的方法,其中 XXX 为消息类型,作用是获得所有该类型的消息,储存在 vector 中。
302+
MessageChain 提供了 `GetAll<T>()``GetFirst<T>()` 方法,可以获取各种类型的消息。使用方法如下:
289303

290-
| 方法签名 | 说明 |
291-
|-------------------------------------|-------------------------------------------------|
292-
| vector< string > GetPlain() const | 获取所有 Plain 类型消息 |
293-
|vector< MiraiImage > GetImage() const| 获取所有 Image、FlashImage 类型消息(图片和闪照) |
294-
| vector< QQ_t > GetAt() const | 获取所有 At 类型消息 |
295-
| vector< int > GetFace() const | 获取所有 Face 类型消息 |
304+
```C++
305+
// 获取所有图片
306+
vector<ImageMessage> img1 = messageChain.GetAll<ImageMessage>();
307+
// 获取第一张图片
308+
ImageMessage img2 = messageChain.GetFirst<ImageMessage>();
309+
```
296310

297311
## 其他函数
298312

299-
GetMessageId 方法可以获得这条消息的 MessageId。
313+
MessageId 方法可以获得这条消息的 MessageId。
300314

301-
GetTimestamp 方法可以获得这条消息的时间戳(类型为 int64_t )。
315+
Timestamp 方法可以获得这条消息的时间戳(类型为 int64_t )。
302316

303317
# 更多内容
304318

@@ -321,7 +335,6 @@ GetTimestamp 方法可以获得这条消息的时间戳(类型为 int64_t )。
321335
| RecallEvent.cpp | 处理其他人撤回消息的事件 |
322336
| Mute.cpp | 和禁言有关的操作 |
323337
| RichMessage.cpp | 发送 JSON、闪照等类型的消息 |
324-
| CompareMessage.cpp | 对比 MessageChain 是否相同 |
325338
| FetchEventsViaHTTP.cpp| 设置通过 HTTP 短轮询获取事件和消息 |
326339
| GroupMemberInfo.cpp | 获取/设置群成员的群名片与群头衔 |
327340

0 commit comments

Comments
 (0)