|
14 | 14 |
|
15 | 15 | 3. [**CURLWrapper**](https://github.com/cyanray/CURLWrapper) 对 **libcurl** 的封装。 |
16 | 16 |
|
17 | | -## 其他 |
| 17 | +## 如何使用 |
| 18 | + |
| 19 | +### 1. 快速尝试 |
| 20 | + |
| 21 | +<details> |
| 22 | + |
| 23 | +(以下内容基于 Windows 10 平台,使用 Visual Studio 2019 作为开发软件。) |
| 24 | + |
| 25 | +本项目使用了 3 个第三方项目,其中 **CURLWrapper** 已经嵌入到本项目,而 **libcurl** 以及 **nlohmann/json** 需要额外安装。 |
| 26 | + |
| 27 | +有很多方法可以在你的电脑上下载并安装 **libcurl** 和 **nlohmann/json** 库,这里介绍一种更不容易出错的方法。 |
| 28 | + |
| 29 | +在这一切开始之前,你需要下载并安装 [**Git for windows**](https://gitforwindows.org/), 如果你已经安装并且很熟悉它,那么可以略过这个步骤。如果你不熟悉,在这之后可以去了解一下什么是 **Git** 。 |
| 30 | + |
| 31 | +然后,我们需要安装 [**vcpkg**](https://github.com/microsoft/vcpkg) , 这是一个来自微软的跨平台的 C++ 库管理器。如果你已经安装了 **vcpkg** 并且很熟悉它,那么可以略过这个步骤。 |
| 32 | + |
| 33 | +#### (1) 安装 **vcpkg** (如果你已经安装则可以略过) |
| 34 | + |
| 35 | +1. 打开 Powershell ,找到一个合适的位置,执行以下命令: |
| 36 | + |
| 37 | +```shell |
| 38 | +git clone https://github.com/Microsoft/vcpkg.git |
| 39 | +cd vcpkg |
| 40 | +.\bootstrap-vcpkg.bat |
| 41 | +``` |
| 42 | + |
| 43 | +2. 如果上面的代码执行无误,那么 **vcpkg** 已经成功编译。执行下面的命令让 **Visual Studio 2019** 与 **vcpkg** 相关联 |
| 44 | + |
| 45 | +```shell |
| 46 | +.\vcpkg integrate install |
| 47 | +``` |
| 48 | + |
| 49 | +#### (2) 使用 **vcpkg** 安装 libcurl 和 **nlohmann/json** |
| 50 | + |
| 51 | +完成这一步你只需执行: |
| 52 | + |
| 53 | +```shell |
| 54 | +./vcpkg install curl nlohmann-json |
| 55 | +``` |
| 56 | + |
| 57 | +#### (3) 使用 **vcpkg** 安装 **mirai-cpp** |
| 58 | + |
| 59 | +这一步稍微复杂,你需要执行: |
| 60 | + |
| 61 | +```shell |
| 62 | +git clone https://github.com/cyanray/mirai-cpp-vcpkg-port.git |
| 63 | +./vcpkg install mirai-cpp |
| 64 | +``` |
| 65 | + |
| 66 | +#### (4) 在 **Visual Studio** 中创建一个项目,开始使用 |
| 67 | + |
| 68 | +尝试以下代码: |
| 69 | + |
| 70 | +```c++ |
| 71 | +#include <iostream> |
| 72 | +#include <mirai.hpp> |
| 73 | + |
| 74 | +int main() |
| 75 | +{ |
| 76 | + using namespace std; |
| 77 | + using namespace Cyan; |
| 78 | + MiraiBot bot; |
| 79 | + while (true) |
| 80 | + { |
| 81 | + try |
| 82 | + { |
| 83 | + // InitKeyVl0CEUzZ 改为你的 InitKey, |
| 84 | + // 2110000000 改为你的 bot 的 QQ 号码 |
| 85 | + bot.Auth("InitKeyVl0CEUzZ", 2110000000ll); |
| 86 | + break; |
| 87 | + } |
| 88 | + catch (const std::exception & ex) |
| 89 | + { |
| 90 | + cout << ex.what() << endl; |
| 91 | + } |
| 92 | + } |
| 93 | + cout << "成功登录 bot。" << endl; |
| 94 | + |
| 95 | + |
| 96 | + bot.OnFriendMessageReceived( |
| 97 | + [&](FriendMessage fm) |
| 98 | + { |
| 99 | + bot.SendFriendMessage(fm.Sender.QQ, fm.MessageChain); |
| 100 | + }); |
| 101 | + |
| 102 | + bot.OnGroupMessageReceived( |
| 103 | + [&](GroupMessage gm) |
| 104 | + { |
| 105 | + bot.SendGroupMessage(gm.Sender.Group.GID, "为什么要 " + gm.MessageChain); |
| 106 | + }); |
| 107 | + |
| 108 | + try |
| 109 | + { |
| 110 | + bot.EventLoop(); |
| 111 | + } |
| 112 | + catch (const std::exception & ex) |
| 113 | + { |
| 114 | + cout << ex.what() << endl; |
| 115 | + } |
| 116 | + |
| 117 | + return 0; |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +如果一切正常,给你的机器人发消息,他会回复同样的消息给你! |
| 122 | + |
| 123 | +</details> |
| 124 | + |
| 125 | +### 2. 其他使用方式 |
18 | 126 |
|
19 | 127 | 未完待续…… |
| 128 | + |
| 129 | +## 代码风格 |
| 130 | + |
| 131 | +本项目的代码使用的是我自己喜欢的代码风格,如果你有更好的建议(比如修改为 Google-Style),欢迎提出 issues 或 pull request。 |
0 commit comments