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

Commit 5d97047

Browse files
committed
支持 新成员入群申请事件;
1 parent ff9cf28 commit 5d97047

File tree

7 files changed

+175
-13
lines changed

7 files changed

+175
-13
lines changed

examples/NewFriendEvent.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,20 @@ int main()
2020
}
2121
MiraiBot::SleepSeconds(1);
2222
}
23-
cout << "³É¹¦µÇ¼ bot¡£" << endl;
23+
cout << "成功登录 bot" << endl;
2424

25-
bot.OnEventReceived<NewFriendEvent>(
26-
[&](NewFriendEvent newFriend)
25+
bot.OnEventReceived<NewFriendRequestEvent>(
26+
[&](NewFriendRequestEvent newFriend)
2727
{
2828
newFriend.Accept();
2929
});
3030

31+
bot.OnEventReceived<MemberJoinRequestEvent>(
32+
[&](MemberJoinRequestEvent newMember)
33+
{
34+
newMember.Reject("sorry!");
35+
});
36+
3137

3238

3339
try

include/event_func.hpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ namespace Cyan
4343
return bot_->Recall(GetMessageId());
4444
}
4545

46-
bool NewFriendEvent::Respose(int operate, const string& message)
46+
bool NewFriendRequestEvent::Respose(int operate, const string& message)
4747
{
4848
static const string api_url = bot_->GetApiUrlPrefix() + "/resp/newFriendRequestEvent";
4949

@@ -68,5 +68,31 @@ namespace Cyan
6868

6969
}
7070

71+
bool MemberJoinRequestEvent::Respose(int operate, const string& message)
72+
{
73+
static const string api_url = bot_->GetApiUrlPrefix() + "/resp/memberJoinRequestEvent";
74+
75+
json j;
76+
j["sessionKey"] = bot_->GetSessionKey();
77+
j["eventId"] = this->EventId;
78+
j["fromId"] = (int64_t)this->FromId;
79+
j["groupId"] = (int64_t)this->GroupId;
80+
j["operate"] = operate;
81+
j["message"] = message;
82+
83+
string pData = j.dump();
84+
HTTP http; http.SetContentType("application/json;charset=UTF-8");
85+
auto res = http.Post(api_url, pData);
86+
87+
if (res.Ready)
88+
{
89+
return true;
90+
}
91+
else
92+
throw runtime_error(res.ErrorMsg);
93+
94+
}
95+
96+
7197
}
7298
#endif // !mirai_cpp__event_func_hpp_H_

include/events/events.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "temp_message.hpp"
99
// 其他事件
1010
#include "new_friend_event.hpp"
11+
#include "member_join_event.hpp"
1112
// 一些定义
1213
#include "event_processer.hpp"
1314
#include "events_name.hpp"

include/events/events_name.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "group_message.hpp"
88
#include "temp_message.hpp"
99
#include "new_friend_event.hpp"
10+
#include "member_join_event.hpp"
1011

1112
namespace Cyan
1213
{
@@ -40,6 +41,7 @@ namespace Cyan
4041
if (miraiEvent == "GroupMessage") return MiraiEvent::GroupMessage;
4142
if (miraiEvent == "TempMessage") return MiraiEvent::TempMessage;
4243
if (miraiEvent == "NewFriendRequestEvent") return MiraiEvent::NewFriendRequestEvent;
44+
if (miraiEvent == "MemberJoinRequestEvent") return MiraiEvent::MemberJoinRequestEvent;
4345
return MiraiEvent::Default;
4446
}
4547

@@ -60,6 +62,9 @@ namespace Cyan
6062
case Cyan::MiraiEvent::NewFriendRequestEvent:
6163
result = "NewFriendRequestEvent";
6264
break;
65+
case Cyan::MiraiEvent::MemberJoinRequestEvent:
66+
result = "MemberJoinRequestEvent";
67+
break;
6368
default:
6469
result = "Default";
6570
break;
@@ -93,11 +98,17 @@ namespace Cyan
9398
}
9499

95100
template<>
96-
MiraiEvent GetEventName<NewFriendEvent>()
101+
MiraiEvent GetEventName<NewFriendRequestEvent>()
97102
{
98103
return MiraiEvent::NewFriendRequestEvent;
99104
}
100105

106+
template<>
107+
MiraiEvent GetEventName<MemberJoinRequestEvent>()
108+
{
109+
return MiraiEvent::MemberJoinRequestEvent;
110+
}
111+
101112
}
102113

103114

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#pragma once
2+
#ifndef mirai_cpp_events_member_join_event_hpp_H_
3+
#define mirai_cpp_events_member_join_event_hpp_H_
4+
5+
#include <nlohmann/json.hpp>
6+
#include "CURLWrapper.h"
7+
#include "defs/qq_types.hpp"
8+
#include "defs/serializable.hpp"
9+
#include "defs/message_chain.hpp"
10+
#include "defs/friend.hpp"
11+
12+
namespace Cyan
13+
{
14+
// 新成员入群请求
15+
class MemberJoinRequestEvent : public Serializable
16+
{
17+
public:
18+
int64_t EventId;
19+
QQ_t FromId;
20+
GID_t GroupId;
21+
string GroupName;
22+
string Nick;
23+
string Message;
24+
25+
MemberJoinRequestEvent() = default;
26+
MemberJoinRequestEvent(const MemberJoinRequestEvent& gm)
27+
{
28+
EventId = gm.EventId;
29+
FromId = gm.FromId;
30+
GroupId = gm.GroupId;
31+
Nick = gm.Nick;
32+
GroupName = gm.GroupName;
33+
Message = gm.Message;
34+
bot_ = gm.bot_;
35+
}
36+
MemberJoinRequestEvent& operator=(const MemberJoinRequestEvent& t)
37+
{
38+
MemberJoinRequestEvent tmp(t);
39+
std::swap(this->EventId, tmp.EventId);
40+
std::swap(this->FromId, tmp.FromId);
41+
std::swap(this->GroupId, tmp.GroupId);
42+
std::swap(this->GroupName, tmp.GroupName);
43+
std::swap(this->Nick, tmp.Nick);
44+
std::swap(this->Message, tmp.Message);
45+
std::swap(this->bot_, tmp.bot_);
46+
return *this;
47+
}
48+
49+
void SetMiraiBot(MiraiBot* bot)
50+
{
51+
this->bot_ = bot;
52+
}
53+
54+
bool Accept()
55+
{
56+
return Respose(0, "");
57+
}
58+
59+
bool Reject(const string& message = "")
60+
{
61+
return Respose(1, message);
62+
}
63+
64+
bool Ignore(const string& message = "")
65+
{
66+
return Respose(2, message);
67+
}
68+
69+
bool RejectAndBlock(const string& message = "")
70+
{
71+
return Respose(3, message);
72+
}
73+
74+
bool IgnoreAndBlock(const string& message = "")
75+
{
76+
return Respose(4, message);
77+
}
78+
79+
80+
virtual ~MemberJoinRequestEvent() = default;
81+
virtual bool Set(const json& j) override
82+
{
83+
this->EventId = j["eventId"].get<int64_t>();
84+
this->FromId = (QQ_t)j["fromId"].get<int64_t>();
85+
this->GroupName = (GID_t)j["groupName"].get<int64_t>();
86+
this->GroupId = (GID_t)j["groupId"].get<int64_t>();
87+
this->Nick = j["nick"].get<string>();
88+
this->Message = j["message"].get<string>();
89+
return true;
90+
}
91+
virtual json ToJson() const override
92+
{
93+
json j = json::object();
94+
j["type"] = "MemberJoinRequestEvent";
95+
j["eventId"] = this->EventId;
96+
j["fromId"] = (int64_t)this->FromId;
97+
j["groupId"] = (int64_t)this->GroupId;
98+
j["groupName"] = this->GroupName;
99+
j["nick"] = this->Nick;
100+
j["message"] = this->Message;
101+
return j;
102+
}
103+
104+
private:
105+
MiraiBot* bot_;
106+
bool Respose(int operate, const string& message);
107+
};
108+
109+
}
110+
111+
#endif // !mirai_cpp_events_member_join_event_hpp_H_

include/events/new_friend_event.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Cyan
1313
{
14-
// кÃÓÑÇëÇóʼþ
15-
class NewFriendEvent : public Serializable
14+
// 新好友请求事件
15+
class NewFriendRequestEvent : public Serializable
1616
{
1717
public:
1818
int64_t EventId;
@@ -21,8 +21,8 @@ namespace Cyan
2121
string Nick;
2222
string Message;
2323

24-
NewFriendEvent() = default;
25-
NewFriendEvent(const NewFriendEvent& gm)
24+
NewFriendRequestEvent() = default;
25+
NewFriendRequestEvent(const NewFriendRequestEvent& gm)
2626
{
2727
EventId = gm.EventId;
2828
FromId = gm.FromId;
@@ -31,9 +31,9 @@ namespace Cyan
3131
Message = gm.Message;
3232
bot_ = gm.bot_;
3333
}
34-
NewFriendEvent& operator=(const NewFriendEvent& t)
34+
NewFriendRequestEvent& operator=(const NewFriendRequestEvent& t)
3535
{
36-
NewFriendEvent tmp(t);
36+
NewFriendRequestEvent tmp(t);
3737
std::swap(this->EventId, tmp.EventId);
3838
std::swap(this->FromId, tmp.FromId);
3939
std::swap(this->GroupId, tmp.GroupId);
@@ -63,7 +63,7 @@ namespace Cyan
6363
return Respose(2, message);
6464
}
6565

66-
virtual ~NewFriendEvent() = default;
66+
virtual ~NewFriendRequestEvent() = default;
6767
virtual bool Set(const json& j) override
6868
{
6969
this->EventId = j["eventId"].get<int64_t>();

include/mirai_bot.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,14 @@ namespace Cyan
673673
}
674674
if (mirai_event == MiraiEvent::NewFriendRequestEvent)
675675
{
676-
std::shared_ptr<NewFriendEvent> tm = std::make_shared<NewFriendEvent>();
676+
std::shared_ptr<NewFriendRequestEvent> tm = std::make_shared<NewFriendRequestEvent>();
677+
tm->SetMiraiBot(this);
678+
tm->Set(json_);
679+
return std::dynamic_pointer_cast<Serializable>(tm);
680+
}
681+
if (mirai_event == MiraiEvent::MemberJoinRequestEvent)
682+
{
683+
std::shared_ptr<MemberJoinRequestEvent> tm = std::make_shared<MemberJoinRequestEvent>();
677684
tm->SetMiraiBot(this);
678685
tm->Set(json_);
679686
return std::dynamic_pointer_cast<Serializable>(tm);

0 commit comments

Comments
 (0)