Skip to content
This repository was archived by the owner on Aug 16, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

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

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

## 如何使用

Expand Down
91 changes: 68 additions & 23 deletions doc/使用说明.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,18 @@ bot.EventLoop() 方法会阻塞当前线程,内含死循环不断轮询新的
bot.EventLoop([](const char* errMsg){/*...*/ });
```

## 注意事项⚠⚠

mirai-cpp 监听到一个事件后,会将该事件加入到线程池中进行处理。因此你需要特别注意多线程环境下数据冲突的问题。(如访问全局变量)

mirai-cpp 的线程池默认是 4 条线程,线程数量可以通过 MiraiBot 的第三个参数进行调整。

```c++
// 16 条事件处理线程
MiraiBot bot("127.0.0.1", 539, 16);
```


# 如何发送消息、引用回复、撤回消息

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

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

# 更多内容
# 指令系统

建议参考 /examples 的源代码,其中的 examples 会随着 mirai-cpp 的更新而更新。
mirai-api-http 提供了一个指令系统,mirai-cpp 对该系统进行了适配。
关于该系统的介绍请查阅[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`中的内容。

(强烈建议将 examples 都阅读一遍,因为精力关系我没能把所有函数都写在文档上)
mirai-cpp 将指令抽象为一个事件,因此你可以像处理事件一样处理指令。

```C++
// 使用 On 方法接收指令事件
bot.On<Command>(
[&](Command e)
{
cout << "收到指令: " << e.CommandName << ", "
<< "发送者: " << e.Sender.ToInt64() << ", "
<< "发送群: " << e.GroupId.ToInt64() << endl;
cout << "参数:" << endl;
for (const auto& arg : e.Args)
cout << arg << " ";
cout << endl;

// 检查指令的发送者是不是 Manager
if (e.SenderIsManager())
{
bot.SendMessage(e.GroupId, MessageChain().Plain("执行指令: ").Plain(e.CommandName));
}

});
```

| 文件名 | 说明 |
|----------------------|------------------------------|
| RepeatMessage.cpp | 简单的复读机器人 |
| SendImageMessage.cpp | 发送图片示例 |
| BotEvents.cpp | 处理有关Bot相关的事件 |
| GetFriendList.cpp | 打印Bot的好友列表 |
| GetGroupList.cpp | 打印Bot的群组列表 |
| MemberJoinEvent.cpp | 处理新成员加入群的申请和事件 |
| MemberLeaveEvent.cpp | 处理成员退出群的事件 |
| MessageType.cpp | 获取/处理各种类型的消息示例 |
| NewFriendEvent.cpp | 处理好友申请 |
| Recall.cpp | 撤回消息示例 |
| RecallEvent.cpp | 处理其他人撤回消息的事件 |
| Mute.cpp | 和禁言有关的操作 |
| RichMessage.cpp | 发送 JSON、闪照等类型的消息 |
| FetchEventsViaHTTP.cpp| 设置通过 HTTP 短轮询获取事件和消息 |
| GroupMemberInfo.cpp | 获取/设置群成员的群名片与群头衔 |


# 关于异常

MiraiBot 中的方法几乎都会抛出异常,建议捕捉起来。
MiraiBot 中的方法几乎都会抛出异常,建议捕捉起来。因为在事件处理函数中出现的异常不一定会导致程序崩溃,出现问题会难以调试。

EventLoop 方法不会抛出异常,它会捕捉异常,并在 cerr 中输出。
EventLoop 方法不会抛出异常,它会捕捉获取消息时的异常,并在 cerr 中输出。
如果希望自定义 EventLoop 处理错误信息的方式,可以尝试这样写:

```C++
Expand All @@ -363,4 +381,31 @@ bot.On<GroupMessage>(
MiraiBot& bot = m.GetMiraiBot();
bot.SendMessage(/*...*/);
});
```
```


# 更多内容

建议参考 /examples 的源代码,其中的 examples 会随着 mirai-cpp 的更新而更新。

(强烈建议将 examples 都阅读一遍,因为精力关系我没能把所有函数都写在文档上)

| 文件名 | 说明 |
|----------------------|------------------------------|
| RepeatMessage.cpp | 简单的复读机器人 |
| SendImageMessage.cpp | 发送图片示例 |
| VoiceMessage.cpp | 发送语音消息示例 |
| BotEvents.cpp | 处理有关Bot相关的事件 |
| GetFriendList.cpp | 打印Bot的好友列表 |
| GetGroupList.cpp | 打印Bot的群组列表 |
| MemberJoinEvent.cpp | 处理新成员加入群的申请和事件 |
| MemberLeaveEvent.cpp | 处理成员退出群的事件 |
| MessageType.cpp | 获取/处理各种类型的消息 |
| NewFriendEvent.cpp | 处理好友申请 |
| Recall.cpp | 撤回消息 |
| RecallEvent.cpp | 处理其他人撤回消息的事件 |
| Mute.cpp | 和禁言有关的操作 |
| RichMessage.cpp | 发送 JSON、闪照等类型的消息 |
| FetchEventsViaHTTP.cpp| 设置通过 HTTP 短轮询获取事件和消息 |
| GroupMemberInfo.cpp | 获取/设置群成员的群名片与群头衔 |
| Command.cpp | 指令系统相关的操作 |