diff --git a/README.md b/README.md index 9aa123b..f29f4db 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/twobot.cc b/src/twobot.cc index 57a15a0..b43fe9c 100644 --- a/src/twobot.cc +++ b/src/twobot.cc @@ -130,7 +130,6 @@ namespace twobot { }); // 仅仅为了特化onEvent模板 - // TODO: 在这里伪装调用一下,特化一下模板 instance->onEvent([](const auto&) {}); instance->onEvent([](const auto&) {}); instance->onEvent([](const auto&) {}); @@ -140,10 +139,14 @@ namespace twobot { instance->onEvent([](const auto&) {}); instance->onEvent([](const auto&) {}); instance->onEvent([](const auto&) {}); + instance->onEvent([](const auto&) {}); + instance->onEvent([](const auto&) {}); + instance->onEvent([](const auto&) {}); + instance->onEvent([](const auto&) {}); + instance->onEvent([](const auto&) {}); } std::unique_ptr Event::EventBase::construct(const EventType& event) { - // TODO: 这里要根据event的类型来创建对应的Event对象 if (event.post_type == "message") { if (event.sub_type == "group") { return std::unique_ptr(new Event::GroupMsg()); @@ -167,12 +170,21 @@ namespace twobot { return std::unique_ptr(new Event::GroupDecreaseNotice()); } else if (event.sub_type == "group_increase"){ return std::unique_ptr(new Event::GroupInceaseNotice()); + } else if (event.sub_type == "group_ban"){ + return std::unique_ptr( new Event::GroupBanNotice()); + } else if (event.sub_type == "friend_add"){ + return std::unique_ptr(new Event::FriendAddNotice()); + } else if (event.sub_type == "group_recall"){ + return std::unique_ptr(new Event::GroupRecallNotice()); + } else if (event.sub_type == "friend_recall"){ + return std::unique_ptr(new Event::FriendRecallNotice()); + } else if (event.sub_type == "group_notify"){ + return std::unique_ptr(new Event::GroupNotifyNotice()); } } return nullptr; } -// TODO: parse要这边写 void Event::GroupMsg::parse() { this->time = this->raw_msg["time"]; this->user_id = raw_msg["user_id"]; @@ -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; + } + } }; diff --git a/src/twobot.hh b/src/twobot.hh index b28dc16..6d0070c 100644 --- a/src/twobot.hh +++ b/src/twobot.hh @@ -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 target_id = std::nullopt; // 如果是戳一戳,则为被戳的人的QQ,如果是群红包运气王,则为群红包的ID + enum HonorType{ + TALKATIVE, // 龙王 + PERFORMER, // 群聊之火 + EMOTION, // 快乐源泉 + }; + std::optional honor_type = std::nullopt; // 荣誉类型 + protected: + virtual void parse() override; + }; } /// BotInstance是一个机器人实例,机器人实例必须通过BotInstance::createInstance()创建