cpp-mirai-client  v2.6.1
cpp client for mirai-api-http
Client.hpp
浏览该文件的文档.
1// Copyright (C) 2022 Numendacil and contributors
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16#ifndef MIRAI_CLIENT_HPP_
17#define MIRAI_CLIENT_HPP_
18
19#include <filesystem>
20#include <fstream>
21#include <functional>
22#include <limits>
23#include <memory>
24#include <optional>
25#include <shared_mutex>
26#include <string>
27#include <thread>
28#include <type_traits>
29#include <unordered_map>
30#include <variant>
31#include <vector>
32
38
39/// 所有mirai相关的对象的命名空间
40namespace Mirai
41{
42
43namespace Utils
44{
45
46class ThreadPool;
47
48}
49
50/**
51 * @brief 提供与mirai-api-http网络交互的封装
52 *
53 * 所有与mirai的交互都需要通过该类来实现。使用时可以通过调用网络api
54 * 以及注册事件回调函数来完成消息的处理与发送。
55 *
56 * 连接设置保存在 `SessionConfigs` 类中,更改设置后需要重新连接mirai来使新配置生效。
57 *
58 * 所有非常量方法均有可能会抛出异常,异常的种类见 `Exceptions.hpp`。
59 */
61{
62private:
63 // type traits
64 struct traits
65 {
66 template<typename T, typename TypeList> struct is_event_type_;
67
68 template<typename T, size_t... I>
69 struct is_event_type_<T, std::index_sequence<I...>>
70 : public std::disjunction<std::is_same<T, GetEventType_t<EventTypesList[I]>>...>
71 {
72 };
73
74 template<typename T> using is_event_type = is_event_type_<T, std::make_index_sequence<EventTypesList.size()>>;
75
76 template<typename T> struct event_callback_variant_
77 {
78 };
79
80 template<size_t... I> struct event_callback_variant_<std::index_sequence<I...>>
81 {
82 using type = std::variant<std::function<void(GetEventType_t<EventTypesList[I]>)>...>;
83 };
84
85 using EventCallbackVariant = event_callback_variant_<std::make_index_sequence<EventTypesList.size()>>::type;
86
87 template<typename T> struct function_traits;
88
89 template<typename EventType> struct function_traits<std::function<void(EventType)>>
90 {
91 using type = EventType;
92 };
93 };
94
95 template<typename EventType> constexpr static void type_check_()
96 {
97 static_assert(std::is_base_of_v<IEvent<EventType>, EventType>,
98 "EventType is not derived from IEvent<EventType>"); // NOLINT(*-array-to-pointer-decay)
100 "Unsupported event type"); // NOLINT(*-array-to-pointer-decay)
101 };
102
103public:
104 template<typename Event> using EventCallback = std::function<void(Event)>;
105
106protected:
107 // For locking session key
108 mutable std::shared_mutex mtx_;
109
110 std::string SessionKey_{};
111 bool connected_ = false;
112 size_t PoolSize_ = std::thread::hardware_concurrency();
113
114 std::shared_ptr<ILogger> logger_ = std::make_shared<NullLogger>();
115 std::unique_ptr<IAdaptor> adaptor_;
116 std::unique_ptr<Utils::ThreadPool> pool_;
117
122
123 using EventHandler = traits::EventCallbackVariant;
124 std::unordered_map<EventTypes, EventHandler> EventHandlers_{};
125
126 std::string GetSessionKey_() const
127 {
128 std::shared_lock<std::shared_mutex> lk(this->mtx_);
129 return this->SessionKey_;
130 }
131
132public:
134 MiraiClient(std::unique_ptr<IAdaptor> adaptor);
135
136 template<typename Adaptor, typename... Args,
137 typename std::enable_if_t<std::is_base_of_v<IAdaptor, Adaptor>, int> = 0>
138 MiraiClient(Args&&... args)
139 : MiraiClient(std::make_unique<Adaptor>(std::forward<Args>(args)...))
140 {
141 }
142
143 MiraiClient(const MiraiClient&) = delete;
145 MiraiClient(MiraiClient&& rhs) noexcept = delete;
146 MiraiClient& operator=(MiraiClient&& rhs) noexcept = delete;
147 ~MiraiClient();
148
149 /// 获取连接mirai-api-http的session key,若尚未建立链接则返回 `std::nullopt`
150 std::optional<std::string> GetSessionKey() const
151 {
152 std::shared_lock<std::shared_mutex> lk(this->mtx_);
153 return (this->connected_) ? std::optional<std::string>(this->SessionKey_) : std::nullopt;
154 }
155
156
157 /**
158 * @brief 设置适配器
159 *
160 * @param adaptor
161 * @return 原先的适配器,`std::unique_ptr<IAdaptor>`
162 */
163 ///@{
164
165 std::unique_ptr<IAdaptor> SetAdaptor(std::unique_ptr<IAdaptor> adaptor)
166 {
167 std::unique_ptr<IAdaptor> ret = std::move(this->adaptor_);
168 this->adaptor_ = std::move(adaptor);
169 return ret;
170 }
171
172 template<typename Adaptor, typename... Args,
173 typename std::enable_if_t<std::is_base_of_v<IAdaptor, Adaptor>, int> = 0>
174 std::unique_ptr<IAdaptor> SetAdaptor(Args&&... args)
175 {
176 return this->SetAdaptor(std::make_unique<Adaptor>(std::forward<Args>(args)...));
177 }
178
179 ///@}
180
181
182 /**
183 * @brief 设置线程池大小
184 *
185 * @param size 线程池大小
186 * @return std::size_t 原先的线程池大小
187 */
188 std::size_t SetPoolSize(std::size_t size)
189 {
190 std::size_t ret = this->PoolSize_;
191 this->PoolSize_ = ret;
192 return ret;
193 }
194
195
196 /// 设置日志记录类
197 void SetLogger(std::shared_ptr<ILogger> logger) { this->logger_ = logger; }
198
199 /// 获取日志记录类
200 std::shared_ptr<ILogger> GetLogger() const { return this->logger_; }
201
202
203 /**
204 * @brief 注册事件回调函数
205 *
206 * @tparam EventType 事件类型
207 * @param callback 回调函数
208 */
209 ///@{
210 template<typename EventType> void On(EventCallback<EventType> callback)
211 {
212 type_check_<EventType>();
213
214 this->EventHandlers_[EventType::GetType()] = std::move(callback);
215 }
216
217 template<EventTypes Type> void On(EventCallback<GetEventType_t<Type>> callback) { this->On(std::move(callback)); }
218 ///@}
219
220
221 /**
222 * @brief 连接mirai-api-http
223 *
224 * 若成功返回说明已成功建立连接,否则会抛出异常。
225 * `Connect()` 返回前会调用 `ClientConnectionEstablishedEvent` 的回调函数
226 */
227 void Connect();
228
229 /**
230 * @brief 断开与mirai-api-http的连接
231 *
232 * 若成功返回说明已成功断开连接,否则会抛出异常。
233 * `Disconnect()` 返回前会调用 `ClientConnectionClosedEvent` 的回调函数,并等待所有正在运行中的事件回调函数结束。
234 */
235
236 /**
237 * @brief 断开与mirai-api-http的连接
238 *
239 * 若成功返回说明已成功断开连接,否则会抛出异常。
240 * `Disconnect()` 返回前会调用 `ClientConnectionClosedEvent` 的回调函数,并等待所有正在运行中的事件回调函数结束。
241 * @param WaitForFinish 是否等待线程池中的任务完成
242 */
243 void Disconnect(bool WaitForFinish = false);
244
245 /// 返回是否已成功连接mirai-api-http
246 bool isConnected() const
247 {
248 std::shared_lock<std::shared_mutex> lk(this->mtx_);
249 return this->connected_;
250 }
251
252
253 using string = std::string;
254
255
256 /**
257 * @brief 返回兼容的mirai-api-http的版本号
258 *
259 * @return 兼容的mirai-api-http插件的版本号 “x.x.x”
260 */
261 constexpr std::string_view GetCompatibleVersion() const { return "2.9.1"; }
262
263 /**
264 * @brief 获取mirai-api-http插件的版本号
265 *
266 * @return mirai-api-http插件的版本号 “x.x.x”
267 */
268 string GetMiraiApiHttpVersion() const;
269
270 /**
271 * @brief 获取Bot账号
272 *
273 * @return `QQ_t`
274 */
275 QQ_t GetBotQQ() const;
276
277 /**
278 * @brief 获取mirai中可用的QQBot列表
279 *
280 * @return `std::vector<QQ_t>`
281 */
282 std::vector<QQ_t> GetBotList() const;
283
284 /**
285 * @brief 从消息id获取好友消息
286 *
287 * @param id 消息id
288 * @param qq 好友QQ
289 * @return `FriendMessageEvent`
290 */
292
293 /**
294 * @brief 从消息id获取群聊消息
295 *
296 * @param id 消息id
297 * @param GroupId 群聊id
298 * @return `GroupMessageEvent`
299 */
301
302 /**
303 * @brief 从消息id获取临时消息
304 *
305 * @param id 消息id
306 * @param GroupId 群聊id
307 * @return `TempMessageEvent`
308 */
310
311 /**
312 * @brief 从消息id获取陌生人消息
313 *
314 * @param id 消息id
315 * @param qq 陌生人QQ
316 * @return `StrangerMessageEvent`
317 */
319
320 /**
321 * @brief 获取好友列表
322 *
323 * @return `std::vector<User>`
324 */
325 std::vector<User> GetFriendList() const;
326
327 /**
328 * @brief 获取群聊列表
329 *
330 * @return `std::vector<Group>`
331 */
332 std::vector<Group> GetGroupList() const;
333
334 /**
335 * @brief 获取群成员列表
336 *
337 * @param GroupId 群聊id
338 * @return `std::vector<GroupMember>`
339 */
340 std::vector<GroupMember> GetMemberList(GID_t GroupId) const;
341
342 /**
343 * @brief 获取最新群成员列表
344 *
345 * 会强制刷新所有群成员的资料
346 * @param GroupId 群聊id
347 * @return `std::vector<GroupMember>`
348 */
349 std::vector<GroupMember> GetLatestMemberList(GID_t GroupId) const;
350
351 /**
352 * @brief 获取Bot用户资料
353 *
354 * @return `UserProfile`
355 */
357
358 /**
359 * @brief 获取好友用户资料
360 *
361 * @param qq 好友QQ
362 * @return `UserProfile`
363 */
365
366 /**
367 * @brief 获取群成员用户资料
368 *
369 * @param GroupId 群聊id
370 * @param MemberId 群成员QQ
371 * @return `UserProfile`
372 */
373 UserProfile GetMemberProfile(GID_t GroupId, QQ_t MemberId) const;
374
375 /**
376 * @brief 获取用户资料
377 *
378 * @param qq 用户QQ
379 * @return `UserProfile`
380 */
382
383 /**
384 * @brief 发送好友消息
385 *
386 * @param qq 好友QQ
387 * @param message 消息内容
388 * @param QuoteId 引用回复内容的消息id
389 * @return 发送的消息id
390 */
391 ///@{
393 std::optional<MessageId_t> QuoteId = std::nullopt) const;
395 std::optional<MessageId_t> QuoteId = std::nullopt) const;
396 ///@}
397
398 /**
399 * @brief 发送群聊消息
400 *
401 * @param GroupId 群聊id
402 * @param message 消息内容
403 * @param QuoteId 引用回复内容的消息id
404 * @return 发送的消息id
405 */
406 ///@{
407 MessageId_t SendGroupMessage(GID_t GroupId, const MessageChain& message,
408 std::optional<MessageId_t> QuoteId = std::nullopt) const;
410 std::optional<MessageId_t> QuoteId = std::nullopt) const;
411 ///@}
412
413 /**
414 * @brief 发送临时会话消息
415 *
416 * @param MemberId 群成员id
417 * @param GroupId 群聊id
418 * @param message 消息内容
419 * @param QuoteId 引用回复内容的消息id
420 * @return 发送的消息id
421 */
422 ///@{
423 MessageId_t SendTempMessage(QQ_t MemberId, GID_t GroupId, const MessageChain& message,
424 std::optional<MessageId_t> QuoteId = std::nullopt) const;
425 MessageId_t SendTempMessage(QQ_t MemberId, GID_t GroupId, MessageChain&& message,
426 std::optional<MessageId_t> QuoteId = std::nullopt) const;
427 ///@}
428
429 /**
430 * @brief 发送头像戳一戳消息
431 *
432 * @param target 戳一戳目标
433 */
434 void SendNudge(const NudgeTarget& target) const;
435
436 /**
437 * @brief 发送好友戳一戳消息
438 *
439 * @param qq 好友QQ
440 */
441 void NudgeFriend(QQ_t qq) const;
442
443 /**
444 * @brief 发送群聊戳一戳消息
445 *
446 * @param MemberId 戳一戳对象QQ
447 * @param GroupId 群聊id
448 */
449 void NudgeGroup(QQ_t MemberId, GID_t GroupId) const;
450
451 /**
452 * @brief 发送陌生人戳一戳消息
453 *
454 * @param qq 陌生人QQ
455 */
456 void NudgeStranger(QQ_t qq) const;
457
458 /**
459 * @brief 撤回好友消息
460 *
461 * @param id 消息id
462 * @param qq 好友QQ
463 */
464 void RecallFriendMessage(MessageId_t id, QQ_t qq) const;
465
466 /**
467 * @brief 撤回群聊消息
468 *
469 * @param id 消息id
470 * @param GroupId 群聊id
471 */
472 void RecallGroupMessage(MessageId_t id, GID_t GroupId) const;
473
474 /**
475 * @brief 获取好友漫游消息
476 *
477 * @param qq 好友QQ
478 * @param TimeStart 消息起始时间
479 * @param TimeEnd 消息结束时间
480 * @return `std::vector<MessageChain>`
481 */
482 std::vector<MessageChain>
483 GetRoamingFriendMessage(QQ_t qq, std::time_t TimeStart = 0,
484 std::time_t TimeEnd = std::numeric_limits<std::time_t>::max()) const;
485
486 /**
487 * @brief 获取群文件列表
488 *
489 * @param GroupId 群聊id
490 * @param dir 父文件夹路径,默认为根目录
491 * @param offset 分页偏移
492 * @param size 分页大小,默认为不分页(即返回所有结果)
493 * @param withDownloadInfo 是否附带下载地址
494 * @return `std::vector<GroupFileInfo>`
495 */
496 std::vector<GroupFileInfo> GetGroupFileList(GID_t GroupId, const FilePath& dir = {}, int64_t offset = 0,
497 int64_t size = 0, bool withDownloadInfo = false) const;
498
499 /**
500 * @brief 获取群文件信息
501 *
502 * @param GroupId 群聊id
503 * @param dir 群文件路径
504 * @param withDownloadInfo 是否附带下载地址
505 * @return `GroupFileInfo`
506 */
507 GroupFileInfo GetGroupFileInfo(GID_t GroupId, const FilePath& dir, bool withDownloadInfo = false) const;
508
509 /**
510 * @brief 获取群文件信息
511 *
512 * @param GroupId 群聊id
513 * @param file 群文件信息,会从中读取路径并把剩余信息填入
514 * @param withDownloadInfo 是否附带下载地址
515 */
516 void GetGroupFileInfo(GID_t GroupId, GroupFileInfo& file, bool withDownloadInfo = false) const;
517
518 /**
519 * @brief 创建群文件夹
520 *
521 * 目前QQ仅支持在根目录下创建文件夹,不许文件夹嵌套
522 * @param GroupId 群聊id
523 * @param directory 文件夹命名
524 * @return 新建的文件夹信息
525 */
526 GroupFileInfo CreateGroupFileDirectory(GID_t GroupId, string directory) const;
527
528 /**
529 * @brief 删除群文件
530 *
531 * @param GroupId 群聊id
532 * @param dir 群文件路径
533 */
534 void RemoveGroupFile(GID_t GroupId, const FilePath& dir) const;
535
536 /**
537 * @brief 移动群文件
538 *
539 * @param GroupId 群聊id
540 * @param FileDir 群文件路径
541 * @param MoveToDir 目标文件夹路径
542 */
543 void MoveGroupFile(GID_t GroupId, const FilePath& FileDir, const FilePath& MoveToDir) const;
544
545 /**
546 * @brief 重命名群文件
547 *
548 * @param GroupId 群聊id
549 * @param FileDir 群文件路径
550 * @param NewName 新文件名
551 */
552 void RenameGroupFile(GID_t GroupId, const FilePath& FileDir, string NewName) const;
553
554 /**
555 * @brief 上传群文件
556 *
557 * @param GroupId 群聊id
558 * @param UploadPath 上传路径
559 * @param name 文件名称
560 * @param content 文件内容
561 * @return 上传的群文件信息
562 */
563 GroupFileInfo UploadGroupFile(GID_t GroupId, string UploadPath, string name, string content) const;
564
565 /**
566 * @brief 上传群文件
567 *
568 * @param GroupId 群聊id
569 * @param UploadPath 上传路径
570 * @param name 文件名称
571 * @param file 文件流
572 * @return 上传的群文件信息
573 */
574 GroupFileInfo UploadGroupFile(GID_t GroupId, string UploadPath, string name,
575 std::istream& file) const;
576
577 /**
578 * @brief 上传群文件
579 *
580 * Experimental: chunked data transfer is only supported in HTTP/1.1
581 *
582 * @param GroupId 群聊id
583 * @param UploadPath 上传路径
584 * @param name 文件名称
585 * @param ContentProvider 文件内容,返回false表示取消请求
586 * @return 上传的群文件信息
587 */
588 GroupFileInfo
589 UploadGroupFile(GID_t GroupId, string UploadPath, string name,
590 std::function<bool(size_t offset, std::ostream& sink, bool& finish)> ContentProvider) const;
591
592 /**
593 * @brief 上传好友图片
594 *
595 * @param content 图片文件内容(原始二进制,不是base64编码)
596 * @return `FriendImage`
597 */
598 FriendImage UploadFriendImage(string content) const;
599 /**
600 * @brief 上传好友图片
601 *
602 * @param file 图片文件流
603 * @return `FriendImage`
604 */
605 FriendImage UploadFriendImage(std::istream& file) const;
606 /**
607 * @brief 上传好友图片
608 *
609 * Experimental: chunked data transfer is only supported in HTTP/1.1
610 *
611 * @param ContentProvider 图片内容,返回false表示取消请求
612 * @return `FriendImage`
613 */
615 UploadFriendImage(std::function<bool(size_t offset, std::ostream& sink, bool& finish)> ContentProvider) const;
616
617 /**
618 * @brief 上传群聊图片
619 *
620 * @param content 图片文件内容(原始二进制,不是base64编码)
621 * @return `GroupImage`
622 */
623 GroupImage UploadGroupImage(string content) const;
624 /**
625 * @brief 上传群聊图片
626 *
627 * @param file 图片文件流
628 * @return `GroupImage`
629 */
630 GroupImage UploadGroupImage(std::istream& file) const;
631 /**
632 * @brief 上传群聊图片
633 *
634 * Experimental: chunked data transfer is only supported in HTTP/1.1
635 *
636 * @param ContentProvider 图片内容,返回false表示取消请求
637 * @return `GroupImage`
638 */
640 UploadGroupImage(std::function<bool(size_t offset, std::ostream& sink, bool& finish)> ContentProvider) const;
641
642 /**
643 * @brief 上传临时会话图片
644 *
645 * @param content 图片文件内容(原始二进制,不是base64编码)
646 * @return `TempImage`
647 */
648 TempImage UploadTempImage(string content) const;
649 /**
650 * @brief 上传临时会话图片
651 *
652 * @param file 图片文件流
653 * @return `TempImage`
654 */
655 TempImage UploadTempImage(std::istream& file) const;
656 /**
657 * @brief 上传临时会话图片
658 *
659 * Experimental: chunked data transfer is only supported in HTTP/1.1
660 *
661 * @param ContentProvider 图片内容,返回false表示取消请求
662 * @return `TempImage`
663 */
665 UploadTempImage(std::function<bool(size_t offset, std::ostream& sink, bool& finish)> ContentProvider) const;
666
667 /**
668 * @brief 上传群聊语音
669 *
670 * @param content 语音文件内容(原始二进制,不是base64编码)
671 * @return `GroupAudio`
672 */
673 GroupAudio UploadGroupAudio(string content) const;
674 /**
675 * @brief 上传群聊语音
676 *
677 * @param file 语音文件流
678 * @return `GroupAudio`
679 */
680 GroupAudio UploadGroupAudio(std::istream& file) const;
681 /**
682 * @brief 上传群聊语音
683 *
684 * Experimental: chunked data transfer is only supported in HTTP/1.1
685 *
686 * @param ContentProvider 语音内容,返回false表示取消请求
687 * @return `GroupAudio`
688 */
690 UploadGroupAudio(std::function<bool(size_t offset, std::ostream& sink, bool& finish)> ContentProvider) const;
691
692 /**
693 * @brief 删除好友
694 *
695 * @param qq 好友QQ
696 */
697 void DeleteFriend(QQ_t qq) const;
698
699 /**
700 * @brief 禁言群成员
701 *
702 * @param GroupId 群聊id
703 * @param member 成员QQ
704 * @param time 禁言时间
705 */
706 void Mute(GID_t GroupId, QQ_t member, std::chrono::seconds time) const;
707 /**
708 * @brief 禁言群成员
709 *
710 * @param member 群成员
711 * @param time 禁言时间
712 */
713 void Mute(const GroupMember& member, std::chrono::seconds time) const;
714
715 /**
716 * @brief 解除群成员禁言
717 *
718 * @param GroupId 群聊id
719 * @param member 成员QQ
720 */
721 void Unmute(GID_t GroupId, QQ_t member) const;
722 /**
723 * @brief 解除群成员禁言
724 *
725 * @param member 群成员
726 */
727 void Unmute(const GroupMember& member) const;
728
729 /**
730 * @brief 移除群成员
731 *
732 * @param GroupId 群聊id
733 * @param member 成员QQ
734 * @param message 附带信息
735 * @param block 是否移除后拉黑
736 */
737 void Kick(GID_t GroupId, QQ_t member, string message, bool block = false) const;
738 /**
739 * @brief 移除群成员
740 *
741 * @param member 群成员
742 * @param message 附带信息
743 * @param block 是否移除后拉黑
744 */
745 void Kick(const GroupMember& member, string message, bool block = false) const;
746
747 /**
748 * @brief 退出群聊
749 *
750 * @param GroupId 群聊id
751 */
752 void LeaveGroup(GID_t GroupId) const;
753
754 /**
755 * @brief 禁言全体成员
756 *
757 * @param GroupId 群聊id
758 */
759 void MuteAll(GID_t GroupId) const;
760
761 /**
762 * @brief 解除全体禁言
763 *
764 * @param GroupId 群聊id
765 */
766 void UnmuteAll(GID_t GroupId) const;
767
768 /**
769 * @brief 设置群精华消息
770 *
771 * @param GroupId 群聊id
772 * @param MessageId 消息id
773 */
774 void SetEssence(GID_t GroupId, MessageId_t MessageId) const;
775
776 /**
777 * @brief 获取群设置
778 *
779 * @param GroupId 群聊id
780 * @return `GroupConfig`
781 */
782 GroupConfig GetGroupConfig(GID_t GroupId) const;
783
784 /**
785 * @brief 修改群设置
786 *
787 * 目前仅能修改群名称与是否允许邀请入群
788 * @param GroupId 群聊id
789 * @param name 群聊名称,默认保持原设置
790 * @param AllowMemberInvite 是否允许邀请入群,默认保持原设置
791 */
792 void SetGroupConfig(GID_t GroupId, string name = "",
793 std::optional<bool> AllowMemberInvite = std::nullopt) const;
794
795 /**
796 * @brief 获取群成员资料
797 *
798 * @param GroupId 群聊id
799 * @param member 成员QQ
800 * @return `GroupMember`
801 */
802 GroupMember GetMemberInfo(GID_t GroupId, QQ_t member) const;
803
804 /**
805 * @brief 设置群成员资料
806 *
807 * @param GroupId 群聊id
808 * @param member 成员QQ
809 * @param name 群名片
810 * @param title 群头衔
811 */
812 void SetMemberInfo(GID_t GroupId, QQ_t member, string name = "", string title = "") const;
813
814 /**
815 * @brief 设置群管理员
816 *
817 * @param GroupId 群聊id
818 * @param member 成员QQ
819 * @param assign 设置管理员/撤销管理员
820 */
821 void SetGroupAdmin(GID_t GroupId, QQ_t member, bool assign = true) const;
822
823 /**
824 * @brief 获取群公告列表
825 *
826 * @param GroupId 群聊id
827 * @param offset 分页偏移
828 * @param size 分页大小,默认为不分页(即返回所有结果)
829 * @return `std::vector<GroupAnnouncement>`
830 */
831 std::vector<GroupAnnouncement> GetAnnouncementList(GID_t GroupId, int64_t offset = 0, int64_t size = 0) const;
832
833 /**
834 * @brief 发布群公告
835 *
836 * @param GroupId 群聊id
837 * @param content 公告内容
838 * @param cover 公告图片,传空值为不附带图片
839 * @param ToNewMember 是否发给新成员
840 * @param pinned 是否置顶
841 * @param ShowEditCard 是否引导群成员修改群名片
842 * @param ShowPopup 是否弹出公告
843 * @param RequireConfirm 是否需要群员确认
844 * @return 发布的群公告
845 */
846 GroupAnnouncement PublishAnnouncement(GID_t GroupId, string content, MiraiImage cover = {},
847 bool ToNewMember = false, bool pinned = false, bool ShowEditCard = false,
848 bool ShowPopup = false, bool RequireConfirm = false) const;
849
850 /**
851 * @brief 删除群公告
852 *
853 * @param GroupId 群聊id
854 * @param fid 群公告id
855 */
856 void DeleteAnnouncement(GID_t GroupId, string fid) const;
857 /**
858 * @brief 删除群公告
859 *
860 * @param announcement 群公告对象
861 */
862 void DeleteAnnouncement(const GroupAnnouncement& announcement) const;
863
864 /**
865 * @brief 处理添加好友申请事件 `NewFriendRequestEvent`
866 *
867 * @param EventId 事件id
868 * @param FromId 申请人qq
869 * @param GroupId 申请人来自的群聊id,可能为 `0_gid`
870 * @param operation 处理操作
871 * @param message 回复消息
872 */
873 void RespNewFriendRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId, NewFriendRequestOp operation,
874 string message) const;
875 /**
876 * @brief 处理添加好友申请事件 `NewFriendRequestEvent`
877 *
878 * @param event 好友申请事件
879 * @param operation 处理操作
880 * @param message 回复消息
881 */
882 void RespNewFriendRequestEvent(const NewFriendRequestEvent& event, NewFriendRequestOp operation,
883 string message) const;
884
885 /**
886 * @brief 处理用户申请入群事件 `MemberJoinRequestEvent`
887 *
888 * @param EventId 事件id
889 * @param FromId 申请人qq
890 * @param GroupId 群聊id
891 * @param operation 处理操作
892 * @param message 回复消息
893 */
894 void RespMemberJoinRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId, MemberJoinRequestOp operation,
895 string message) const;
896 /**
897 * @brief 处理用户申请入群事件 `MemberJoinRequestEvent`
898 *
899 * @param event 用户申请入群事件
900 * @param operation 处理操作
901 * @param message 回复消息
902 */
903 void RespMemberJoinRequestEvent(const MemberJoinRequestEvent& event, MemberJoinRequestOp operation,
904 string message) const;
905
906 /**
907 * @brief 处理Bot被邀请入群事件 `BotInvitedJoinGroupRequestEvent`
908 *
909 * @param EventId 事件id
910 * @param FromId 申请人qq
911 * @param GroupId 群聊id
912 * @param operation 处理操作
913 * @param message 回复消息
914 */
915 void RespBotInvitedJoinGroupRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId,
916 BotInvitedJoinGroupRequestOp operation, string message) const;
917 /**
918 * @brief 处理Bot被邀请入群事件 `BotInvitedJoinGroupRequestEvent`
919 *
920 * @param event Bot被邀请入群事件
921 * @param operation 处理操作
922 * @param message 回复消息
923 */
924 void RespBotInvitedJoinGroupRequestEvent(const BotInvitedJoinGroupRequestEvent& event,
925 BotInvitedJoinGroupRequestOp operation, string message) const;
926
927 /**
928 * @brief 注册指令
929 *
930 * @param name 指令名称
931 * @param alias 指令别名
932 * @param usage 使用说明
933 * @param description 指令描述
934 */
935 void RegisterCommand(string name, std::vector<string> alias, string usage,
936 string description) const;
937
938 /**
939 * @brief 执行指令
940 *
941 * @param command 指令内容
942 */
943 ///@{
944 void ExecuteCommand(const MessageChain& command) const;
945 void ExecuteCommand(MessageChain&& command) const;
946 ///@}
947
948 /**
949 * @brief 直接向mirai-api-http发送请求
950 *
951 * 备用方法,可用于hack新功能或提供临时替代,正常情况下不推荐使用
952 * @param path 路径
953 * @param method 内容格式,可能是 "POST" "GET" 或subcommand等,取决于具体的Adaptor
954 * @param data 请求数据
955 * @return `std::string`
956 */
957 string CallAPI(const string& path, const string& method, const string& data) const;
958};
959
960template<>
961inline void MiraiClient::On<ClientConnectionEstablishedEvent>(EventCallback<ClientConnectionEstablishedEvent> callback)
962{
963 this->ConnectionEstablishedCallback_ = callback;
964}
965
966template<> inline void MiraiClient::On<ClientConnectionErrorEvent>(EventCallback<ClientConnectionErrorEvent> callback)
967{
968 this->ConnectionErrorCallback_ = callback;
969}
970
971template<> inline void MiraiClient::On<ClientConnectionClosedEvent>(EventCallback<ClientConnectionClosedEvent> callback)
972{
973 this->ConnectionClosedCallback_ = callback;
974}
975
976template<> inline void MiraiClient::On<ClientParseErrorEvent>(EventCallback<ClientParseErrorEvent> callback)
977{
978 this->ParseErrorCallback_ = callback;
979}
980
981} // namespace Mirai
982
983#endif
文件路径
Definition: MediaTypes.hpp:108
好友消息事件
群聊号码类型
Definition: BasicTypes.hpp:88
群聊消息事件
消息链对象,由一系列消息元素组成
提供与mirai-api-http网络交互的封装
Definition: Client.hpp:61
EventCallback< ClientConnectionClosedEvent > ConnectionClosedCallback_
Definition: Client.hpp:119
std::vector< MessageChain > GetRoamingFriendMessage(QQ_t qq, std::time_t TimeStart=0, std::time_t TimeEnd=std::numeric_limits< std::time_t >::max()) const
获取好友漫游消息
Definition: Client.cpp:444
void SetLogger(std::shared_ptr< ILogger > logger)
设置日志记录类
Definition: Client.hpp:197
void Connect()
连接mirai-api-http
Definition: Client.cpp:114
traits::EventCallbackVariant EventHandler
Definition: Client.hpp:123
GroupAudio UploadGroupAudio(string content) const
上传群聊语音
Definition: Client.cpp:582
void NudgeFriend(QQ_t qq) const
发送好友戳一戳消息
Definition: Client.cpp:419
MiraiClient(const MiraiClient &)=delete
MiraiClient(MiraiClient &&rhs) noexcept=delete
string GetMiraiApiHttpVersion() const
获取mirai-api-http插件的版本号
Definition: Client.cpp:272
void On(EventCallback< GetEventType_t< Type > > callback)
注册事件回调函数
Definition: Client.hpp:217
MessageId_t SendFriendMessage(QQ_t qq, const MessageChain &message, std::optional< MessageId_t > QuoteId=std::nullopt) const
发送好友消息
Definition: Client.cpp:374
std::string string
Definition: Client.hpp:253
void RemoveGroupFile(GID_t GroupId, const FilePath &dir) const
删除群文件
Definition: Client.cpp:473
MiraiClient(Args &&... args)
Definition: Client.hpp:138
std::shared_ptr< ILogger > GetLogger() const
获取日志记录类
Definition: Client.hpp:200
std::vector< GroupAnnouncement > GetAnnouncementList(GID_t GroupId, int64_t offset=0, int64_t size=0) const
获取群公告列表
Definition: Client.cpp:686
GroupFileInfo UploadGroupFile(GID_t GroupId, string UploadPath, string name, string content) const
上传群文件
Definition: Client.cpp:490
std::unordered_map< EventTypes, EventHandler > EventHandlers_
Definition: Client.hpp:124
GroupConfig GetGroupConfig(GID_t GroupId) const
获取群设置
Definition: Client.cpp:660
StrangerMessageEvent GetStrangerMessage(MessageId_t id, QQ_t qq) const
从消息id获取陌生人消息
Definition: Client.cpp:321
string CallAPI(const string &path, const string &method, const string &data) const
直接向mirai-api-http发送请求
Definition: Client.cpp:771
void RecallGroupMessage(MessageId_t id, GID_t GroupId) const
撤回群聊消息
Definition: Client.cpp:439
UserProfile GetFriendProfile(QQ_t qq) const
获取好友用户资料
Definition: Client.cpp:359
GroupImage UploadGroupImage(string content) const
上传群聊图片
Definition: Client.cpp:538
UserProfile GetUserProfile(QQ_t qq) const
获取用户资料
Definition: Client.cpp:369
void SetGroupConfig(GID_t GroupId, string name="", std::optional< bool > AllowMemberInvite=std::nullopt) const
修改群设置
Definition: Client.cpp:665
std::unique_ptr< IAdaptor > adaptor_
Definition: Client.hpp:115
std::vector< GroupMember > GetMemberList(GID_t GroupId) const
获取群成员列表
Definition: Client.cpp:343
UserProfile GetMemberProfile(GID_t GroupId, QQ_t MemberId) const
获取群成员用户资料
Definition: Client.cpp:364
void RespMemberJoinRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId, MemberJoinRequestOp operation, string message) const
处理用户申请入群事件 MemberJoinRequestEvent
Definition: Client.cpp:724
std::string SessionKey_
Definition: Client.hpp:110
void SendNudge(const NudgeTarget &target) const
发送头像戳一戳消息
Definition: Client.cpp:410
void Mute(GID_t GroupId, QQ_t member, std::chrono::seconds time) const
禁言群成员
Definition: Client.cpp:609
void RespNewFriendRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId, NewFriendRequestOp operation, string message) const
处理添加好友申请事件 NewFriendRequestEvent
Definition: Client.cpp:710
std::size_t SetPoolSize(std::size_t size)
设置线程池大小
Definition: Client.hpp:188
void DeleteAnnouncement(GID_t GroupId, string fid) const
删除群公告
Definition: Client.cpp:699
GroupMember GetMemberInfo(GID_t GroupId, QQ_t member) const
获取群成员资料
Definition: Client.cpp:670
std::vector< QQ_t > GetBotList() const
获取mirai中可用的QQBot列表
Definition: Client.cpp:282
void ExecuteCommand(const MessageChain &command) const
执行指令
Definition: Client.cpp:761
std::function< void(Event)> EventCallback
Definition: Client.hpp:104
void SetMemberInfo(GID_t GroupId, QQ_t member, string name="", string title="") const
设置群成员资料
Definition: Client.cpp:675
std::vector< GroupMember > GetLatestMemberList(GID_t GroupId) const
获取最新群成员列表
Definition: Client.cpp:348
GroupFileInfo GetGroupFileInfo(GID_t GroupId, const FilePath &dir, bool withDownloadInfo=false) const
获取群文件信息
Definition: Client.cpp:458
MiraiClient & operator=(const MiraiClient &)=delete
void MuteAll(GID_t GroupId) const
禁言全体成员
Definition: Client.cpp:644
MessageId_t SendTempMessage(QQ_t MemberId, GID_t GroupId, const MessageChain &message, std::optional< MessageId_t > QuoteId=std::nullopt) const
发送临时会话消息
Definition: Client.cpp:398
void NudgeGroup(QQ_t MemberId, GID_t GroupId) const
发送群聊戳一戳消息
Definition: Client.cpp:424
EventCallback< ClientConnectionEstablishedEvent > ConnectionEstablishedCallback_
Definition: Client.hpp:118
EventCallback< ClientParseErrorEvent > ParseErrorCallback_
Definition: Client.hpp:121
std::shared_ptr< ILogger > logger_
Definition: Client.hpp:114
std::shared_mutex mtx_
Definition: Client.hpp:108
TempImage UploadTempImage(string content) const
上传临时会话图片
Definition: Client.cpp:560
void Disconnect(bool WaitForFinish=false)
断开与mirai-api-http的连接
Definition: Client.cpp:256
std::vector< GroupFileInfo > GetGroupFileList(GID_t GroupId, const FilePath &dir={}, int64_t offset=0, int64_t size=0, bool withDownloadInfo=false) const
获取群文件列表
Definition: Client.cpp:451
void DeleteFriend(QQ_t qq) const
删除好友
Definition: Client.cpp:604
void RegisterCommand(string name, std::vector< string > alias, string usage, string description) const
注册指令
Definition: Client.cpp:755
std::string GetSessionKey_() const
Definition: Client.hpp:126
EventCallback< ClientConnectionErrorEvent > ConnectionErrorCallback_
Definition: Client.hpp:120
MessageId_t SendGroupMessage(GID_t GroupId, const MessageChain &message, std::optional< MessageId_t > QuoteId=std::nullopt) const
发送群聊消息
Definition: Client.cpp:386
TempMessageEvent GetTempMessage(MessageId_t id, GID_t GroupId) const
从消息id获取临时消息
Definition: Client.cpp:310
std::vector< User > GetFriendList() const
获取好友列表
Definition: Client.cpp:333
FriendMessageEvent GetFriendMessage(MessageId_t id, QQ_t qq) const
从消息id获取好友消息
Definition: Client.cpp:288
GroupMessageEvent GetGroupMessage(MessageId_t id, GID_t GroupId) const
从消息id获取群聊消息
Definition: Client.cpp:299
void MoveGroupFile(GID_t GroupId, const FilePath &FileDir, const FilePath &MoveToDir) const
移动群文件
Definition: Client.cpp:478
void On(EventCallback< EventType > callback)
注册事件回调函数
Definition: Client.hpp:210
void LeaveGroup(GID_t GroupId) const
退出群聊
Definition: Client.cpp:639
std::unique_ptr< Utils::ThreadPool > pool_
Definition: Client.hpp:116
void Kick(GID_t GroupId, QQ_t member, string message, bool block=false) const
移除群成员
Definition: Client.cpp:629
void SetEssence(GID_t GroupId, MessageId_t MessageId) const
设置群精华消息
Definition: Client.cpp:654
void NudgeStranger(QQ_t qq) const
发送陌生人戳一戳消息
Definition: Client.cpp:429
std::vector< Group > GetGroupList() const
获取群聊列表
Definition: Client.cpp:338
std::unique_ptr< IAdaptor > SetAdaptor(Args &&... args)
设置适配器
Definition: Client.hpp:174
QQ_t GetBotQQ() const
获取Bot账号
Definition: Client.cpp:277
void RenameGroupFile(GID_t GroupId, const FilePath &FileDir, string NewName) const
重命名群文件
Definition: Client.cpp:484
void RecallFriendMessage(MessageId_t id, QQ_t qq) const
撤回好友消息
Definition: Client.cpp:434
void UnmuteAll(GID_t GroupId) const
解除全体禁言
Definition: Client.cpp:649
MiraiClient & operator=(MiraiClient &&rhs) noexcept=delete
FriendImage UploadFriendImage(string content) const
上传好友图片
Definition: Client.cpp:516
GroupFileInfo CreateGroupFileDirectory(GID_t GroupId, string directory) const
创建群文件夹
Definition: Client.cpp:468
GroupAnnouncement PublishAnnouncement(GID_t GroupId, string content, MiraiImage cover={}, bool ToNewMember=false, bool pinned=false, bool ShowEditCard=false, bool ShowPopup=false, bool RequireConfirm=false) const
发布群公告
Definition: Client.cpp:691
bool isConnected() const
返回是否已成功连接mirai-api-http
Definition: Client.hpp:246
UserProfile GetBotProfile() const
获取Bot用户资料
Definition: Client.cpp:354
void SetGroupAdmin(GID_t GroupId, QQ_t member, bool assign=true) const
设置群管理员
Definition: Client.cpp:680
void RespBotInvitedJoinGroupRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId, BotInvitedJoinGroupRequestOp operation, string message) const
处理Bot被邀请入群事件 BotInvitedJoinGroupRequestEvent
Definition: Client.cpp:738
std::optional< std::string > GetSessionKey() const
获取连接mirai-api-http的session key,若尚未建立链接则返回 std::nullopt
Definition: Client.hpp:150
std::unique_ptr< IAdaptor > SetAdaptor(std::unique_ptr< IAdaptor > adaptor)
设置适配器
Definition: Client.hpp:165
void Unmute(GID_t GroupId, QQ_t member) const
解除群成员禁言
Definition: Client.cpp:619
constexpr std::string_view GetCompatibleVersion() const
返回兼容的mirai-api-http的版本号
Definition: Client.hpp:261
QQ移动端头像戳一戳动作的对象
Definition: NudgeTarget.hpp:37
QQ号码类型
Definition: BasicTypes.hpp:71
临时会话消息事件
所有mirai相关的对象的命名空间
MiraiAudio GroupAudio
Definition: MediaTypes.hpp:287
NewFriendRequestOp
处理好友申请的操作
Definition: BasicTypes.hpp:115
typename GetEventType< type >::type GetEventType_t
Definition: EventTypes.hpp:109
MiraiImage FriendImage
Definition: MediaTypes.hpp:239
BotInvitedJoinGroupRequestOp
处理被邀请入群的操作
Definition: BasicTypes.hpp:150
constexpr std::array< EventTypes, static_cast< size_t >(EventTypes::ENUM_END)> EventTypesList
Definition: EventTypes.hpp:95
MiraiImage TempImage
Definition: MediaTypes.hpp:241
int64_t MessageId_t
消息id类型,用于撤回消息和引用消息
Definition: BasicTypes.hpp:35
MemberJoinRequestOp
处理用户入群申请的操作
Definition: BasicTypes.hpp:133
MiraiImage GroupImage
Definition: MediaTypes.hpp:240
STL namespace
std::variant< std::function< void(GetEventType_t< EventTypesList[I]>)>... > type
Definition: Client.hpp:82
QQ用户资料
Definition: BasicTypes.hpp:409