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

Commit 8be3360

Browse files
committed
支持 EventParsingError 事件.
1 parent 50123d8 commit 8be3360

File tree

5 files changed

+108
-20
lines changed

5 files changed

+108
-20
lines changed

examples/RepeatMessage.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ int main(int argc, char* argv[])
7878
cout << "成功与 mirai-api-http 重新建立连接!" << endl;
7979
});
8080

81+
bot.On<EventParsingError>([&](EventParsingError e)
82+
{
83+
try
84+
{
85+
e.Rethrow();
86+
}
87+
catch (const std::exception& ex)
88+
{
89+
cout << "解析事件时出现错误: " << ex.what() << endl;
90+
}
91+
});
92+
8193
string command;
8294
while (cin >> command)
8395
{
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#pragma once
2+
#ifndef mirai_cpp_events_EventParsingError_hpp_H_
3+
#define mirai_cpp_events_EventParsingError_hpp_H_
4+
5+
#include <string>
6+
#include "mirai/third-party/nlohmann/json.hpp"
7+
#include "event_interface.hpp"
8+
using std::string;
9+
namespace Cyan
10+
{
11+
/**
12+
* \brief 解析事件错误 (一般因为与MAH版本不同导致)
13+
*/
14+
class EventParsingError : public EventBase
15+
{
16+
public:
17+
EventParsingError(std::exception_ptr ptr) : eptr(ptr) {}
18+
19+
static MiraiEvent GetMiraiEvent()
20+
{
21+
return MiraiEvent::Default;
22+
}
23+
24+
virtual bool Set(const json& j) override
25+
{
26+
throw std::logic_error("Function not yet implemented.");
27+
}
28+
virtual json ToJson() const override
29+
{
30+
throw std::logic_error("Function not yet implemented.");
31+
}
32+
33+
void Rethrow()
34+
{
35+
if (eptr)
36+
{
37+
std::rethrow_exception(eptr);
38+
}
39+
}
40+
41+
private:
42+
std::exception_ptr eptr;
43+
};
44+
45+
}
46+
47+
#endif

include/mirai/events/events.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,7 @@
5454
// 指令事件
5555
#include "Command.hpp"
5656

57+
#include "LostConnection.hpp"
58+
#include "EventParsingError.hpp"
59+
5760
#endif // !mirai_cpp_events_events_hpp_H_

include/mirai/mirai_bot.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "mirai/events/GroupMessage.hpp"
1919
#include "mirai/events/Message.hpp"
2020
#include "mirai/events/LostConnection.hpp"
21+
#include "mirai/events/EventParsingError.hpp"
2122
#include "mirai/SessionOptions.hpp"
2223

2324
using std::string;
@@ -452,6 +453,7 @@ namespace Cyan
452453
struct pimpl;
453454
pimpl* pmem = nullptr;
454455
EventCallback<LostConnection> lostConnectionCallback;
456+
EventCallback<EventParsingError> eventParsingErrorCallback;
455457
std::unordered_multimap<MiraiEvent, CallbackInvoker> processors;
456458

457459
template <typename T>
@@ -512,6 +514,16 @@ namespace Cyan
512514
return *this;
513515
}
514516

517+
/**
518+
* @brief 对 EventParsingError 的偏特化
519+
*/
520+
template<>
521+
inline MiraiBot& MiraiBot::OnEventReceived<EventParsingError>(const EventCallback<EventParsingError>& cb)
522+
{
523+
eventParsingErrorCallback = cb;
524+
return *this;
525+
}
526+
515527
} // namespace Cyan
516528

517529
#endif // !mirai_cpp__mirai_bot_hpp_H_

src/mirai_bot.cpp

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -202,32 +202,46 @@ namespace Cyan
202202

203203
pmem->eventClient.OnTextReceived([&](WebSocketClient& client, string text)
204204
{
205-
json event_json = json::parse(text)["data"];
206-
if (!event_json.contains("type")) return;
207-
string event_name = event_json["type"].get<string>();
208-
MiraiEvent mirai_event = MiraiEventStr(event_name);
209-
auto range = processors.equal_range(mirai_event);
210-
for (auto& it = range.first; it != range.second; ++it)
205+
try
211206
{
212-
auto& executor = it->second;
213-
// 给 executor 传入 nullptr 可以创建一个 WeakEvent
214-
WeakEvent pevent = executor(nullptr);
215-
pevent->SetMiraiBot(this);
216-
pevent->Set(event_json);
217-
218-
pmem->threadPool->enqueue([=]()
219-
{
220-
executor(pevent);
221-
});
207+
json event_json = json::parse(text)["data"];
208+
if (!event_json.contains("type")) return;
209+
string event_name = event_json["type"].get<string>();
210+
MiraiEvent mirai_event = MiraiEventStr(event_name);
211+
auto range = processors.equal_range(mirai_event);
212+
for (auto& it = range.first; it != range.second; ++it)
213+
{
214+
auto& executor = it->second;
215+
// 给 executor 传入 nullptr 可以创建一个 WeakEvent
216+
WeakEvent pevent = executor(nullptr);
217+
pevent->SetMiraiBot(this);
218+
pevent->Set(event_json);
219+
220+
pmem->threadPool->enqueue([=]()
221+
{
222+
executor(pevent);
223+
});
224+
}
225+
226+
}
227+
catch (...)
228+
{
229+
if (eventParsingErrorCallback)
230+
{
231+
eventParsingErrorCallback(EventParsingError(std::current_exception()));
232+
}
222233
}
223234
});
224235

225236
pmem->eventClient.OnLostConnection([&](WebSocketClient& client, int code)
226237
{
227-
LostConnection result;
228-
result.Code = code;
229-
result.ErrorMessage = "与 mirai-api-http 失去连接.";
230-
lostConnectionCallback(result);
238+
if (lostConnectionCallback)
239+
{
240+
LostConnection result;
241+
result.Code = code;
242+
result.ErrorMessage = "与 mirai-api-http 失去连接.";
243+
lostConnectionCallback(result);
244+
}
231245
});
232246
}
233247

0 commit comments

Comments
 (0)