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

Commit 82b0d3d

Browse files
committed
更新文档;
1 parent ce19b18 commit 82b0d3d

File tree

1 file changed

+12
-246
lines changed

1 file changed

+12
-246
lines changed

README.md

Lines changed: 12 additions & 246 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99
## 项目依赖
1010

11-
1. **libcurl** HTTP 协议
11+
> 本项目所有依赖都为 Header-Only 库,已经内置到本项目的 third-party 文件夹中
1212
13-
2. [**nlohmann/json**](https://github.com/nlohmann/json) 解析 JSON 数据。
13+
1. [**cpp-httplib**](https://github.com/yhirose/cpp-httplib) A C++ header-only HTTP/HTTPS server and client library.
1414

15-
3. [**CURLWrapper**](https://github.com/cyanray/CURLWrapper)**libcurl** 的封装
15+
2. [**nlohmann/json**](https://github.com/nlohmann/json) 解析 JSON 数据
1616

17-
4. **boost/asio/thread_pool** 异步处理消息
17+
3. [**ThreadPool**](https://github.com/cyanray/CURLWrapper) 线程池库,用于异步处理消息
1818

1919
## 项目文档
2020

@@ -24,164 +24,6 @@
2424

2525
### 1. 快速尝试
2626

27-
<details>
28-
29-
(以下内容基于 Windows 10 平台,使用 Visual Studio 2019 作为开发软件。)
30-
31-
本项目使用了 4 个第三方项目,其中 **CURLWrapper** 已经嵌入到本项目,而 **libcurl****boost/asio** 以及 **nlohmann/json** 需要额外安装。
32-
33-
有很多方法可以在你的电脑上下载并安装这几个库,这里介绍一种更不容易出错的方法。
34-
35-
在这一切开始之前,你需要下载并安装 [**Git for windows**](https://gitforwindows.org/), 如果你已经安装并且很熟悉它,那么可以略过这个步骤。如果你不熟悉,在这之后可以去了解一下什么是 **Git**
36-
37-
然后,我们需要安装 [**vcpkg**](https://github.com/microsoft/vcpkg) , 这是一个来自微软的跨平台的 C++ 库管理器。如果你已经安装了 **vcpkg** 并且很熟悉它,那么可以略过这个步骤。
38-
39-
#### (1) 安装 **vcpkg** (如果你已经安装则可以略过)
40-
41-
1. 打开 Powershell ,找到一个合适的位置,执行以下命令:
42-
43-
```powershell
44-
git clone https://github.com/Microsoft/vcpkg.git
45-
cd vcpkg
46-
.\bootstrap-vcpkg.bat
47-
```
48-
49-
2. 如果上面的代码执行无误,那么 **vcpkg** 已经成功编译。执行下面的命令让 **Visual Studio 2019****vcpkg** 相关联
50-
51-
```powershell
52-
.\vcpkg integrate install
53-
```
54-
55-
3. 如果你使用的中文 Visual Studio,因为没有安装英文语言包,vcpkg 可能会无法正常工作。请打开 Visual Studio Installer 安装英文语言包(只需要安装英文语言包,不需要把 Visual Studio 切换为英文)
56-
57-
![通过 Visual Studio Install 安装英文语言包](./doc/pic/install_vs_eng_pkg.png)
58-
59-
60-
#### (2) 使用 **vcpkg** 安装 **mirai-cpp**
61-
62-
这一步稍微复杂,你需要执行(这里一定要在 **Powershell** 里面执行):
63-
64-
```powershell
65-
git clone https://github.com/cyanray/mirai-cpp-vcpkg-port.git tmp ; mv tmp/* ports/ ; rm -Recurse -Force tmp
66-
67-
./vcpkg install mirai-cpp
68-
69-
# 对于 x64 的项目,还需要执行:
70-
#./vcpkg mirai-cpp:x64-windows
71-
```
72-
73-
耐心等待,上面的指令会帮你安装 mirai-cpp 以及它的依赖项目。
74-
75-
#### (3) 在 **Visual Studio** 中创建一个项目,开始使用
76-
77-
尝试以下代码:
78-
79-
```c++
80-
#include <iostream>
81-
#include <mirai.h>
82-
83-
int main()
84-
{
85-
using namespace std;
86-
using namespace Cyan;
87-
system("chcp 65001");
88-
MiraiBot bot("127.0.0.1",8080);
89-
while (true)
90-
{
91-
try
92-
{
93-
bot.Auth("InitKeyVl0CEUzZ", 2110000000_qq);
94-
break;
95-
}
96-
catch (const std::exception & ex)
97-
{
98-
cout << ex.what() << endl;
99-
}
100-
}
101-
cout << "Bot Working..." << endl;
102-
103-
bot.On<FriendMessage>(
104-
[&](FriendMessage m)
105-
{
106-
// bot.SendMessage(fm.Sender.QQ, fm.MessageChain);
107-
m.Reply(m.MessageChain);
108-
});
109-
110-
bot.On<GroupMessage>(
111-
[&](GroupMessage m)
112-
{
113-
// bot.SendMessage(gm.Sender.Group.GID, "What is " + gm.MessageChain);
114-
m.QuoteReply("What is " + m.MessageChain);
115-
});
116-
117-
bot.EventLoop();
118-
119-
return 0;
120-
}
121-
```
122-
123-
以上代码你很可能会编译错误,因为 mirai-cpp 的源文件采用了 UTF-8 格式保存。
124-
125-
MSVC 并没有默认启动对 UTF-8 编码的支持。
126-
127-
要想成功通过编译,需要在 C++ 编译器的命令行中添加 **/utf-8** 参数。
128-
129-
1. 在 Visual Studio 开发环境中设置此编译器选项
130-
2. 打开项目“属性页” 对话框。
131-
3. 展开 "配置属性, C/C++ ,命令行" 文件夹。
132-
4. 在 "其他选项" 中, 添加 /utf-8选项以指定首选编码。
133-
5. 选择“确定”以保存更改。
134-
135-
如图:
136-
137-
![操作过程截图](./doc/pic/pic_1.png)
138-
139-
更多信息可以参考: [https://docs.microsoft.com/zh-cn/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8?view=vs-2019](https://docs.microsoft.com/zh-cn/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8?view=vs-2019)
140-
141-
如果一切正常,给你的机器人发消息,他会回复同样的消息给你!
142-
143-
> 提示: 添加了编译参数 **/utf-8** 后,**你的项目****源文件的编码**也必须是 **UTF-8** 编码,不然会出现编译错误。中文操作系统下,Visual Studio 创建的源文件默认为 **GB2312** 编码。所以你需要手动地修改**你的项目**的源文件为 **UTF-8** 编码。
144-
145-
146-
</details>
147-
148-
149-
150-
### 2. 快速尝试2
151-
152-
<details>
153-
154-
#### (1) 安装 **vcpkg** (如果你已经安装则可以略过)
155-
156-
1. 打开 Powershell ,找到一个合适的位置,执行以下命令:
157-
158-
```powershell
159-
git clone https://github.com/Microsoft/vcpkg.git
160-
cd vcpkg
161-
.\bootstrap-vcpkg.bat
162-
```
163-
164-
2. 如果上面的代码执行无误,那么 **vcpkg** 已经成功编译。执行下面的命令让 **Visual Studio 2019****vcpkg** 相关联
165-
166-
```powershell
167-
.\vcpkg integrate install
168-
```
169-
170-
3. 如果你使用的中文 Visual Studio,因为没有安装英文语言包,vcpkg 可能会无法正常工作。请打开 Visual Studio Installer 安装英文语言包(只需要安装英文语言包,不需要把 Visual Studio 切换为英文)
171-
172-
![通过 Visual Studio Install 安装英文语言包](./doc/pic/install_vs_eng_pkg.png)
173-
174-
175-
#### (2) 使用 **vcpkg** 安装 **mirai-cpp** 的依赖项目
176-
177-
```powershell
178-
./vcpkg install nlohmann-json curl boost-asio
179-
```
180-
181-
耐心等待,上面的指令会帮你安装 mirai-cpp 的依赖项目。
182-
183-
#### (3) 直接开始体验 mirai-cpp
184-
18527
将本仓库克隆到合适的位置
18628

18729
```powershell
@@ -194,111 +36,35 @@ git clone https://github.com/cyanray/mirai-cpp.git
19436

19537
![使用 VS 直接打开 mirai-cpp 文件夹](./doc/pic/vs.png)
19638

197-
之后,需要设置一下 CMake Toolchain File,按照图中的步骤操作。
198-
199-
> 提示: 把图中 `E:/OpenSource/vcpkg/` 替换为你的 vcpkg 文件夹的路径。比如替换成 `D:/vcpkg/scripts/buildsystems/vcpkg.cmake`
39+
如果一切顺利,你可以直接运行我写好的示例。
20040

201-
![设置 CMake](./doc/pic/vs2.png)
202-
203-
如果一切顺利,你就可以直接运行我写好的示例。
41+
在我的示例的基础上进行修改,即可编写你自己的插件。
20442

20543
![开始运行 examples](./doc/pic/vs3.png)
20644

207-
</details>
20845

20946
### 3. 其他使用方式
21047

211-
#### (1) 更新 mirai-cpp
212-
213-
在更新 mirai-cpp 之前,需要先删除已经安装的 mirai-cpp
214-
215-
Windows:
216-
217-
```powershell
218-
./vcpkg remove mirai-cpp mirai-cpp:x64-windows
219-
```
220-
221-
Linux:
222-
223-
```bash
224-
./vcpkg remove mirai-cpp
225-
```
226-
227-
删除之后,重新安装即可:
228-
229-
Windows:
230-
231-
```powershell
232-
git clone https://github.com/cyanray/mirai-cpp-vcpkg-port.git tmp ; rm -Recurse -Force ports/mirai-cpp ; mv tmp/* ports/ ; rm -Recurse -Force tmp
233-
234-
./vcpkg install mirai-cpp mirai-cpp:x64-windows
235-
```
48+
#### (1) 将程序轻松移植、部署到 Linux 上
23649

237-
Linux:
238-
```bash
239-
git clone https://github.com/cyanray/mirai-cpp-vcpkg-port.git tmp ; rm -rf ports/mirai-cpp ; mv tmp/* ports/ ; rm -rf tmp
240-
./vcpkg install mirai-cpp
241-
```
24250

243-
#### (2) 将程序轻松移植、部署到 Linux 上
244-
245-
<details>
246-
247-
(以下内容基于 “快速尝试2”,请先完成“快速尝试2”。)
51+
(以下内容基于 “快速尝试”,请先完成“快速尝试”。)
24852

24953
上面的内容介绍了如何在 Windows 上开发使用 mirai-cpp 的程序,下面来介绍如何将你的程序移植到 Linux 平台,以便将程序部署到 Linux 服务器上。
25054

25155
为了易于讲解与操作,以下内容在 **WSL** (**W**indows **S**ubsystem for **L**inux) 上进行。这里不对如何安装 WSL 进行说明,关于如何安装 WSL 还请自行查阅资料。
25256

253-
1. 在 WSL 上安装 vcpkg
254-
255-
步骤与上文类似,但是略有不同:
256-
257-
```bash
258-
git clone https://github.com/Microsoft/vcpkg.git
259-
cd vcpkg
260-
./bootstrap-vcpkg.sh
261-
```
262-
263-
![在 WSL 上安装 vcpkg](./doc/pic/install_vcpkg_on_linux.png)
264-
265-
耐心等待,其结果如图所示。
266-
注意,如果执行 ls 发现没有生成 vcpkg 的可执行文件,请尝试重新执行上述的安装指令。(这似乎是个bug)
267-
268-
![在 WSL 上安装 vcpkg2](./doc/pic/install_vcpkg_on_linux2.png)
269-
270-
2. 使用 vcpkg 安装 mirai-cpp
57+
1. 创建针对 WSL 的配置
27158

272-
273-
```bash
274-
git clone https://github.com/cyanray/mirai-cpp-vcpkg-port.git tmp ; mv tmp/* ports/ ; rm -rf tmp
275-
276-
./vcpkg install mirai-cpp
277-
```
278-
279-
![在 WSL 上安装 mirai-cpp](./doc/pic/install_mirai_cpp_on_linux.png)
280-
281-
3. 创建针对 WSL 的配置
282-
283-
打开在 “快速尝试2” 中用到的项目。按照如图所示步骤,创建一个针对 WSL 平台的配置。因为我的 WSL 安装了 GCC 编译器,所以这里选择 **WSL-GCC-Releas**
59+
打开在 “快速尝试” 中用到的项目。按照如图所示步骤,创建一个针对 WSL 平台的配置。因为我的 WSL 安装了 GCC 编译器,所以这里选择 **WSL-GCC-Releas**
28460

28561
![创建WSL-GCC平台配置1](./doc/pic/vs_configure_linux_project.png)
28662

287-
和 “快速尝试2” 一样的操作,配置 CMake Toolchain File。但是这次的路径要填写 WSL 上的路径。
288-
289-
![创建WSL-GCC平台配置2](./doc/pic/vs_configure_linux_project2.png)
290-
291-
Enjoy it;
292-
293-
</details>
63+
如果一切顺利,等待 CMake 缓存生成成功后,即可编译出 Linux 平台的可执行文件。
29464

29565
## 常见问题
29666

297-
### 1. 使用 vcpkg 安装 mirai-cpp 时出错
298-
299-
首先检查是不是网络错误导致下载文件失败。这种错误只要重新执行 `./vcpkg install mirai-cpp` 。如果你有可以加速网络的代理服务器,可以添加 **HTTP_PROXY****HTTPS_PROXY** 环境变量,以加速 **vcpkg** 的下载。
300-
301-
![配置环境变量](./doc/pic/env.png)
67+
未完待续……
30268

30369
## 代码风格
30470

0 commit comments

Comments
 (0)