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

Commit 2fb7645

Browse files
committed
提取分离处理事件函数;
1 parent 51142ae commit 2fb7645

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

include/mirai_bot.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ namespace Cyan
117117
private:
118118
bool SessionVerify();
119119
bool SessionRelease();
120-
unsigned int FetchMessagesAndEvents(unsigned int count = 10);
120+
unsigned int FetchEvents(unsigned int count = 10);
121+
void ProcessEvents(const nlohmann::json& ele);
121122
template<typename T>
122123
inline WeakEvent MakeWeakEvent(const json& json_)
123124
{

src/mirai_bot.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ namespace Cyan
571571
unsigned count = 0;
572572
try
573573
{
574-
count = FetchMessagesAndEvents(count_per_loop);
574+
count = FetchEvents(count_per_loop);
575575
}
576576
catch (const std::exception& ex)
577577
{
@@ -656,7 +656,7 @@ namespace Cyan
656656
}
657657

658658

659-
unsigned int MiraiBot::FetchMessagesAndEvents(unsigned int count)
659+
unsigned int MiraiBot::FetchEvents(unsigned int count)
660660
{
661661
int received_count = 0;
662662
stringstream api_url;
@@ -689,31 +689,33 @@ namespace Cyan
689689
}
690690
for (const auto& ele : reJson["data"])
691691
{
692-
string event_name = ele["type"].get<string>();
693-
MiraiEvent mirai_event = MiraiEventStr(event_name);
694-
// 寻找能处理事件的 Processor
695-
auto pit = processors_.find(mirai_event);
696-
if (pit != processors_.end())
697-
{
698-
auto exector = pit->second;
699-
WeakEvent pevent = CreateEvent(mirai_event, ele);
700-
pool_.enqueue([=]()
701-
{
702-
exector(pevent);
703-
});
704-
705-
}
692+
ProcessEvents(ele);
706693
received_count++;
707694
}
708-
709-
710695
}
711696
else
712697
throw std::runtime_error("网络错误");
713698
return received_count;
714699

715700
}
716701

702+
void MiraiBot::ProcessEvents(const nlohmann::json& ele)
703+
{
704+
string event_name = ele["type"].get<string>();
705+
MiraiEvent mirai_event = MiraiEventStr(event_name);
706+
// 寻找能处理事件的 Processor
707+
auto pit = processors_.find(mirai_event);
708+
if (pit != processors_.end())
709+
{
710+
auto exector = pit->second;
711+
WeakEvent pevent = CreateEvent(mirai_event, ele);
712+
pool_.enqueue([=]()
713+
{
714+
exector(pevent);
715+
});
716+
}
717+
}
718+
717719

718720
WeakEvent MiraiBot::CreateEvent(MiraiEvent mirai_event, const json& json_)
719721
{

0 commit comments

Comments
 (0)