This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
RepeatMessage.cpp
115 lines (102 loc) · 2.61 KB
/
RepeatMessage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
// 使用静态库必须要在引入 mirai.h 前定义这个宏
#define MIRAICPP_STATICLIB
// 按需引用头文件
// 你也可以使用 #include <mirai.h> 引用所有头文件(可能导致编译缓慢)
#include <mirai/MiraiBot.hpp>
#include <mirai/events/OtherClientMessage.hpp>
#include <mirai/events/FriendSyncMessage.hpp>
using namespace std;
using namespace Cyan;
int main(int argc, char* argv[])
{
// 源文件使用 UTF-8 编码保存,在 Windows 上需要切换代码页才不会显示乱码
#if defined(WIN32) || defined(_WIN32)
system("chcp 65001");
#endif
MiraiBot bot;
SessionOptions opts = SessionOptions::FromCommandLine(argc, argv);
// 自动重试地与 mirai-api-http 建立连接
while (true)
{
try
{
bot.Connect(opts);
break;
}
catch (const std::exception& ex)
{
cout << ex.what() << endl;
}
MiraiBot::SleepSeconds(1);
}
// 检查一下版本
try
{
string mah_version = bot.GetMiraiApiHttpVersion();
string mc_version = bot.GetMiraiCppVersion();
cout << "mirai-api-http 的版本: " << mah_version
<< "; mirai-cpp 的版本: " << mc_version << "; " << endl;
if (mah_version != mc_version)
{
cout << "Warning: 你的 mirai-api-http 插件的版本与 mirai-cpp 的版本不同,可能存在兼容性问题。" << endl;
}
}
catch (const std::exception& ex)
{
cout << ex.what() << endl;
}
cout << "Bot Working..." << endl;
bot.On<Message>(
[&](Message m)
{
cout << int64_t(m.Sender) << " 发来一条消息." << m.MessageChain.ToString() << endl;
// m.Reply(m.MessageChain);
});
bot.On<OtherClientMessage>([&](OtherClientMessage m)
{
cout << m.MessageChain.ToString() << endl;
});
bot.On<FriendSyncMessage>([&](FriendSyncMessage m)
{
cout << m.MessageChain.ToString() << endl;
});
bot.On<LostConnection>([&](LostConnection e)
{
cout << e.ErrorMessage << " (" << e.Code << ")" << endl;
while (true)
{
try
{
cout << "尝试与 mirai-api-http 重新建立连接..." << endl;
bot.Reconnect();
break;
}
catch (const std::exception& ex)
{
cout << ex.what() << endl;
}
MiraiBot::SleepSeconds(1);
}
cout << "成功与 mirai-api-http 重新建立连接!" << endl;
});
bot.On<EventParsingError>([&](EventParsingError e)
{
try
{
e.Rethrow();
}
catch (const std::exception& ex)
{
cout << "解析事件时出现错误: " << ex.what() << endl;
}
});
string command;
while (cin >> command)
{
if (command == "exit") break;
}
// 程序结束前必须释放 Session, 否则会导致 mirai-api-http 内存泄漏
bot.Disconnect();
return 0;
}