Skip to content

Commit

Permalink
实现了所有的事件
Browse files Browse the repository at this point in the history
  • Loading branch information
ScSofts committed Jul 30, 2022
1 parent 3379591 commit ad6aa61
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ git clone https://github.com/TwoBotFramework/TwoBot --recursive
+ [x] 建立项目
+ [x] 实现大致框架
+ [x] 引入需要使用的第三方库
+ [ ] 支持onebot的所有功能
+ [x] 支持onebot的所有功能
- [x] 实现Onebot HttpAPI的调用
- [ ] 实现Onebot WebSocket接收消息
- [x] 实现Onebot WebSocket接收消息
+ [ ] 书写完善的文档

## FAQ
Expand Down
71 changes: 68 additions & 3 deletions src/twobot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ namespace twobot {
});

// 仅仅为了特化onEvent模板
// TODO: 在这里伪装调用一下,特化一下模板
instance->onEvent<Event::GroupMsg>([](const auto&) {});
instance->onEvent<Event::PrivateMsg>([](const auto&) {});
instance->onEvent<Event::EnableEvent>([](const auto&) {});
Expand All @@ -140,10 +139,14 @@ namespace twobot {
instance->onEvent<Event::GroupAdminNotice>([](const auto&) {});
instance->onEvent<Event::GroupDecreaseNotice>([](const auto&) {});
instance->onEvent<Event::GroupInceaseNotice>([](const auto&) {});
instance->onEvent<Event::GroupBanNotice>([](const auto&) {});
instance->onEvent<Event::FriendAddNotice>([](const auto&) {});
instance->onEvent<Event::GroupRecallNotice>([](const auto&) {});
instance->onEvent<Event::FriendRecallNotice>([](const auto&) {});
instance->onEvent<Event::GroupNotifyNotice>([](const auto&) {});
}

std::unique_ptr<Event::EventBase> Event::EventBase::construct(const EventType& event) {
// TODO: 这里要根据event的类型来创建对应的Event对象
if (event.post_type == "message") {
if (event.sub_type == "group") {
return std::unique_ptr<Event::EventBase>(new Event::GroupMsg());
Expand All @@ -167,12 +170,21 @@ namespace twobot {
return std::unique_ptr<Event::EventBase>(new Event::GroupDecreaseNotice());
} else if (event.sub_type == "group_increase"){
return std::unique_ptr<Event::EventBase>(new Event::GroupInceaseNotice());
} else if (event.sub_type == "group_ban"){
return std::unique_ptr<Event::EventBase>( new Event::GroupBanNotice());
} else if (event.sub_type == "friend_add"){
return std::unique_ptr<Event::EventBase>(new Event::FriendAddNotice());
} else if (event.sub_type == "group_recall"){
return std::unique_ptr<Event::EventBase>(new Event::GroupRecallNotice());
} else if (event.sub_type == "friend_recall"){
return std::unique_ptr<Event::EventBase>(new Event::FriendRecallNotice());
} else if (event.sub_type == "group_notify"){
return std::unique_ptr<Event::EventBase>(new Event::GroupNotifyNotice());
}
}
return nullptr;
}

// TODO: parse要这边写
void Event::GroupMsg::parse() {
this->time = this->raw_msg["time"];
this->user_id = raw_msg["user_id"];
Expand Down Expand Up @@ -262,4 +274,57 @@ namespace twobot {
this->sub_type = INVITE;
}

void Event::GroupBanNotice::parse(){
this->time = this->raw_msg["time"];
this->self_id = raw_msg["self_id"];
this->group_id = raw_msg["group_id"];
this->user_id = raw_msg["user_id"];
this->operator_id = raw_msg["operator_id"];
this->duration = raw_msg["duration"];
this->sub_type = ( raw_msg["sub_type"] == "ban" ) ? BAN : LIFT_BAN;
}

void Event::FriendAddNotice::parse(){
this->time = this->raw_msg["time"];
this->self_id = raw_msg["self_id"];
this->user_id = raw_msg["user_id"];
}

void Event::FriendRecallNotice::parse(){
this->time = this->raw_msg["time"];
this->self_id = raw_msg["self_id"];
this->user_id = raw_msg["user_id"];
this->message_id = raw_msg["message_id"];
}

void Event::GroupRecallNotice::parse(){
this->time = this->raw_msg["time"];
this->self_id = raw_msg["self_id"];
this->group_id = raw_msg["group_id"];
this->message_id = raw_msg["message_id"];
this->operator_id = raw_msg["operator_id"];
this->user_id = raw_msg["user_id"];
}

void Event::GroupNotifyNotice::parse(){
this->time = this->raw_msg["time"];
this->self_id = raw_msg["self_id"];
this->user_id = raw_msg["user_id"];
this->group_id = raw_msg["group_id"];
if(raw_msg["sub_type"] == "poke"){
this->sub_type = POKE;
this->target_id = raw_msg["target_id"];
} else if(raw_msg["sub_type"] == "lucky_king"){
this->sub_type = LUCKY_KING;
this->target_id = raw_msg["target_id"];
} else{
this->sub_type = HONOR;
if(raw_msg["honor_type"] == "talkative")
this->honor_type = TALKATIVE;
else if(raw_msg["honor_type"] == "performer")
this->honor_type = PERFORMER;
else if(raw_msg["honor_type"] == "emotion")
this->honor_type = EMOTION;
}
}
};
83 changes: 82 additions & 1 deletion src/twobot.hh
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,88 @@ namespace twobot {
virtual void parse() override;
};

// TODO: 在这里 声明事件类
struct GroupBanNotice : EventBase{
EventType getType() const override{
return {"notice", "group_ban"};
}
uint64_t time; // 事件产生的时间
uint64_t self_id; // 机器人自身QQ
uint64_t group_id; // 群QQ
uint64_t user_id; // 被禁言的人的QQ
uint64_t operator_id; // 操作者QQ 如果是主动禁言,和user_id一致
uint64_t duration; //禁言时长,单位秒
enum {
BAN, // 禁言
LIFT_BAN, // 解除禁言
} sub_type; // 事件子类型,分别表示禁言、解除禁言
protected:
virtual void parse() override;
};

struct FriendAddNotice : EventBase{
EventType getType() const override{
return {"notice", "friend_add"};
}
uint64_t time; // 事件产生的时间
uint64_t self_id; // 机器人自身QQ
uint64_t user_id; // 新添加好友 QQ 号
protected:
virtual void parse() override;
};

// 群消息撤回事件
struct GroupRecallNotice : EventBase{
EventType getType() const override{
return {"notice", "group_recall"};
}
uint64_t time; // 事件产生的时间
uint64_t self_id; // 机器人自身QQ
uint64_t group_id; // 群QQ
uint64_t message_id; // 消息ID
uint64_t user_id; // 发送者QQ
uint64_t operator_id; // 操作者QQ

protected:
virtual void parse() override;
};

// 好友消息撤回事件
struct FriendRecallNotice : EventBase{
EventType getType() const override{
return {"notice", "friend_recall"};
}
uint64_t time; // 事件产生的时间
uint64_t self_id; // 机器人自身QQ
uint64_t user_id; // 发送者QQ
uint64_t message_id; // 消息ID
protected:
virtual void parse() override;
};

// 群内通知事件,如戳一戳、群红包运气王、群成员荣誉变更
struct GroupNotifyNotice : EventBase{
EventType getType() const override{
return {"notice", "group_notify"};
}
uint64_t time; // 事件产生的时间
uint64_t self_id; // 机器人自身QQ
uint64_t group_id; // 群QQ
uint64_t user_id; // 发送者QQ,如戳一戳的发送者,红包的发送者,荣誉变更者
enum {
POKE, //戳一戳
LUCKY_KING, //群红包运气王
HONOR, //群成员荣誉变更
} sub_type; // 事件子类型,分别表示戳一戳、群红包运气王、群成员荣誉变更
std::optional<uint64_t> target_id = std::nullopt; // 如果是戳一戳,则为被戳的人的QQ,如果是群红包运气王,则为群红包的ID
enum HonorType{
TALKATIVE, // 龙王
PERFORMER, // 群聊之火
EMOTION, // 快乐源泉
};
std::optional<HonorType> honor_type = std::nullopt; // 荣誉类型
protected:
virtual void parse() override;
};
}

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

0 comments on commit ad6aa61

Please sign in to comment.