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

Commit 6033b09

Browse files
authored
Merge pull request #44 from cyanray/dev/cyanray
更新: README和使用说明;
2 parents eb596d6 + de81022 commit 6033b09

File tree

2 files changed

+71
-24
lines changed

2 files changed

+71
-24
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818

1919
推荐阅读 examples 目录下的示例以了解使用方法。
2020

21-
| 文件名 | 说明 |
21+
| 文件名 | 说明 |
2222
|----------------------|------------------------------|
2323
| RepeatMessage.cpp | 简单的复读机器人 |
2424
| SendImageMessage.cpp | 发送图片示例 |
25+
| VoiceMessage.cpp | 发送语音消息示例 |
2526
| BotEvents.cpp | 处理有关Bot相关的事件 |
2627
| GetFriendList.cpp | 打印Bot的好友列表 |
2728
| GetGroupList.cpp | 打印Bot的群组列表 |
@@ -35,6 +36,7 @@
3536
| RichMessage.cpp | 发送 JSON、闪照等类型的消息 |
3637
| FetchEventsViaHTTP.cpp| 设置通过 HTTP 短轮询获取事件和消息 |
3738
| GroupMemberInfo.cpp | 获取/设置群成员的群名片与群头衔 |
39+
| Command.cpp | 指令系统相关的操作 |
3840

3941
## 如何使用
4042

doc/使用说明.md

Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,18 @@ bot.EventLoop() 方法会阻塞当前线程,内含死循环不断轮询新的
179179
bot.EventLoop([](const char* errMsg){/*...*/ });
180180
```
181181
182+
## 注意事项⚠⚠
183+
184+
mirai-cpp 监听到一个事件后,会将该事件加入到线程池中进行处理。因此你需要特别注意多线程环境下数据冲突的问题。(如访问全局变量)
185+
186+
mirai-cpp 的线程池默认是 4 条线程,线程数量可以通过 MiraiBot 的第三个参数进行调整。
187+
188+
```c++
189+
// 16 条事件处理线程
190+
MiraiBot bot("127.0.0.1", 539, 16);
191+
```
192+
193+
182194
# 如何发送消息、引用回复、撤回消息
183195

184196
mirai 支持好友消息、群组消息和临时消息。在 mirai-cpp 中要发送这些消息统一使用 SendMessage 方法。
@@ -314,36 +326,42 @@ MessageId 方法可以获得这条消息的 MessageId。
314326

315327
Timestamp 方法可以获得这条消息的时间戳(类型为 int64_t )。
316328

317-
# 更多内容
329+
# 指令系统
318330

319-
建议参考 /examples 的源代码,其中的 examples 会随着 mirai-cpp 的更新而更新。
331+
mirai-api-http 提供了一个指令系统,mirai-cpp 对该系统进行了适配。
332+
关于该系统的介绍请查阅[mirai-api-http的文档](https://github.com/project-mirai/mirai-api-http#%E6%8F%92%E4%BB%B6%E7%9B%B8%E5%85%B3console%E7%9B%B8%E5%85%B3)。具体使用方法请参考`examples/Command.cpp`中的内容。
320333

321-
(强烈建议将 examples 都阅读一遍,因为精力关系我没能把所有函数都写在文档上)
334+
mirai-cpp 将指令抽象为一个事件,因此你可以像处理事件一样处理指令。
335+
336+
```C++
337+
// 使用 On 方法接收指令事件
338+
bot.On<Command>(
339+
[&](Command e)
340+
{
341+
cout << "收到指令: " << e.CommandName << ", "
342+
<< "发送者: " << e.Sender.ToInt64() << ", "
343+
<< "发送群: " << e.GroupId.ToInt64() << endl;
344+
cout << "参数:" << endl;
345+
for (const auto& arg : e.Args)
346+
cout << arg << " ";
347+
cout << endl;
348+
349+
// 检查指令的发送者是不是 Manager
350+
if (e.SenderIsManager())
351+
{
352+
bot.SendMessage(e.GroupId, MessageChain().Plain("执行指令: ").Plain(e.CommandName));
353+
}
354+
355+
});
356+
```
322357
323-
| 文件名 | 说明 |
324-
|----------------------|------------------------------|
325-
| RepeatMessage.cpp | 简单的复读机器人 |
326-
| SendImageMessage.cpp | 发送图片示例 |
327-
| BotEvents.cpp | 处理有关Bot相关的事件 |
328-
| GetFriendList.cpp | 打印Bot的好友列表 |
329-
| GetGroupList.cpp | 打印Bot的群组列表 |
330-
| MemberJoinEvent.cpp | 处理新成员加入群的申请和事件 |
331-
| MemberLeaveEvent.cpp | 处理成员退出群的事件 |
332-
| MessageType.cpp | 获取/处理各种类型的消息示例 |
333-
| NewFriendEvent.cpp | 处理好友申请 |
334-
| Recall.cpp | 撤回消息示例 |
335-
| RecallEvent.cpp | 处理其他人撤回消息的事件 |
336-
| Mute.cpp | 和禁言有关的操作 |
337-
| RichMessage.cpp | 发送 JSON、闪照等类型的消息 |
338-
| FetchEventsViaHTTP.cpp| 设置通过 HTTP 短轮询获取事件和消息 |
339-
| GroupMemberInfo.cpp | 获取/设置群成员的群名片与群头衔 |
340358
341359
342360
# 关于异常
343361
344-
MiraiBot 中的方法几乎都会抛出异常,建议捕捉起来。
362+
MiraiBot 中的方法几乎都会抛出异常,建议捕捉起来。因为在事件处理函数中出现的异常不一定会导致程序崩溃,出现问题会难以调试。
345363
346-
EventLoop 方法不会抛出异常,它会捕捉异常,并在 cerr 中输出。
364+
EventLoop 方法不会抛出异常,它会捕捉获取消息时的异常,并在 cerr 中输出。
347365
如果希望自定义 EventLoop 处理错误信息的方式,可以尝试这样写:
348366
349367
```C++
@@ -363,4 +381,31 @@ bot.On<GroupMessage>(
363381
MiraiBot& bot = m.GetMiraiBot();
364382
bot.SendMessage(/*...*/);
365383
});
366-
```
384+
```
385+
386+
387+
# 更多内容
388+
389+
建议参考 /examples 的源代码,其中的 examples 会随着 mirai-cpp 的更新而更新。
390+
391+
(强烈建议将 examples 都阅读一遍,因为精力关系我没能把所有函数都写在文档上)
392+
393+
| 文件名 | 说明 |
394+
|----------------------|------------------------------|
395+
| RepeatMessage.cpp | 简单的复读机器人 |
396+
| SendImageMessage.cpp | 发送图片示例 |
397+
| VoiceMessage.cpp | 发送语音消息示例 |
398+
| BotEvents.cpp | 处理有关Bot相关的事件 |
399+
| GetFriendList.cpp | 打印Bot的好友列表 |
400+
| GetGroupList.cpp | 打印Bot的群组列表 |
401+
| MemberJoinEvent.cpp | 处理新成员加入群的申请和事件 |
402+
| MemberLeaveEvent.cpp | 处理成员退出群的事件 |
403+
| MessageType.cpp | 获取/处理各种类型的消息 |
404+
| NewFriendEvent.cpp | 处理好友申请 |
405+
| Recall.cpp | 撤回消息 |
406+
| RecallEvent.cpp | 处理其他人撤回消息的事件 |
407+
| Mute.cpp | 和禁言有关的操作 |
408+
| RichMessage.cpp | 发送 JSON、闪照等类型的消息 |
409+
| FetchEventsViaHTTP.cpp| 设置通过 HTTP 短轮询获取事件和消息 |
410+
| GroupMemberInfo.cpp | 获取/设置群成员的群名片与群头衔 |
411+
| Command.cpp | 指令系统相关的操作 |

0 commit comments

Comments
 (0)