Skip to content

Commit ad6aa61

Browse files
committed
实现了所有的事件
1 parent 3379591 commit ad6aa61

3 files changed

Lines changed: 152 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ git clone https://github.com/TwoBotFramework/TwoBot --recursive
2525
+ [x] 建立项目
2626
+ [x] 实现大致框架
2727
+ [x] 引入需要使用的第三方库
28-
+ [ ] 支持onebot的所有功能
28+
+ [x] 支持onebot的所有功能
2929
- [x] 实现Onebot HttpAPI的调用
30-
- [ ] 实现Onebot WebSocket接收消息
30+
- [x] 实现Onebot WebSocket接收消息
3131
+ [ ] 书写完善的文档
3232

3333
## FAQ

src/twobot.cc

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ namespace twobot {
130130
});
131131

132132
// 仅仅为了特化onEvent模板
133-
// TODO: 在这里伪装调用一下,特化一下模板
134133
instance->onEvent<Event::GroupMsg>([](const auto&) {});
135134
instance->onEvent<Event::PrivateMsg>([](const auto&) {});
136135
instance->onEvent<Event::EnableEvent>([](const auto&) {});
@@ -140,10 +139,14 @@ namespace twobot {
140139
instance->onEvent<Event::GroupAdminNotice>([](const auto&) {});
141140
instance->onEvent<Event::GroupDecreaseNotice>([](const auto&) {});
142141
instance->onEvent<Event::GroupInceaseNotice>([](const auto&) {});
142+
instance->onEvent<Event::GroupBanNotice>([](const auto&) {});
143+
instance->onEvent<Event::FriendAddNotice>([](const auto&) {});
144+
instance->onEvent<Event::GroupRecallNotice>([](const auto&) {});
145+
instance->onEvent<Event::FriendRecallNotice>([](const auto&) {});
146+
instance->onEvent<Event::GroupNotifyNotice>([](const auto&) {});
143147
}
144148

145149
std::unique_ptr<Event::EventBase> Event::EventBase::construct(const EventType& event) {
146-
// TODO: 这里要根据event的类型来创建对应的Event对象
147150
if (event.post_type == "message") {
148151
if (event.sub_type == "group") {
149152
return std::unique_ptr<Event::EventBase>(new Event::GroupMsg());
@@ -167,12 +170,21 @@ namespace twobot {
167170
return std::unique_ptr<Event::EventBase>(new Event::GroupDecreaseNotice());
168171
} else if (event.sub_type == "group_increase"){
169172
return std::unique_ptr<Event::EventBase>(new Event::GroupInceaseNotice());
173+
} else if (event.sub_type == "group_ban"){
174+
return std::unique_ptr<Event::EventBase>( new Event::GroupBanNotice());
175+
} else if (event.sub_type == "friend_add"){
176+
return std::unique_ptr<Event::EventBase>(new Event::FriendAddNotice());
177+
} else if (event.sub_type == "group_recall"){
178+
return std::unique_ptr<Event::EventBase>(new Event::GroupRecallNotice());
179+
} else if (event.sub_type == "friend_recall"){
180+
return std::unique_ptr<Event::EventBase>(new Event::FriendRecallNotice());
181+
} else if (event.sub_type == "group_notify"){
182+
return std::unique_ptr<Event::EventBase>(new Event::GroupNotifyNotice());
170183
}
171184
}
172185
return nullptr;
173186
}
174187

175-
// TODO: parse要这边写
176188
void Event::GroupMsg::parse() {
177189
this->time = this->raw_msg["time"];
178190
this->user_id = raw_msg["user_id"];
@@ -262,4 +274,57 @@ namespace twobot {
262274
this->sub_type = INVITE;
263275
}
264276

277+
void Event::GroupBanNotice::parse(){
278+
this->time = this->raw_msg["time"];
279+
this->self_id = raw_msg["self_id"];
280+
this->group_id = raw_msg["group_id"];
281+
this->user_id = raw_msg["user_id"];
282+
this->operator_id = raw_msg["operator_id"];
283+
this->duration = raw_msg["duration"];
284+
this->sub_type = ( raw_msg["sub_type"] == "ban" ) ? BAN : LIFT_BAN;
285+
}
286+
287+
void Event::FriendAddNotice::parse(){
288+
this->time = this->raw_msg["time"];
289+
this->self_id = raw_msg["self_id"];
290+
this->user_id = raw_msg["user_id"];
291+
}
292+
293+
void Event::FriendRecallNotice::parse(){
294+
this->time = this->raw_msg["time"];
295+
this->self_id = raw_msg["self_id"];
296+
this->user_id = raw_msg["user_id"];
297+
this->message_id = raw_msg["message_id"];
298+
}
299+
300+
void Event::GroupRecallNotice::parse(){
301+
this->time = this->raw_msg["time"];
302+
this->self_id = raw_msg["self_id"];
303+
this->group_id = raw_msg["group_id"];
304+
this->message_id = raw_msg["message_id"];
305+
this->operator_id = raw_msg["operator_id"];
306+
this->user_id = raw_msg["user_id"];
307+
}
308+
309+
void Event::GroupNotifyNotice::parse(){
310+
this->time = this->raw_msg["time"];
311+
this->self_id = raw_msg["self_id"];
312+
this->user_id = raw_msg["user_id"];
313+
this->group_id = raw_msg["group_id"];
314+
if(raw_msg["sub_type"] == "poke"){
315+
this->sub_type = POKE;
316+
this->target_id = raw_msg["target_id"];
317+
} else if(raw_msg["sub_type"] == "lucky_king"){
318+
this->sub_type = LUCKY_KING;
319+
this->target_id = raw_msg["target_id"];
320+
} else{
321+
this->sub_type = HONOR;
322+
if(raw_msg["honor_type"] == "talkative")
323+
this->honor_type = TALKATIVE;
324+
else if(raw_msg["honor_type"] == "performer")
325+
this->honor_type = PERFORMER;
326+
else if(raw_msg["honor_type"] == "emotion")
327+
this->honor_type = EMOTION;
328+
}
329+
}
265330
};

src/twobot.hh

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,88 @@ namespace twobot {
720720
virtual void parse() override;
721721
};
722722

723-
// TODO: 在这里 声明事件类
723+
struct GroupBanNotice : EventBase{
724+
EventType getType() const override{
725+
return {"notice", "group_ban"};
726+
}
727+
uint64_t time; // 事件产生的时间
728+
uint64_t self_id; // 机器人自身QQ
729+
uint64_t group_id; // 群QQ
730+
uint64_t user_id; // 被禁言的人的QQ
731+
uint64_t operator_id; // 操作者QQ 如果是主动禁言,和user_id一致
732+
uint64_t duration; //禁言时长,单位秒
733+
enum {
734+
BAN, // 禁言
735+
LIFT_BAN, // 解除禁言
736+
} sub_type; // 事件子类型,分别表示禁言、解除禁言
737+
protected:
738+
virtual void parse() override;
739+
};
740+
741+
struct FriendAddNotice : EventBase{
742+
EventType getType() const override{
743+
return {"notice", "friend_add"};
744+
}
745+
uint64_t time; // 事件产生的时间
746+
uint64_t self_id; // 机器人自身QQ
747+
uint64_t user_id; // 新添加好友 QQ 号
748+
protected:
749+
virtual void parse() override;
750+
};
751+
752+
// 群消息撤回事件
753+
struct GroupRecallNotice : EventBase{
754+
EventType getType() const override{
755+
return {"notice", "group_recall"};
756+
}
757+
uint64_t time; // 事件产生的时间
758+
uint64_t self_id; // 机器人自身QQ
759+
uint64_t group_id; // 群QQ
760+
uint64_t message_id; // 消息ID
761+
uint64_t user_id; // 发送者QQ
762+
uint64_t operator_id; // 操作者QQ
763+
764+
protected:
765+
virtual void parse() override;
766+
};
767+
768+
// 好友消息撤回事件
769+
struct FriendRecallNotice : EventBase{
770+
EventType getType() const override{
771+
return {"notice", "friend_recall"};
772+
}
773+
uint64_t time; // 事件产生的时间
774+
uint64_t self_id; // 机器人自身QQ
775+
uint64_t user_id; // 发送者QQ
776+
uint64_t message_id; // 消息ID
777+
protected:
778+
virtual void parse() override;
779+
};
780+
781+
// 群内通知事件,如戳一戳、群红包运气王、群成员荣誉变更
782+
struct GroupNotifyNotice : EventBase{
783+
EventType getType() const override{
784+
return {"notice", "group_notify"};
785+
}
786+
uint64_t time; // 事件产生的时间
787+
uint64_t self_id; // 机器人自身QQ
788+
uint64_t group_id; // 群QQ
789+
uint64_t user_id; // 发送者QQ,如戳一戳的发送者,红包的发送者,荣誉变更者
790+
enum {
791+
POKE, //戳一戳
792+
LUCKY_KING, //群红包运气王
793+
HONOR, //群成员荣誉变更
794+
} sub_type; // 事件子类型,分别表示戳一戳、群红包运气王、群成员荣誉变更
795+
std::optional<uint64_t> target_id = std::nullopt; // 如果是戳一戳,则为被戳的人的QQ,如果是群红包运气王,则为群红包的ID
796+
enum HonorType{
797+
TALKATIVE, // 龙王
798+
PERFORMER, // 群聊之火
799+
EMOTION, // 快乐源泉
800+
};
801+
std::optional<HonorType> honor_type = std::nullopt; // 荣誉类型
802+
protected:
803+
virtual void parse() override;
804+
};
724805
}
725806

726807
/// BotInstance是一个机器人实例,机器人实例必须通过BotInstance::createInstance()创建

0 commit comments

Comments
 (0)