cpp-mirai-client  v2.6.1
cpp client for mirai-api-http
Client.cpp
浏览该文件的文档.
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#include "Client.hpp"
17
18#include <condition_variable>
19#include <exception>
20#include <fstream>
21#include <memory>
22#include <mutex>
23#include <string>
24#include <thread>
25#include <type_traits>
26#include <utility>
27#include <variant>
28
29#include <httplib.h>
30#include <nlohmann/json.hpp>
31
33#include <libmirai/Serialization/Events/Events.hpp>
34#include <libmirai/Serialization/Types/Types.hpp>
35#include <libmirai/Utils/Common.hpp>
37#include <libmirai/Utils/ThreadPool.hpp>
38
39namespace Mirai
40{
41
43
44MiraiClient::MiraiClient() = default;
45MiraiClient::MiraiClient(std::unique_ptr<IAdaptor> adaptor) : adaptor_(std::move(adaptor)) {}
46
47// MiraiClient::MiraiClient(MiraiClient&& rhs) noexcept
48// {
49// std::unique_lock<std::shared_mutex> lk(rhs.mtx_);
50
51// #define MIRAI_MOVE_ASSIGN(m_var) this->m_var = std::move(rhs.m_var)
52// #define MIRAI_COPY_ASSIGN(m_var) this->m_var = rhs.m_var
53
54// // NOLINTBEGIN(*-prefer-member-initializer)
55// MIRAI_MOVE_ASSIGN(SessionKey_);
56// MIRAI_MOVE_ASSIGN(logger_);
57// MIRAI_MOVE_ASSIGN(adaptor_);
58// MIRAI_MOVE_ASSIGN(pool_);
59
60// MIRAI_MOVE_ASSIGN(ConnectionEstablishedCallback_);
61// MIRAI_MOVE_ASSIGN(ConnectionClosedCallback_);
62// MIRAI_MOVE_ASSIGN(ConnectionErrorCallback_);
63// MIRAI_MOVE_ASSIGN(ParseErrorCallback_);
64
65// MIRAI_MOVE_ASSIGN(EventHandlers_);
66
67// MIRAI_COPY_ASSIGN(connected_);
68// MIRAI_COPY_ASSIGN(PoolSize_);
69// // NOLINTEND(*-prefer-member-initializer)
70
71// #undef MIRAI_MOVE_ASSIGN
72// #undef MIRAI_COPY_ASSIGN
73// }
74
75// MiraiClient& MiraiClient::operator=(MiraiClient&& rhs) noexcept
76// {
77// if (this != &rhs)
78// {
79// std::unique_lock<std::shared_mutex> lk_this(this->mtx_, std::defer_lock);
80// std::unique_lock<std::shared_mutex> lk_rhs(rhs.mtx_, std::defer_lock);
81
82// std::scoped_lock lk(lk_rhs, lk_this);
83
84// #define MIRAI_MOVE_ASSIGN(m_var) this->m_var = std::move(rhs.m_var)
85// #define MIRAI_COPY_ASSIGN(m_var) this->m_var = rhs.m_var
86
87// MIRAI_MOVE_ASSIGN(SessionKey_);
88// MIRAI_MOVE_ASSIGN(logger_);
89// MIRAI_MOVE_ASSIGN(adaptor_);
90// MIRAI_MOVE_ASSIGN(pool_);
91
92// MIRAI_MOVE_ASSIGN(ConnectionEstablishedCallback_);
93// MIRAI_MOVE_ASSIGN(ConnectionClosedCallback_);
94// MIRAI_MOVE_ASSIGN(ConnectionErrorCallback_);
95// MIRAI_MOVE_ASSIGN(ParseErrorCallback_);
96
97// MIRAI_MOVE_ASSIGN(EventHandlers_);
98
99// MIRAI_COPY_ASSIGN(connected_);
100// MIRAI_COPY_ASSIGN(PoolSize_);
101
102// #undef MIRAI_MOVE_ASSIGN
103// #undef MIRAI_COPY_ASSIGN
104// }
105// return *this;
106// }
107
109{
110 this->Disconnect(false);
111}
112
113
115{
116 if (!this->adaptor_) throw std::runtime_error("Null pointer: No adaptor is set");
117
118 if (this->connected_) return;
119
120 LOG_DEBUG(*(this->logger_), "Connecting to Mirai-Api-Http, initializing threadpool");
121
122 this->pool_ = std::make_unique<Utils::ThreadPool>(this->PoolSize_);
123
124 LOG_DEBUG(*(this->logger_), "Threadpool initialized");
125
126 this->adaptor_->OnConnectionEstablished(
128 {
129 LOG_DEBUG(
130 *(this->logger_),
131 "MessageClient connected with response header: " + [&]() -> std::string
132 {
133 std::string header;
134 for (const auto& p : event.headers)
135 header += "\n" + p.first + ": " + p.second;
136 return header;
137 }());
138 {
139 std::unique_lock<std::shared_mutex> lk(this->mtx_);
140 this->connected_ = true;
141 this->SessionKey_ = event.SessionKey;
142 }
143 if (this->ConnectionEstablishedCallback_) this->ConnectionEstablishedCallback_(std::move(event));
144 });
145
146 this->adaptor_->OnConnectionClosed(
147 [this](ClientConnectionClosedEvent event)
148 {
149 LOG_DEBUG(
150 *(this->logger_),
151 "MessageClient connection closed: " + [&]() -> std::string
152 {
153 std::string msg;
154 msg += "\nCode: " + std::to_string(event.code);
155 msg += "\nReason: " + event.reason;
156 msg += "\nClosedByRemote: ";
157 msg += event.remote ? "True" : "False";
158 return msg;
159 }());
160 {
161 std::unique_lock<std::shared_mutex> lk(this->mtx_);
162 this->connected_ = false;
163 }
164 if (this->ConnectionClosedCallback_) this->ConnectionClosedCallback_(std::move(event));
165 });
166
167 this->adaptor_->OnConnectionError(
168 [this](ClientConnectionErrorEvent event)
169 {
170 LOG_DEBUG(
171 *(this->logger_),
172 "MessageClient failed to connect with error: " + [&event]() -> std::string
173 {
174 std::string msg;
175 msg += "\nHTTP Status: " + std::to_string(event.HttpStatus);
176 msg += "\nReason: " + event.reason;
177 msg += "\nDecompressionError: ";
178 msg += event.DecompressionError ? "True" : "False";
179 return msg;
180 }());
181 if (this->ConnectionErrorCallback_) this->ConnectionErrorCallback_(std::move(event));
182 });
183
184 this->adaptor_->OnRecv(
185 [this](const std::string& message)
186 {
187 LOG_TRACE(*(this->logger_), "Received: " + message);
188
189 try
190 {
191 json msg = json::parse(message);
192 if (!msg.contains("data")) return;
193
194 /*std::string id = msg.value("syncId", "");*/
195 json data = msg.at("data");
196 if (!data.is_object()) return;
197 data = Utils::ParseResponse(data);
198 if (!(data.contains("type") && data.at("type").is_string())) return;
199
200 auto type = data.at("type").get<EventTypes>();
201 LOG_TRACE(*(this->logger_), "Dispatching message, type: " + to_string(type));
202
203 EventHandler handler;
204 {
205 if (this->EventHandlers_.count(type)) handler = this->EventHandlers_.at(type);
206 }
207 std::visit(
208 [&data, this](auto&& handler)
209 {
210 using FuncType = std::decay_t<decltype(handler)>;
211 using EventType = typename traits::function_traits<FuncType>::type;
212
213 if (!handler)
214 {
215 LOG_TRACE(*(this->logger_), "No matching handler found");
216 return;
217 }
218
219 LOG_TRACE(*(this->logger_), "Found handler");
220
221 if constexpr (!std::disjunction_v< std::is_same<EventType, ClientConnectionEstablishedEvent>,
222 std::is_same<EventType, ClientConnectionErrorEvent>,
223 std::is_same<EventType, ClientConnectionClosedEvent>,
224 std::is_same<EventType, ClientParseErrorEvent> >)
225 {
226 EventType event;
227 event.SetClient_(this);
228 from_json(std::move(data), event);
229 (void)this->pool_->enqueue(handler, std::move(event));
230 }
231 },
232 handler);
233 }
234 catch (const ParseError& e)
235 {
236 if (this->ParseErrorCallback_) this->ParseErrorCallback_({{}, e, message});
237 }
238 catch (const std::exception& e)
239 {
240 if (this->ParseErrorCallback_) this->ParseErrorCallback_({{}, ParseError{e.what(), message}, message});
241 }
242 catch (...)
243 {
244 if (this->ParseErrorCallback_)
245 this->ParseErrorCallback_({{}, ParseError{"Unknown error", message}, message});
246 }
247 });
248
249 LOG_DEBUG(*(this->logger_), "Calling connect");
250
251 (void)this->adaptor_->Connect();
252
253 LOG_DEBUG(*(this->logger_), "Successfully connected");
254}
255
256void MiraiClient::Disconnect(bool WaitForFinish)
257{
258 this->adaptor_->Disconnect(this->GetSessionKey_());
259 LOG_DEBUG(*(this->logger_), "Successfully disconnected");
260
261 if (this->pool_)
262 {
263 this->pool_->stop(WaitForFinish);
264 this->pool_ = nullptr;
265 LOG_DEBUG(*(this->logger_), "Threadpool shutdown complete");
266 }
267}
268
269
270using std::string;
271
273{
274 return this->adaptor_->Version();
275}
276
278{
279 return this->adaptor_->GetBotQQ();
280}
281
282std::vector<QQ_t> MiraiClient::GetBotList() const
283{
284 return this->adaptor_->BotList();
285}
286
287
289{
290 auto msg = this->adaptor_->MessageFromId(this->GetSessionKey_(), id, qq);
291
292 if (!std::holds_alternative<FriendMessageEvent>(msg))
294 to_string(std::visit([](auto&& m) -> EventTypes { return m.GetType(); }, msg)));
295
296 return std::get<FriendMessageEvent>(msg);
297}
298
300{
301 auto msg = this->adaptor_->MessageFromId(this->GetSessionKey_(), id, GroupId);
302
303 if (!std::holds_alternative<GroupMessageEvent>(msg))
305 to_string(std::visit([](auto&& m) -> EventTypes { return m.GetType(); }, msg)));
306
307 return std::get<GroupMessageEvent>(msg);
308}
309
311{
312 auto msg = this->adaptor_->MessageFromId(this->GetSessionKey_(), id, GroupId);
313
314 if (!std::holds_alternative<TempMessageEvent>(msg))
316 to_string(std::visit([](auto&& m) -> EventTypes { return m.GetType(); }, msg)));
317
318 return std::get<TempMessageEvent>(msg);
319}
320
322{
323 auto msg = this->adaptor_->MessageFromId(this->GetSessionKey_(), id, qq);
324
325 if (!std::holds_alternative<StrangerMessageEvent>(msg))
327 to_string(std::visit([](auto&& m) -> EventTypes { return m.GetType(); }, msg)));
328
329 return std::get<StrangerMessageEvent>(msg);
330}
331
332
333std::vector<User> MiraiClient::GetFriendList() const
334{
335 return this->adaptor_->FriendList(this->GetSessionKey_());
336}
337
338std::vector<Group> MiraiClient::GetGroupList() const
339{
340 return this->adaptor_->GroupList(this->GetSessionKey_());
341}
342
343std::vector<GroupMember> MiraiClient::GetMemberList(GID_t GroupId) const
344{
345 return this->adaptor_->MemberList(this->GetSessionKey_(), GroupId);
346}
347
348std::vector<GroupMember> MiraiClient::GetLatestMemberList(GID_t GroupId) const
349{
350 return this->adaptor_->LatestMemberList(this->GetSessionKey_(), GroupId);
351}
352
353
355{
356 return this->adaptor_->GetBotProfile(this->GetSessionKey_());
357}
358
360{
361 return this->adaptor_->GetFriendProfile(this->GetSessionKey_(), qq);
362}
363
365{
366 return this->adaptor_->GetMemberProfile(this->GetSessionKey_(), GroupId, MemberId);
367}
368
370{
371 return this->adaptor_->GetUserProfile(this->GetSessionKey_(), qq);
372}
373
375 std::optional<MessageId_t> QuoteId) const
376{
377 return this->adaptor_->SendFriendMessage(this->GetSessionKey_(), qq, message, QuoteId);
378}
379
381 std::optional<MessageId_t> QuoteId) const
382{
383 return this->adaptor_->SendFriendMessage(this->GetSessionKey_(), qq, std::move(message), QuoteId);
384}
385
387 std::optional<MessageId_t> QuoteId) const
388{
389 return this->adaptor_->SendGroupMessage(this->GetSessionKey_(), GroupId, message, QuoteId);
390}
391
393 std::optional<MessageId_t> QuoteId) const
394{
395 return this->adaptor_->SendGroupMessage(this->GetSessionKey_(), GroupId, std::move(message), QuoteId);
396}
397
399 std::optional<MessageId_t> QuoteId) const
400{
401 return this->adaptor_->SendTempMessage(this->GetSessionKey_(), MemberId, GroupId, message, QuoteId);
402}
403
405 std::optional<MessageId_t> QuoteId) const
406{
407 return this->adaptor_->SendTempMessage(this->GetSessionKey_(), MemberId, GroupId, std::move(message), QuoteId);
408}
409
410void MiraiClient::SendNudge(const NudgeTarget& target) const
411{
412 if (target.GetNudgeType() == NudgeType::GROUP)
413 this->adaptor_->SendNudge(this->GetSessionKey_(), target.GetTarget(), target.GetGroup(), target.GetNudgeType());
414 else
415 this->adaptor_->SendNudge(this->GetSessionKey_(), target.GetTarget(), target.GetTarget(),
416 target.GetNudgeType());
417}
418
420{
421 this->adaptor_->SendNudge(this->GetSessionKey_(), qq, qq, NudgeType::FRIEND);
422}
423
424void MiraiClient::NudgeGroup(QQ_t MemberId, GID_t GroupId) const
425{
426 this->adaptor_->SendNudge(this->GetSessionKey_(), MemberId, GroupId, NudgeType::GROUP);
427}
428
430{
431 this->adaptor_->SendNudge(this->GetSessionKey_(), qq, qq, NudgeType::STRANGER);
432}
433
435{
436 this->adaptor_->Recall(this->GetSessionKey_(), id, qq);
437}
438
440{
441 this->adaptor_->Recall(this->GetSessionKey_(), id, GroupId);
442}
443
444std::vector<MessageChain> MiraiClient::GetRoamingFriendMessage(QQ_t qq, std::time_t TimeStart,
445 std::time_t TimeEnd) const
446{
447 return this->adaptor_->RoamingMessages(this->GetSessionKey_(), TimeStart, TimeEnd, qq);
448}
449
450
451std::vector<GroupFileInfo> MiraiClient::GetGroupFileList(GID_t GroupId, const FilePath& dir, int64_t offset,
452 int64_t size, bool withDownloadInfo) const
453{
454 return this->adaptor_->FileList(this->GetSessionKey_(), dir.GetId(), dir.GetPath(), GroupId, offset, size,
455 withDownloadInfo);
456}
457
458GroupFileInfo MiraiClient::GetGroupFileInfo(GID_t GroupId, const FilePath& dir, bool withDownloadInfo) const
459{
460 return this->adaptor_->GetFileInfo(this->GetSessionKey_(), dir.GetId(), dir.GetPath(), GroupId, withDownloadInfo);
461}
462
463void MiraiClient::GetGroupFileInfo(GID_t GroupId, GroupFileInfo& file, bool withDownloadInfo) const
464{
465 this->adaptor_->GetFileInfo(this->GetSessionKey_(), file.id, "", GroupId, withDownloadInfo);
466}
467
469{
470 return this->adaptor_->FileMkdir(this->GetSessionKey_(), "", "", GroupId, std::move(directory));
471}
472
473void MiraiClient::RemoveGroupFile(GID_t GroupId, const FilePath& dir) const
474{
475 this->adaptor_->FileDelete(this->GetSessionKey_(), dir.GetId(), dir.GetPath(), GroupId);
476}
477
478void MiraiClient::MoveGroupFile(GID_t GroupId, const FilePath& FileDir, const FilePath& MoveToDir) const
479{
480 this->adaptor_->FileMove(this->GetSessionKey_(), FileDir.GetId(), FileDir.GetPath(), GroupId, MoveToDir.GetId(),
481 MoveToDir.GetPath());
482}
483
484void MiraiClient::RenameGroupFile(GID_t GroupId, const FilePath& FileDir, string NewName) const
485{
486 this->adaptor_->FileRename(this->GetSessionKey_(), FileDir.GetId(), FileDir.GetPath(), GroupId, std::move(NewName));
487}
488
489
490GroupFileInfo MiraiClient::UploadGroupFile(GID_t GroupId, string UploadPath, string name,
491 string content) const
492{
493 return this->adaptor_->FileUpload(this->GetSessionKey_(), std::move(UploadPath), GroupId, "group", std::move(name), std::move(content));
494}
495
496GroupFileInfo MiraiClient::UploadGroupFile(GID_t GroupId, string UploadPath, string name,
497 std::istream& file) const
498{
499 string s;
500 char buffer[4096]; // NOLINT(*-avoid-c-arrays)
501 while (file.read(buffer, sizeof(buffer))) // NOLINT(*-array-to-pointer-decay)
502 s.append(buffer, sizeof(buffer)); // NOLINT(*-array-to-pointer-decay)
503 s.append(buffer, file.gcount()); // NOLINT(*-array-to-pointer-decay)
504 return this->UploadGroupFile(GroupId, std::move(UploadPath), std::move(name), std::move(s));
505}
506
508MiraiClient::UploadGroupFile(GID_t GroupId, string UploadPath, string name,
509 std::function<bool(size_t offset, std::ostream& sink, bool& finish)> ContentProvider) const
510{
511 return this->adaptor_->FileUploadChunked(this->GetSessionKey_(), std::move(UploadPath), GroupId, "group", std::move(name),
512 std::move(ContentProvider));
513}
514
515
517{
518 return this->adaptor_->UploadImage(this->GetSessionKey_(), "friend", std::move(content));
519}
520
522{
523 string s;
524 char buffer[4096]; // NOLINT(*-avoid-c-arrays)
525 while (file.read(buffer, sizeof(buffer))) // NOLINT(*-array-to-pointer-decay)
526 s.append(buffer, sizeof(buffer)); // NOLINT(*-array-to-pointer-decay)
527 s.append(buffer, file.gcount()); // NOLINT(*-array-to-pointer-decay)
528 return this->UploadFriendImage(std::move(s));
529}
530
532 std::function<bool(size_t offset, std::ostream& sink, bool& finish)> ContentProvider) const
533{
534 return this->adaptor_->UploadImageChunked(this->GetSessionKey_(), "friend", std::move(ContentProvider));
535}
536
537
539{
540 return this->adaptor_->UploadImage(this->GetSessionKey_(), "group", std::move(content));
541}
542
544{
545 string s;
546 char buffer[4096]; // NOLINT(*-avoid-c-arrays)
547 while (file.read(buffer, sizeof(buffer))) // NOLINT(*-array-to-pointer-decay)
548 s.append(buffer, sizeof(buffer)); // NOLINT(*-array-to-pointer-decay)
549 s.append(buffer, file.gcount()); // NOLINT(*-array-to-pointer-decay)
550 return this->UploadGroupImage(std::move(s));
551}
552
554 std::function<bool(size_t offset, std::ostream& sink, bool& finish)> ContentProvider) const
555{
556 return this->adaptor_->UploadImageChunked(this->GetSessionKey_(), "group", std::move(ContentProvider));
557}
558
559
561{
562 return this->adaptor_->UploadImage(this->GetSessionKey_(), "temp", std::move(content));
563}
564
566{
567 string s;
568 char buffer[4096]; // NOLINT(*-avoid-c-arrays)
569 while (file.read(buffer, sizeof(buffer))) // NOLINT(*-array-to-pointer-decay)
570 s.append(buffer, sizeof(buffer)); // NOLINT(*-array-to-pointer-decay)
571 s.append(buffer, file.gcount()); // NOLINT(*-array-to-pointer-decay)
572 return this->UploadTempImage(std::move(s));
573}
574
576MiraiClient::UploadTempImage(std::function<bool(size_t offset, std::ostream& sink, bool& finish)> ContentProvider) const
577{
578 return this->adaptor_->UploadImageChunked(this->GetSessionKey_(), "temp", std::move(ContentProvider));
579}
580
581
583{
584 return this->adaptor_->UploadAudio(this->GetSessionKey_(), "group", content);
585}
586
588{
589 string s;
590 char buffer[4096]; // NOLINT(*-avoid-c-arrays)
591 while (file.read(buffer, sizeof(buffer))) // NOLINT(*-array-to-pointer-decay)
592 s.append(buffer, sizeof(buffer)); // NOLINT(*-array-to-pointer-decay)
593 s.append(buffer, file.gcount()); // NOLINT(*-array-to-pointer-decay)
594 return this->UploadGroupAudio(std::move(s));
595}
596
598 std::function<bool(size_t offset, std::ostream& sink, bool& finish)> ContentProvider) const
599{
600 return this->adaptor_->UploadAudioChunked(this->GetSessionKey_(), "group", std::move(ContentProvider));
601}
602
603
605{
606 return this->adaptor_->DeleteFriend(this->GetSessionKey_(), qq);
607}
608
609void MiraiClient::Mute(GID_t GroupId, QQ_t member, std::chrono::seconds time) const
610{
611 return this->adaptor_->Mute(this->GetSessionKey_(), GroupId, member, time);
612}
613
614void MiraiClient::Mute(const GroupMember& member, std::chrono::seconds time) const
615{
616 return this->adaptor_->Mute(this->GetSessionKey_(), member.group.id, member.id, time);
617}
618
619void MiraiClient::Unmute(GID_t GroupId, QQ_t member) const
620{
621 return this->adaptor_->Unmute(this->GetSessionKey_(), GroupId, member);
622}
623
624void MiraiClient::Unmute(const GroupMember& member) const
625{
626 return this->adaptor_->Unmute(this->GetSessionKey_(), member.group.id, member.id);
627}
628
629void MiraiClient::Kick(GID_t GroupId, QQ_t member, string message, bool block) const
630{
631 return this->adaptor_->Kick(this->GetSessionKey_(), GroupId, member, std::move(message), block);
632}
633
634void MiraiClient::Kick(const GroupMember& member, string message, bool block) const
635{
636 return this->adaptor_->Kick(this->GetSessionKey_(), member.group.id, member.id, std::move(message), block);
637}
638
640{
641 return this->adaptor_->Quit(this->GetSessionKey_(), GroupId);
642}
643
644void MiraiClient::MuteAll(GID_t GroupId) const
645{
646 return this->adaptor_->MuteAll(this->GetSessionKey_(), GroupId);
647}
648
649void MiraiClient::UnmuteAll(GID_t GroupId) const
650{
651 return this->adaptor_->UnmuteAll(this->GetSessionKey_(), GroupId);
652}
653
654void MiraiClient::SetEssence(GID_t GroupId, MessageId_t MessageId) const
655{
656 return this->adaptor_->SetEssence(this->GetSessionKey_(), MessageId, GroupId);
657}
658
659
661{
662 return this->adaptor_->GetGroupConfig(this->GetSessionKey_(), GroupId);
663}
664
665void MiraiClient::SetGroupConfig(GID_t GroupId, string name, std::optional<bool> AllowMemberInvite) const
666{
667 return this->adaptor_->SetGroupConfig(this->GetSessionKey_(), GroupId, std::move(name), std::nullopt, AllowMemberInvite);
668}
669
671{
672 return this->adaptor_->GetMemberInfo(this->GetSessionKey_(), GroupId, member);
673}
674
675void MiraiClient::SetMemberInfo(GID_t GroupId, QQ_t member, string name, string title) const
676{
677 return this->adaptor_->SetMemberInfo(this->GetSessionKey_(), GroupId, member, std::move(name), std::move(title));
678}
679
680void MiraiClient::SetGroupAdmin(GID_t GroupId, QQ_t member, bool assign) const
681{
682 return this->adaptor_->MemberAdmin(this->GetSessionKey_(), GroupId, member, assign);
683}
684
685
686std::vector<GroupAnnouncement> MiraiClient::GetAnnouncementList(GID_t GroupId, int64_t offset, int64_t size) const
687{
688 return this->adaptor_->AnnoList(this->GetSessionKey_(), GroupId, offset, size);
689}
690
692 bool ToNewMember, bool pinned, bool ShowEditCard, bool ShowPopup,
693 bool RequireConfirm) const
694{
695 return this->adaptor_->AnnoPublish(this->GetSessionKey_(), GroupId, std::move(content), std::move(cover.url), std::move(cover.path), std::move(cover.base64),
696 ToNewMember, pinned, ShowEditCard, ShowPopup, RequireConfirm);
697}
698
699void MiraiClient::DeleteAnnouncement(GID_t GroupId, string fid) const
700{
701 return this->adaptor_->AnnoDelete(this->GetSessionKey_(), GroupId, std::move(fid));
702}
703
705{
706 return this->adaptor_->AnnoDelete(this->GetSessionKey_(), announcement.group.id, announcement.fid);
707}
708
709
710void MiraiClient::RespNewFriendRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId, NewFriendRequestOp operation,
711 string message) const
712{
713 return this->adaptor_->RespNewFriendRequestEvent(this->GetSessionKey_(), EventId, FromId, GroupId, (int)operation,
714 std::move(message));
715}
716
718 string message) const
719{
720 return this->adaptor_->RespNewFriendRequestEvent(this->GetSessionKey_(), event.GetEventId(), event.GetUserId(),
721 event.GetGroupId(), (int)operation, std::move(message));
722}
723
724void MiraiClient::RespMemberJoinRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId, MemberJoinRequestOp operation,
725 string message) const
726{
727 return this->adaptor_->RespMemberJoinRequestEvent(this->GetSessionKey_(), EventId, FromId, GroupId, (int)operation,
728 std::move(message));
729}
730
732 string message) const
733{
734 return this->adaptor_->RespMemberJoinRequestEvent(this->GetSessionKey_(), event.GetEventId(), event.GetUserId(),
735 event.GetGroupId(), (int)operation, std::move(message));
736}
737
738void MiraiClient::RespBotInvitedJoinGroupRequestEvent(int64_t EventId, QQ_t FromId, GID_t GroupId,
740 string message) const
741{
742 return this->adaptor_->RespBotInvitedJoinGroupRequestEvent(this->GetSessionKey_(), EventId, FromId, GroupId,
743 (int)operation, std::move(message));
744}
745
748 string message) const
749{
750 return this->adaptor_->RespBotInvitedJoinGroupRequestEvent(
751 this->GetSessionKey_(), event.GetEventId(), event.GetUserId(), event.GetGroupId(), (int)operation, std::move(message));
752}
753
754
755void MiraiClient::RegisterCommand(string name, std::vector<string> alias, string usage,
756 string description) const
757{
758 return this->adaptor_->CmdRegister(this->GetSessionKey_(), std::move(name), std::move(alias), std::move(usage), std::move(description));
759}
760
762{
763 return this->adaptor_->CmdExecute(this->GetSessionKey_(), command);
764}
766{
767 return this->adaptor_->CmdExecute(this->GetSessionKey_(), std::move(command));
768}
769
770
771string MiraiClient::CallAPI(const string& path, const string& method, const string& data) const
772{
773 return this->adaptor_->CallAPI(path, method, data);
774}
775
776} // namespace Mirai
#define LOG_TRACE(logger, msg)
Definition: Logger.hpp:134
#define LOG_DEBUG(logger, msg)
Definition: Logger.hpp:145
QQ_t GetUserId() const
获取邀请人QQ
Definition: BotEvents.hpp:172
GID_t GetGroupId() const
获取群聊id
Definition: BotEvents.hpp:176
int64_t GetEventId() const
获取事件id,唯一标识符
Definition: BotEvents.hpp:170
文件路径
Definition: MediaTypes.hpp:108
std::string GetId() const
获取文件id
Definition: MediaTypes.hpp:155
std::string GetPath() const
获取文件名路径
Definition: MediaTypes.hpp:147
好友消息事件
群聊号码类型
Definition: BasicTypes.hpp:88
群聊消息事件
static constexpr EventTypes GetType()
获取事件类型
Definition: IEvent.hpp:62
用户申请入群事件
QQ_t GetUserId() const
获取申请人QQ
GID_t GetGroupId() const
获取群聊id
int64_t GetEventId() const
获取事件id,唯一标识符
消息链对象,由一系列消息元素组成
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 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
string GetMiraiApiHttpVersion() const
获取mirai-api-http插件的版本号
Definition: Client.cpp:272
MessageId_t SendFriendMessage(QQ_t qq, const MessageChain &message, std::optional< MessageId_t > QuoteId=std::nullopt) const
发送好友消息
Definition: Client.cpp:374
void RemoveGroupFile(GID_t GroupId, const FilePath &dir) const
删除群文件
Definition: Client.cpp:473
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
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
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
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 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
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
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
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
void Unmute(GID_t GroupId, QQ_t member) const
解除群成员禁言
Definition: Client.cpp:619
用户申请添加Bot好友事件
GID_t GetGroupId() const
获取申请人来自的群聊,若无则返回 0_gid
QQ_t GetUserId() const
获取申请人QQ
int64_t GetEventId() const
获取事件id,唯一标识符
QQ移动端头像戳一戳动作的对象
Definition: NudgeTarget.hpp:37
QQ_t GetTarget() const
返回戳一戳的对象
Definition: NudgeTarget.hpp:73
GID_t GetGroup() const
返回戳一戳所在的群聊
Definition: NudgeTarget.hpp:81
NudgeType GetNudgeType() const
返回戳一戳类型
Definition: NudgeTarget.hpp:66
消息解析错误
Definition: Exceptions.hpp:100
QQ号码类型
Definition: BasicTypes.hpp:71
临时会话消息事件
类型匹配错误
Definition: Exceptions.hpp:117
所有mirai相关的对象的命名空间
NewFriendRequestOp
处理好友申请的操作
Definition: BasicTypes.hpp:115
std::string to_string(EventTypes type)
BotInvitedJoinGroupRequestOp
处理被邀请入群的操作
Definition: BasicTypes.hpp:150
nlohmann::json json
Definition: Client.cpp:42
int64_t MessageId_t
消息id类型,用于撤回消息和引用消息
Definition: BasicTypes.hpp:35
MemberJoinRequestOp
处理用户入群申请的操作
Definition: BasicTypes.hpp:133
STL namespace
std::map< std::string, std::string > headers
接收的文件头信息
std::string fid
公告id,唯一标识符
群文件信息
Definition: MediaTypes.hpp:76
std::string id
文件id,唯一标识符
Definition: MediaTypes.hpp:78
群员资料
Definition: BasicTypes.hpp:362
Group group
群聊资料
Definition: BasicTypes.hpp:378
QQ_t id
群员id
Definition: BasicTypes.hpp:364
GID_t id
群聊号码
Definition: BasicTypes.hpp:335
std::string base64
图片base64编码
Definition: MediaTypes.hpp:210
std::string url
图片链接
Definition: MediaTypes.hpp:206
std::string path
图片路径
Definition: MediaTypes.hpp:208
QQ用户资料
Definition: BasicTypes.hpp:409