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

Commit ad5f7f0

Browse files
committed
mirai_bot.hpp 添加注释;
1 parent d36dab9 commit ad5f7f0

File tree

2 files changed

+221
-19
lines changed

2 files changed

+221
-19
lines changed

include/mirai_bot.hpp

Lines changed: 218 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,62 +39,264 @@ 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);
199+
/**
200+
* \brief 禁言群成员
201+
* \param gid 群组(GID_t)
202+
* \param memberId 群成员(QQ_t)
203+
* \param time_seconds 时长(秒)
204+
* \return 始终为 true 出错会抛出异常
205+
*/
74206
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+
*/
75213
bool UnMute(GID_t gid, QQ_t memberId);
214+
/**
215+
* \brief 将群成员踢出群组
216+
* \param gid 群组(GID_t)
217+
* \param memberId 群成员(QQ_t)
218+
* \param msg 可选, 填写踢人理由, 默认为空
219+
* \return 始终为 true 出错会抛出异常
220+
*/
76221
bool Kick(GID_t gid, QQ_t memberId, const string& 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();
291+
/**
292+
* \brief 使用HTTP轮询获取消息和事件
293+
* \return MiraiBot 引用
294+
*/
97295
MiraiBot& UseHttp();
296+
/**
297+
* \brief 阻塞当前线程,轮询/等待消息
298+
* \param errLogger 可选, 错误信息处理函数
299+
*/
98300
void EventLoop(function<void(const char*)> errLogger = nullptr);
99301
private:
100302
// 私有成员函数
@@ -103,10 +305,10 @@ namespace Cyan
103305
bool SessionConfigure(int cacheSize, bool enableWebsocket);
104306
unsigned int FetchEventsHttp(unsigned int count = 10);
105307
void FetchEventsWs();
106-
void ProcessEvents(const nlohmann::json& ele);
107-
template<typename T>
108-
inline WeakEvent MakeWeakEvent(const nlohmann::json& json);
109-
WeakEvent CreateEvent(MiraiEvent miraiEvent, const nlohmann::json& json);
308+
void ProcessEvents(const json& ele);
309+
template <typename T>
310+
WeakEvent MakeWeakEvent(const json& json);
311+
WeakEvent CreateEvent(MiraiEvent miraiEvent, const json& json);
110312
bool Release() noexcept;
111313
static inline string ReadFile(const string& filename);
112314
// 私有成员变量
@@ -119,30 +321,30 @@ namespace Cyan
119321
bool ws_enabled_;
120322
httplib::Client http_client_;
121323
ThreadPool pool_;
122-
unordered_map<MiraiEvent, function<void(WeakEvent)> > processors_;
324+
unordered_map<MiraiEvent, function<void(WeakEvent)>> processors_;
123325
};
124326

125-
template<typename T>
126-
inline MiraiBot& MiraiBot::OnEventReceived(const EventProcessor<T>& ep)
327+
template <typename T>
328+
MiraiBot& MiraiBot::OnEventReceived(const EventProcessor<T>& ep)
127329
{
128-
processors_.insert({ GetEventType<T>(),
330+
processors_.insert({
331+
GetEventType<T>(),
129332
[=](WeakEvent we)
130333
{
131334
ep(*(std::dynamic_pointer_cast<T>(we)));
132335
}
133-
});
336+
});
134337
return *this;
135338
}
136339

137-
template<typename T>
138-
inline WeakEvent MiraiBot::MakeWeakEvent(const nlohmann::json& json)
340+
template <typename T>
341+
WeakEvent MiraiBot::MakeWeakEvent(const json& json)
139342
{
140343
std::shared_ptr<T> e = std::make_shared<T>();
141344
e->SetMiraiBot(this);
142345
e->Set(json);
143346
return std::dynamic_pointer_cast<Serializable>(e);
144347
}
145-
146348
} // namespace Cyan
147349

148350
#endif // !mirai_cpp__mirai_bot_hpp_H_

src/mirai_bot.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ namespace Cyan
6767
return version;
6868
}
6969

70-
bool MiraiBot::Auth(const string& auth_key, QQ_t qq)
70+
bool MiraiBot::Auth(const string& authKey, QQ_t qq)
7171
{
7272
json data =
7373
{
74-
{ "authKey", auth_key }
74+
{ "authKey", authKey }
7575
};
7676

7777
auto res = http_client_.Post("/auth", data.dump(), "application/json;charset=UTF-8");
@@ -84,7 +84,7 @@ namespace Cyan
8484
if (code == 0)
8585
{
8686
this->sessionKey_ = reJson["session"].get<string>();
87-
this->authKey_ = auth_key;
87+
this->authKey_ = authKey;
8888
this->qq_ = qq;
8989
return SessionVerify();
9090
}

0 commit comments

Comments
 (0)