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

Commit 80713a5

Browse files
authored
Merge pull request #7 from cyanray/dev/cyanray
重构 mirai_bot.hpp 和 mirai_bot.cpp 并添加注释
2 parents c0725b5 + 555df81 commit 80713a5

File tree

5 files changed

+494
-381
lines changed

5 files changed

+494
-381
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.vs/
22
out/
3-
CMakeSettings.json
3+
CMakeSettings.json
4+
Folder.DotSettings.user

examples/CompareMessage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <iostream>
2-
// 使用静态库必须要在引入 mirai.h 前定义这个宏
2+
// 使用静态库必须要在引入 mirai.h 前定义这个宏
33
#define MIRAICPP_STATICLIB
44
#include <mirai.h>
55

examples/FetchEventsViaHTTP.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main()
4343
// 默认使用 WebSocket 拉取事件、消息
4444
// 如果要使用 HTTP 可以在 EventLoop 前执行 UseHTTP
4545
// 记录轮询事件时的错误
46-
bot.UseHTTP().EventLoop([](const char* errMsg)
46+
bot.UseHttp().EventLoop([](const char* errMsg)
4747
{
4848
cout << "轮询事件时出错: " << errMsg << endl;
4949
});

include/mirai_bot.hpp

Lines changed: 230 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,109 +39,312 @@ namespace Cyan
3939
{
4040
public:
4141
MiraiBot();
42+
/**
43+
* \brief
44+
* \param host hostname
45+
* \param port port
46+
*/
4247
MiraiBot(const string& host, int port);
4348
~MiraiBot();
49+
50+
/**
51+
* \brief 获得 mirai-cpp 适配的 API 版本
52+
* \return 用数字表示的版本号 (10605)
53+
*/
4454
int GetRequiredApiVersionInt() const
4555
{
4656
// mirai-api-http v1.6.5
4757
return 10605;
4858
}
59+
60+
/**
61+
* \brief 获得 mirai-cpp 适配的 API 版本
62+
* \return 用字符串表示的版本号(v1.6.5)
63+
*/
4964
string GetRequiredApiVersion() const
5065
{
5166
// mirai-api-http v1.6.5
5267
return "v1.6.5";
5368
}
69+
70+
/**
71+
* \brief 获得 mirai-api-http 插件的版本
72+
* \return 用字符串表示的版本号(v1.6.5)
73+
*/
5474
string GetApiVersion();
75+
/**
76+
* \brief 获得验证后的 SessionKey
77+
* \return SessionKey
78+
*/
5579
string GetSessionKey() const;
80+
/**
81+
* \brief 获得验证后的 QQ 号码
82+
* \return QQ_t
83+
*/
5684
QQ_t GetBotQQ() const;
85+
/**
86+
* \brief 获得用于访问 mirai-api-http 的 HttpClient
87+
* \return httplib::Client
88+
*/
5789
httplib::Client* GetHttpClient();
90+
/**
91+
* \brief 验证 AuthKey 并自动验证 Session Key
92+
* \param authKey AuthKey
93+
* \param qq Bot QQ
94+
* \return 始终为 true (失败会抛出异常)
95+
*/
5896
bool Auth(const string& authKey, QQ_t qq);
97+
/**
98+
* \brief 发送私聊消息
99+
* \param target 发送对象(QQ_t)
100+
* \param messageChain 消息链
101+
* \param msgId 可选, 如果不为空则发送引用消息
102+
* \return 用于引用或撤回的消息 ID (MessageId)
103+
*/
59104
MessageId SendMessage(QQ_t target, const MessageChain& messageChain, MessageId msgId = 0);
105+
/**
106+
* \brief 发送群聊消息
107+
* \param target 发送对象(GID_t)
108+
* \param messageChain 消息链
109+
* \param msgId 可选, 如果不为空则发送引用消息
110+
* \return 用于引用或撤回的消息 ID (MessageId)
111+
*/
60112
MessageId SendMessage(GID_t target, const MessageChain& messageChain, MessageId msgId = 0);
113+
/**
114+
* \brief 发送临时消息
115+
* \param gid 群组(GID)
116+
* \param qq 群成员(QQ_t)
117+
* \param messageChain 消息链
118+
* \param msgId 可选, 如果不为空则发送引用消息
119+
* \return 用于引用或撤回的消息 ID (MessageId)
120+
*/
61121
MessageId SendMessage(GID_t gid, QQ_t qq, const MessageChain& messageChain, MessageId msgId = 0);
122+
/**
123+
* \brief 上传可以发送给好友的图片
124+
* \param fileName 文件名
125+
* \return 好友图片
126+
*/
62127
FriendImage UploadFriendImage(const string& fileName);
128+
/**
129+
* \brief 上传可以发送给群组的图片
130+
* \param fileName 文件名
131+
* \return 群组图片
132+
*/
63133
GroupImage UploadGroupImage(const string& fileName);
134+
/**
135+
* \brief 上传可以发送给临时消息的图片
136+
* \param fileName 文件名
137+
* \return 临时消息图片
138+
*/
64139
TempImage UploadTempImage(const string& fileName);
140+
/**
141+
* \brief 获得好友列表
142+
* \return vector<Friend_t>
143+
*/
65144
vector<Friend_t> GetFriendList();
145+
/**
146+
* \brief 获得群组列表
147+
* \return vector<Group_t>
148+
*/
66149
vector<Group_t> GetGroupList();
150+
/**
151+
* \brief 获得群组的群成员列表
152+
* \param target 群组(GID_t)
153+
* \return vector<GroupMember_t>
154+
*/
67155
vector<GroupMember_t> GetGroupMembers(GID_t target);
156+
/**
157+
* \brief 获得群成员的群名片和群头衔信息
158+
* \param gid 群组(GID_t)
159+
* \param memberId 群成员(QQ_t)
160+
* \return GroupMemberInfo
161+
*/
68162
GroupMemberInfo GetGroupMemberInfo(GID_t gid, QQ_t memberId);
163+
/**
164+
* \brief 设置群成员的群名片和群头衔信息
165+
* \param gid 群组(GID_t)
166+
* \param memberId 群成员(QQ_t)
167+
* \param memberInfo 群成员信息
168+
* \return 始终为 true 出错会抛出异常
169+
*/
69170
bool SetGroupMemberInfo(GID_t gid, QQ_t memberId, const GroupMemberInfo& memberInfo);
171+
/**
172+
* \brief 设置群成员的群名片
173+
* \param gid 群组(GID_t)
174+
* \param memberId 群成员(QQ_t)
175+
* \param name 新的群名片
176+
* \return 始终为 true 出错会抛出异常
177+
*/
70178
bool SetGroupMemberName(GID_t gid, QQ_t memberId, const string& name);
179+
/**
180+
* \brief 设置群成员的群头衔
181+
* \param gid 群组(GID_t)
182+
* \param memberId 群成员(QQ_t)
183+
* \param title 新的群头衔
184+
* \return 始终为 true 出错会抛出异常
185+
*/
71186
bool SetGroupMemberSpecialTitle(GID_t gid, QQ_t memberId, const string& title);
187+
/**
188+
* \brief 全体禁言
189+
* \param target 群组(GID_t)
190+
* \return 始终为 true 出错会抛出异常
191+
*/
72192
bool MuteAll(GID_t target);
193+
/**
194+
* \brief 取消全体禁言
195+
* \param target 群组(GID_t)
196+
* \return 始终为 true 出错会抛出异常
197+
*/
73198
bool UnMuteAll(GID_t target);
74-
bool Mute(GID_t GID, QQ_t memberID, unsigned int time_seconds);
75-
bool UnMute(GID_t GID, QQ_t memberID);
76-
bool Kick(GID_t GID, QQ_t memberID, const string& msg = "");
199+
/**
200+
* \brief 禁言群成员
201+
* \param gid 群组(GID_t)
202+
* \param memberId 群成员(QQ_t)
203+
* \param time_seconds 时长(秒)
204+
* \return 始终为 true 出错会抛出异常
205+
*/
206+
bool Mute(GID_t gid, QQ_t memberId, unsigned int time_seconds);
207+
/**
208+
* \brief 取消禁言群成员
209+
* \param gid 群组(GID_t)
210+
* \param memberId 群成员(QQ_t)
211+
* \return 始终为 true 出错会抛出异常
212+
*/
213+
bool UnMute(GID_t gid, QQ_t memberId);
214+
/**
215+
* \brief 将群成员踢出群组
216+
* \param gid 群组(GID_t)
217+
* \param memberId 群成员(QQ_t)
218+
* \param reason_msg 可选, 填写踢人理由, 默认为空
219+
* \return 始终为 true 出错会抛出异常
220+
*/
221+
bool Kick(GID_t gid, QQ_t memberId, const string& reason_msg = "");
222+
/**
223+
* \brief 撤回一条消息
224+
* \param mid 消息ID(MessageId)
225+
* \return 始终为 true 出错会抛出异常
226+
*/
77227
bool Recall(MessageId mid);
228+
/**
229+
* \brief 根据消息ID(MessageId)获取对应的好友消息
230+
* \param mid 消息ID(MessageId)
231+
* \return 始终为 true 出错会抛出异常
232+
*/
78233
FriendMessage GetFriendMessageFromId(MessageId mid);
234+
/**
235+
* \brief 根据消息ID(MessageId)获取对应的群组消息
236+
* \param mid 消息ID(MessageId)
237+
* \return 始终为 true 出错会抛出异常
238+
*/
79239
GroupMessage GetGroupMessageFromId(MessageId mid);
80-
template<typename T>
240+
241+
/**
242+
* \brief 监听事件
243+
* \tparam T 事件类型
244+
* \param ep 事件处理函数
245+
* \return MiraiBot 引用
246+
*/
247+
template <typename T>
81248
MiraiBot& On(const EventProcessor<T>& ep)
82249
{
83250
return OnEventReceived<T>(ep);
84251
}
85-
template<typename T>
252+
253+
/**
254+
* \brief 监听事件
255+
* \tparam T 事件类型
256+
* \param ep 事件处理函数
257+
* \return MiraiBot 引用
258+
*/
259+
template <typename T>
86260
MiraiBot& OnEventReceived(const EventProcessor<T>& ep);
87-
void inline static SleepSeconds(int sec)
261+
262+
/**
263+
* \brief 睡眠当前线程
264+
* \param sec 时长(秒)
265+
*/
266+
void static SleepSeconds(int sec)
88267
{
89268
std::this_thread::sleep_for(std::chrono::seconds(sec));
90269
}
91-
void inline static SleepMilliseconds(int ms)
270+
271+
/**
272+
* \brief 睡眠当前线程
273+
* \param ms 时长(毫秒)
274+
*/
275+
void static SleepMilliseconds(int ms)
92276
{
93277
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
94278
}
279+
280+
/**
281+
* \brief 设置缓存消息的条数(默认为 4096 条, 过小可能会导致撤回/引用失败)
282+
* \param cacheSize 缓存消息的条数
283+
* \return MiraiBot 引用
284+
*/
95285
MiraiBot& SetCacheSize(int cacheSize);
286+
/**
287+
* \brief 使用WebSocket获取消息和事件
288+
* \return MiraiBot 引用
289+
*/
96290
MiraiBot& UseWebSocket();
97-
MiraiBot& UseHTTP();
291+
/**
292+
* \brief 使用HTTP轮询获取消息和事件
293+
* \return MiraiBot 引用
294+
*/
295+
MiraiBot& UseHttp();
296+
/**
297+
* \brief 阻塞当前线程,轮询/等待消息
298+
* \param errLogger 可选, 错误信息处理函数
299+
*/
98300
void EventLoop(function<void(const char*)> errLogger = nullptr);
99301
private:
302+
// 私有成员函数
100303
bool SessionVerify();
101304
bool SessionRelease();
102305
bool SessionConfigure(int cacheSize, bool enableWebsocket);
103-
unsigned int FetchEvents_HTTP(unsigned int count = 10);
104-
void FetchEvents_WS();
105-
void ProcessEvents(const nlohmann::json& ele);
106-
template<typename T>
107-
inline WeakEvent MakeWeakEvent(const nlohmann::json& json_);
108-
WeakEvent CreateEvent(MiraiEvent mirai_event, const nlohmann::json& json_);
306+
unsigned int FetchEventsHttp(unsigned int count = 10);
307+
void FetchEventsWs();
308+
void ProcessEvents(const json& ele);
309+
template <typename T>
310+
WeakEvent MakeWeakEvent(const json& json);
311+
WeakEvent CreateEvent(MiraiEvent miraiEvent, const json& json);
109312
bool Release() noexcept;
110-
inline string ReadFile(const string& filename);
111-
313+
static inline string ReadFile(const string& filename);
314+
// 私有成员变量
315+
string host_;
316+
int port_;
112317
string authKey_;
113318
QQ_t qq_;
114319
string sessionKey_;
115-
string host_;
116-
int port_;
117320
int cacheSize_;
118321
bool ws_enabled_;
119322
httplib::Client http_client_;
120-
unordered_map<MiraiEvent, function<void(WeakEvent)> > processors_;
121323
ThreadPool pool_;
324+
unordered_map<MiraiEvent, function<void(WeakEvent)>> processors_;
122325
};
123326

124-
template<typename T>
125-
inline MiraiBot& MiraiBot::OnEventReceived(const EventProcessor<T>& ep)
327+
template <typename T>
328+
MiraiBot& MiraiBot::OnEventReceived(const EventProcessor<T>& ep)
126329
{
127-
processors_.insert({ GetEventType<T>(),
330+
processors_.insert({
331+
GetEventType<T>(),
128332
[=](WeakEvent we)
129333
{
130334
ep(*(std::dynamic_pointer_cast<T>(we)));
131335
}
132-
});
336+
});
133337
return *this;
134338
}
135339

136-
template<typename T>
137-
inline WeakEvent MiraiBot::MakeWeakEvent(const nlohmann::json& json_)
340+
template <typename T>
341+
WeakEvent MiraiBot::MakeWeakEvent(const json& json)
138342
{
139343
std::shared_ptr<T> e = std::make_shared<T>();
140344
e->SetMiraiBot(this);
141-
e->Set(json_);
345+
e->Set(json);
142346
return std::dynamic_pointer_cast<Serializable>(e);
143347
}
144-
145348
} // namespace Cyan
146349

147350
#endif // !mirai_cpp__mirai_bot_hpp_H_

0 commit comments

Comments
 (0)