16#ifndef MIRAI_CLIENT_HPP_
17#define MIRAI_CLIENT_HPP_
25#include <shared_mutex>
29#include <unordered_map>
68 template<
typename T,
size_t... I>
70 :
public std::disjunction<std::is_same<T, GetEventType_t<EventTypesList[I]>>...>
95 template<
typename EventType>
constexpr static void type_check_()
97 static_assert(std::is_base_of_v<IEvent<EventType>, EventType>,
98 "EventType is not derived from IEvent<EventType>");
100 "Unsupported event type");
108 mutable std::shared_mutex
mtx_;
114 std::shared_ptr<ILogger>
logger_ = std::make_shared<NullLogger>();
116 std::unique_ptr<Utils::ThreadPool>
pool_;
128 std::shared_lock<std::shared_mutex> lk(this->mtx_);
136 template<
typename Adaptor,
typename... Args,
137 typename std::enable_if_t<std::is_base_of_v<IAdaptor, Adaptor>,
int> = 0>
152 std::shared_lock<std::shared_mutex> lk(this->mtx_);
153 return (this->connected_) ? std::optional<std::string>(this->
SessionKey_) : std::nullopt;
165 std::unique_ptr<IAdaptor>
SetAdaptor(std::unique_ptr<IAdaptor> adaptor)
167 std::unique_ptr<IAdaptor> ret = std::move(this->adaptor_);
168 this->adaptor_ = std::move(adaptor);
172 template<
typename Adaptor,
typename... Args,
173 typename std::enable_if_t<std::is_base_of_v<IAdaptor, Adaptor>,
int> = 0>
176 return this->
SetAdaptor(std::make_unique<Adaptor>(std::forward<Args>(args)...));
191 this->PoolSize_ = ret;
197 void SetLogger(std::shared_ptr<ILogger> logger) { this->logger_ = logger; }
212 type_check_<EventType>();
248 std::shared_lock<std::shared_mutex> lk(this->mtx_);
393 std::optional<MessageId_t> QuoteId = std::nullopt)
const;
395 std::optional<MessageId_t> QuoteId = std::nullopt)
const;
408 std::optional<MessageId_t> QuoteId = std::nullopt)
const;
410 std::optional<MessageId_t> QuoteId = std::nullopt)
const;
424 std::optional<MessageId_t> QuoteId = std::nullopt)
const;
426 std::optional<MessageId_t> QuoteId = std::nullopt)
const;
482 std::vector<MessageChain>
484 std::time_t TimeEnd = std::numeric_limits<std::time_t>::max())
const;
497 int64_t size = 0,
bool withDownloadInfo =
false)
const;
507 GroupFileInfo
GetGroupFileInfo(GID_t GroupId,
const FilePath& dir,
bool withDownloadInfo =
false)
const;
516 void GetGroupFileInfo(GID_t GroupId, GroupFileInfo& file,
bool withDownloadInfo =
false)
const;
543 void MoveGroupFile(GID_t GroupId,
const FilePath& FileDir,
const FilePath& MoveToDir)
const;
552 void RenameGroupFile(GID_t GroupId,
const FilePath& FileDir,
string NewName)
const;
563 GroupFileInfo
UploadGroupFile(GID_t GroupId,
string UploadPath,
string name,
string content)
const;
574 GroupFileInfo
UploadGroupFile(GID_t GroupId,
string UploadPath,
string name,
575 std::istream& file)
const;
590 std::function<
bool(
size_t offset, std::ostream& sink,
bool& finish)> ContentProvider)
const;
615 UploadFriendImage(std::function<
bool(
size_t offset, std::ostream& sink,
bool& finish)> ContentProvider)
const;
640 UploadGroupImage(std::function<
bool(
size_t offset, std::ostream& sink,
bool& finish)> ContentProvider)
const;
665 UploadTempImage(std::function<
bool(
size_t offset, std::ostream& sink,
bool& finish)> ContentProvider)
const;
690 UploadGroupAudio(std::function<
bool(
size_t offset, std::ostream& sink,
bool& finish)> ContentProvider)
const;
706 void Mute(GID_t GroupId, QQ_t member, std::chrono::seconds time)
const;
713 void Mute(
const GroupMember& member, std::chrono::seconds time)
const;
721 void Unmute(GID_t GroupId, QQ_t member)
const;
727 void Unmute(
const GroupMember& member)
const;
737 void Kick(GID_t GroupId, QQ_t member,
string message,
bool block =
false)
const;
745 void Kick(
const GroupMember& member,
string message,
bool block =
false)
const;
759 void MuteAll(GID_t GroupId)
const;
793 std::optional<bool> AllowMemberInvite = std::nullopt)
const;
812 void SetMemberInfo(GID_t GroupId, QQ_t member,
string name =
"",
string title =
"")
const;
821 void SetGroupAdmin(GID_t GroupId, QQ_t member,
bool assign =
true)
const;
831 std::vector<GroupAnnouncement>
GetAnnouncementList(GID_t GroupId, int64_t offset = 0, int64_t size = 0)
const;
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;
874 string message)
const;
883 string message)
const;
895 string message)
const;
904 string message)
const;
935 void RegisterCommand(
string name, std::vector<string> alias,
string usage,
936 string description)
const;
957 string CallAPI(
const string& path,
const string& method,
const string& data)
const;
963 this->ConnectionEstablishedCallback_ = callback;
968 this->ConnectionErrorCallback_ = callback;
973 this->ConnectionClosedCallback_ = callback;
978 this->ParseErrorCallback_ = callback;
EventCallback< ClientConnectionClosedEvent > ConnectionClosedCallback_
std::vector< MessageChain > GetRoamingFriendMessage(QQ_t qq, std::time_t TimeStart=0, std::time_t TimeEnd=std::numeric_limits< std::time_t >::max()) const
获取好友漫游消息
void SetLogger(std::shared_ptr< ILogger > logger)
设置日志记录类
void Connect()
连接mirai-api-http
traits::EventCallbackVariant EventHandler
GroupAudio UploadGroupAudio(string content) const
上传群聊语音
void NudgeFriend(QQ_t qq) const
发送好友戳一戳消息
MiraiClient(const MiraiClient &)=delete
MiraiClient(MiraiClient &&rhs) noexcept=delete
string GetMiraiApiHttpVersion() const
获取mirai-api-http插件的版本号
void On(EventCallback< GetEventType_t< Type > > callback)
注册事件回调函数
MessageId_t SendFriendMessage(QQ_t qq, const MessageChain &message, std::optional< MessageId_t > QuoteId=std::nullopt) const
发送好友消息
void RemoveGroupFile(GID_t GroupId, const FilePath &dir) const
删除群文件
MiraiClient(Args &&... args)
std::shared_ptr< ILogger > GetLogger() const
获取日志记录类
std::vector< GroupAnnouncement > GetAnnouncementList(GID_t GroupId, int64_t offset=0, int64_t size=0) const
获取群公告列表
GroupFileInfo UploadGroupFile(GID_t GroupId, string UploadPath, string name, string content) const
上传群文件
std::unordered_map< EventTypes, EventHandler > EventHandlers_
GroupConfig GetGroupConfig(GID_t GroupId) const
获取群设置
StrangerMessageEvent GetStrangerMessage(MessageId_t id, QQ_t qq) const
从消息id获取陌生人消息
string CallAPI(const string &path, const string &method, const string &data) const
直接向mirai-api-http发送请求
void RecallGroupMessage(MessageId_t id, GID_t GroupId) const
撤回群聊消息
UserProfile GetFriendProfile(QQ_t qq) const
获取好友用户资料
GroupImage UploadGroupImage(string content) const
上传群聊图片
UserProfile GetUserProfile(QQ_t qq) const
获取用户资料
void SetGroupConfig(GID_t GroupId, string name="", std::optional< bool > AllowMemberInvite=std::nullopt) const
修改群设置
std::unique_ptr< IAdaptor > adaptor_
std::vector< GroupMember > GetMemberList(GID_t GroupId) const
获取群成员列表
UserProfile GetMemberProfile(GID_t GroupId, QQ_t MemberId) const
获取群成员用户资料
void RespMemberJoinRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId, MemberJoinRequestOp operation, string message) const
处理用户申请入群事件 MemberJoinRequestEvent
void SendNudge(const NudgeTarget &target) const
发送头像戳一戳消息
void Mute(GID_t GroupId, QQ_t member, std::chrono::seconds time) const
禁言群成员
void RespNewFriendRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId, NewFriendRequestOp operation, string message) const
处理添加好友申请事件 NewFriendRequestEvent
std::size_t SetPoolSize(std::size_t size)
设置线程池大小
void DeleteAnnouncement(GID_t GroupId, string fid) const
删除群公告
GroupMember GetMemberInfo(GID_t GroupId, QQ_t member) const
获取群成员资料
std::vector< QQ_t > GetBotList() const
获取mirai中可用的QQBot列表
void ExecuteCommand(const MessageChain &command) const
执行指令
std::function< void(Event)> EventCallback
void SetMemberInfo(GID_t GroupId, QQ_t member, string name="", string title="") const
设置群成员资料
std::vector< GroupMember > GetLatestMemberList(GID_t GroupId) const
获取最新群成员列表
GroupFileInfo GetGroupFileInfo(GID_t GroupId, const FilePath &dir, bool withDownloadInfo=false) const
获取群文件信息
MiraiClient & operator=(const MiraiClient &)=delete
void MuteAll(GID_t GroupId) const
禁言全体成员
MessageId_t SendTempMessage(QQ_t MemberId, GID_t GroupId, const MessageChain &message, std::optional< MessageId_t > QuoteId=std::nullopt) const
发送临时会话消息
void NudgeGroup(QQ_t MemberId, GID_t GroupId) const
发送群聊戳一戳消息
EventCallback< ClientConnectionEstablishedEvent > ConnectionEstablishedCallback_
EventCallback< ClientParseErrorEvent > ParseErrorCallback_
std::shared_ptr< ILogger > logger_
TempImage UploadTempImage(string content) const
上传临时会话图片
void Disconnect(bool WaitForFinish=false)
断开与mirai-api-http的连接
std::vector< GroupFileInfo > GetGroupFileList(GID_t GroupId, const FilePath &dir={}, int64_t offset=0, int64_t size=0, bool withDownloadInfo=false) const
获取群文件列表
void DeleteFriend(QQ_t qq) const
删除好友
void RegisterCommand(string name, std::vector< string > alias, string usage, string description) const
注册指令
std::string GetSessionKey_() const
EventCallback< ClientConnectionErrorEvent > ConnectionErrorCallback_
MessageId_t SendGroupMessage(GID_t GroupId, const MessageChain &message, std::optional< MessageId_t > QuoteId=std::nullopt) const
发送群聊消息
TempMessageEvent GetTempMessage(MessageId_t id, GID_t GroupId) const
从消息id获取临时消息
std::vector< User > GetFriendList() const
获取好友列表
FriendMessageEvent GetFriendMessage(MessageId_t id, QQ_t qq) const
从消息id获取好友消息
GroupMessageEvent GetGroupMessage(MessageId_t id, GID_t GroupId) const
从消息id获取群聊消息
void MoveGroupFile(GID_t GroupId, const FilePath &FileDir, const FilePath &MoveToDir) const
移动群文件
void On(EventCallback< EventType > callback)
注册事件回调函数
void LeaveGroup(GID_t GroupId) const
退出群聊
std::unique_ptr< Utils::ThreadPool > pool_
void Kick(GID_t GroupId, QQ_t member, string message, bool block=false) const
移除群成员
void SetEssence(GID_t GroupId, MessageId_t MessageId) const
设置群精华消息
void NudgeStranger(QQ_t qq) const
发送陌生人戳一戳消息
std::vector< Group > GetGroupList() const
获取群聊列表
std::unique_ptr< IAdaptor > SetAdaptor(Args &&... args)
设置适配器
QQ_t GetBotQQ() const
获取Bot账号
void RenameGroupFile(GID_t GroupId, const FilePath &FileDir, string NewName) const
重命名群文件
void RecallFriendMessage(MessageId_t id, QQ_t qq) const
撤回好友消息
void UnmuteAll(GID_t GroupId) const
解除全体禁言
MiraiClient & operator=(MiraiClient &&rhs) noexcept=delete
FriendImage UploadFriendImage(string content) const
上传好友图片
GroupFileInfo CreateGroupFileDirectory(GID_t GroupId, string directory) const
创建群文件夹
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
发布群公告
bool isConnected() const
返回是否已成功连接mirai-api-http
UserProfile GetBotProfile() const
获取Bot用户资料
void SetGroupAdmin(GID_t GroupId, QQ_t member, bool assign=true) const
设置群管理员
void RespBotInvitedJoinGroupRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId, BotInvitedJoinGroupRequestOp operation, string message) const
处理Bot被邀请入群事件 BotInvitedJoinGroupRequestEvent
std::optional< std::string > GetSessionKey() const
获取连接mirai-api-http的session key,若尚未建立链接则返回 std::nullopt
std::unique_ptr< IAdaptor > SetAdaptor(std::unique_ptr< IAdaptor > adaptor)
设置适配器
void Unmute(GID_t GroupId, QQ_t member) const
解除群成员禁言
constexpr std::string_view GetCompatibleVersion() const
返回兼容的mirai-api-http的版本号
NewFriendRequestOp
处理好友申请的操作
typename GetEventType< type >::type GetEventType_t
BotInvitedJoinGroupRequestOp
处理被邀请入群的操作
constexpr std::array< EventTypes, static_cast< size_t >(EventTypes::ENUM_END)> EventTypesList
int64_t MessageId_t
消息id类型,用于撤回消息和引用消息
MemberJoinRequestOp
处理用户入群申请的操作
std::variant< std::function< void(GetEventType_t< EventTypesList[I]>)>... > type