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

适配 mah v2.5.0 其他客户端同步消息; #139

Merged
merged 2 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion examples/RepeatMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// 按需引用头文件
// 你也可以使用 #include <mirai.h> 引用所有头文件(可能导致编译缓慢)
#include <mirai/MiraiBot.hpp>
#include <mirai/events/OtherClientMessage.hpp>
#include <mirai/events/FriendSyncMessage.hpp>
using namespace std;
using namespace Cyan;

Expand Down Expand Up @@ -55,7 +57,17 @@ int main(int argc, char* argv[])
[&](Message m)
{
cout << int64_t(m.Sender) << " 发来一条消息." << m.MessageChain.ToString() << endl;
m.Reply(m.MessageChain);
// m.Reply(m.MessageChain);
});

bot.On<OtherClientMessage>([&](OtherClientMessage m)
{
cout << m.MessageChain.ToString() << endl;
});

bot.On<FriendSyncMessage>([&](FriendSyncMessage m)
{
cout << m.MessageChain.ToString() << endl;
});

bot.On<LostConnection>([&](LostConnection e)
Expand Down
53 changes: 53 additions & 0 deletions include/mirai/events/FriendSyncMessage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#ifndef mirai_cpp_events_FriendSyncMessage_hpp_H_
#define mirai_cpp_events_FriendSyncMessage_hpp_H_

#include "mirai/third-party/nlohmann/json.hpp"
#include "mirai/defs/QQType.hpp"
#include "mirai/defs/MessageChain.hpp"
#include "mirai/defs/Friend.hpp"
#include "EventBase.hpp"

namespace Cyan
{
/**
* \brief Bot在其他客户端发送给好友的消息
*/
class FriendSyncMessage : public EventBase
{
public:
Cyan::MessageChain MessageChain;
Friend_t Subject;

static MiraiEvent GetMiraiEvent()
{
return MiraiEvent::FriendSyncMessage;
}

MessageId_t MessageId() const
{
return (this->MessageChain).MessageId();
}

int64_t Timestamp() const
{
return (this->MessageChain).Timestamp();
}

virtual bool Set(const json& j) override
{
this->MessageChain.Set(j["messageChain"]);
Subject.Set(j["subject"]);
return true;
}
virtual json ToJson() const override
{
json j = json::object();
j["messageChain"] = this->MessageChain.ToJson();
j["subject"] = this->Subject.ToJson();
return j;
}
};
}

#endif
53 changes: 53 additions & 0 deletions include/mirai/events/GroupSyncMessage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#ifndef mirai_cpp_events_GroupSyncMessage_hpp_H_
#define mirai_cpp_events_GroupSyncMessage_hpp_H_

#include "mirai/third-party/nlohmann/json.hpp"
#include "mirai/defs/QQType.hpp"
#include "mirai/defs/MessageChain.hpp"
#include "mirai/defs/Group.hpp"
#include "EventBase.hpp"

namespace Cyan
{
/**
* \brief Bot在其他客户端发送给群组的消息
*/
class GroupSyncMessage : public EventBase
{
public:
Cyan::MessageChain MessageChain;
Group_t Subject;

static MiraiEvent GetMiraiEvent()
{
return MiraiEvent::GroupSyncMessage;
}

MessageId_t MessageId() const
{
return (this->MessageChain).MessageId();
}

int64_t Timestamp() const
{
return (this->MessageChain).Timestamp();
}

virtual bool Set(const json& j) override
{
this->MessageChain.Set(j["messageChain"]);
Subject.Set(j["subject"]);
return true;
}
virtual json ToJson() const override
{
json j = json::object();
j["messageChain"] = this->MessageChain.ToJson();
j["subject"] = this->Subject.ToJson();
return j;
}
};
}

#endif
106 changes: 55 additions & 51 deletions include/mirai/events/MiraiEvent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,68 @@
namespace Cyan
{

// Mirai 事件类型
enum class MiraiEvent
{
Default, // 未知
BotOnlineEvent, // Bot 登录成功
BotOfflineEventActive, // Bot 主动离线
BotOfflineEventForce, // Bot 被挤下线
BotOfflineEventDropped, // Bot 被挤下线
BotReloginEvent, // Bot 主动重新登录
FriendMessage, // 好友消息
GroupMessage, // 群组消息
TempMessage, // 临时消息
GroupRecallEvent, // 群消息撤回
FriendRecallEvent, // 好友消息撤回
BotMuteEvent, // Bot被禁言
BotUnmuteEvent, // Bot被取消禁言
BotJoinGroupEvent, // Bot加入了一个新群
GroupNameChangeEvent, // 某个群名称改变
GroupMuteAllEvent, // 群全员禁言
MemberJoinEvent, // 新人入群事件
MemberLeaveEventKick, // 成员被踢出群(该成员不是Bot)
MemberLeaveEventQuit, // 成员主动离开群组
MemberMuteEvent, // 群成员被禁言(该成员不是Bot)
MemberUnmuteEvent, // 群成员被取消禁言(该成员不是Bot)
NewFriendRequestEvent, // 添加好友申请
MemberJoinRequestEvent, // 用户入群申请
BotLeaveEventActive, // Bot 主动离开群
BotLeaveEventKick, // Bot 被剔出群
Message, // 通用消息事件
BotInvitedJoinGroupRequestEvent, // Bot被邀请入群申请
MemberCardChangeEvent, // 群成员群名片被修改事件
CommandExecutedEvent, // 指令事件
NudgeEvent, // 戳一戳(头像)事件
StrangerMessage, // 陌生人消息
OtherClientMessage, // 其他客户端消息
FriendInputStatusChangedEvent, // 好友输入状态改变事件
FriendNickChangedEvent, // 好友昵称改变事件
GroupEntranceAnnouncementChangeEvent, // 某群入群公告改变
GroupAllowAnonymousChatEvent, // 群匿名聊天权限改变
GroupAllowConfessTalkEvent, // 群坦白说权限改变
GroupAllowMemberInviteEvent, // 群员邀请好友加群权限改变
MemberSpecialTitleChangeEvent, // 群头衔改动事件
BotGroupPermissionChangeEvent, // bot 群权限改变事件
MemberPermissionChangeEvent, // 群成员权限改变事件
MemberHonorChangeEvent // 群成员称号改变事件
};
// Mirai 事件类型
enum class MiraiEvent
{
Default, // 未知
BotOnlineEvent, // Bot 登录成功
BotOfflineEventActive, // Bot 主动离线
BotOfflineEventForce, // Bot 被挤下线
BotOfflineEventDropped, // Bot 被挤下线
BotReloginEvent, // Bot 主动重新登录
FriendMessage, // 好友消息
GroupMessage, // 群组消息
TempMessage, // 临时消息
GroupRecallEvent, // 群消息撤回
FriendRecallEvent, // 好友消息撤回
BotMuteEvent, // Bot被禁言
BotUnmuteEvent, // Bot被取消禁言
BotJoinGroupEvent, // Bot加入了一个新群
GroupNameChangeEvent, // 某个群名称改变
GroupMuteAllEvent, // 群全员禁言
MemberJoinEvent, // 新人入群事件
MemberLeaveEventKick, // 成员被踢出群(该成员不是Bot)
MemberLeaveEventQuit, // 成员主动离开群组
MemberMuteEvent, // 群成员被禁言(该成员不是Bot)
MemberUnmuteEvent, // 群成员被取消禁言(该成员不是Bot)
NewFriendRequestEvent, // 添加好友申请
MemberJoinRequestEvent, // 用户入群申请
BotLeaveEventActive, // Bot 主动离开群
BotLeaveEventKick, // Bot 被剔出群
Message, // 通用消息事件
BotInvitedJoinGroupRequestEvent, // Bot被邀请入群申请
MemberCardChangeEvent, // 群成员群名片被修改事件
CommandExecutedEvent, // 指令事件
NudgeEvent, // 戳一戳(头像)事件
StrangerMessage, // 陌生人消息
OtherClientMessage, // 其他客户端消息
FriendInputStatusChangedEvent, // 好友输入状态改变事件
FriendNickChangedEvent, // 好友昵称改变事件
GroupEntranceAnnouncementChangeEvent, // 某群入群公告改变
GroupAllowAnonymousChatEvent, // 群匿名聊天权限改变
GroupAllowConfessTalkEvent, // 群坦白说权限改变
GroupAllowMemberInviteEvent, // 群员邀请好友加群权限改变
MemberSpecialTitleChangeEvent, // 群头衔改动事件
BotGroupPermissionChangeEvent, // bot 群权限改变事件
MemberPermissionChangeEvent, // 群成员权限改变事件
MemberHonorChangeEvent, // 群成员称号改变事件
FriendSyncMessage, // Bot在其他客户端发送给好友的消息
GroupSyncMessage, // Bot在其他客户端发送给群组的消息
TempSyncMessage, // Bot在其他客户端发送给群成员的临时消息
StrangerSyncMessage // Bot在其他客户端发送给陌生人的消息
};

MiraiEvent MiraiEventStr(const std::string& miraiEvent);
MiraiEvent MiraiEventStr(const std::string& miraiEvent);

std::string MiraiEventStr(MiraiEvent miraiEvent);
std::string MiraiEventStr(MiraiEvent miraiEvent);

}

// https://stackoverflow.com/questions/18837857/cant-use-enum-class-as-unordered-map-key
namespace std {
template <> struct hash<Cyan::MiraiEvent> {
size_t operator() (const Cyan::MiraiEvent& t) const { return size_t(t); }
};
template <> struct hash<Cyan::MiraiEvent> {
size_t operator() (const Cyan::MiraiEvent& t) const { return size_t(t); }
};
}

#endif // !mirai_cpp_events_events_name_hpp_H_
53 changes: 53 additions & 0 deletions include/mirai/events/StrangerSyncMessage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#ifndef mirai_cpp_events_StrangerSyncMessage_hpp_H_
#define mirai_cpp_events_StrangerSyncMessage_hpp_H_

#include "mirai/third-party/nlohmann/json.hpp"
#include "mirai/defs/QQType.hpp"
#include "mirai/defs/MessageChain.hpp"
#include "mirai/defs/Friend.hpp"
#include "EventBase.hpp"

namespace Cyan
{
/**
* \brief Bot在其他客户端发送给好友的消息
*/
class StrangerSyncMessage : public EventBase
{
public:
Cyan::MessageChain MessageChain;
Friend_t Subject;

static MiraiEvent GetMiraiEvent()
{
return MiraiEvent::StrangerSyncMessage;
}

MessageId_t MessageId() const
{
return (this->MessageChain).MessageId();
}

int64_t Timestamp() const
{
return (this->MessageChain).Timestamp();
}

virtual bool Set(const json& j) override
{
this->MessageChain.Set(j["messageChain"]);
Subject.Set(j["subject"]);
return true;
}
virtual json ToJson() const override
{
json j = json::object();
j["messageChain"] = this->MessageChain.ToJson();
j["subject"] = this->Subject.ToJson();
return j;
}
};
}

#endif
53 changes: 53 additions & 0 deletions include/mirai/events/TempSyncMessage.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#pragma once
#ifndef mirai_cpp_events_TempSyncMessage_hpp_H_
#define mirai_cpp_events_TempSyncMessage_hpp_H_

#include "mirai/third-party/nlohmann/json.hpp"
#include "mirai/defs/QQType.hpp"
#include "mirai/defs/MessageChain.hpp"
#include "mirai/defs/GroupMember.hpp"
#include "EventBase.hpp"

namespace Cyan
{
/**
* \brief Bot在其他客户端发送给群组的消息
*/
class TempSyncMessage : public EventBase
{
public:
Cyan::MessageChain MessageChain;
GroupMember Subject;

static MiraiEvent GetMiraiEvent()
{
return MiraiEvent::TempSyncMessage;
}

MessageId_t MessageId() const
{
return (this->MessageChain).MessageId();
}

int64_t Timestamp() const
{
return (this->MessageChain).Timestamp();
}

virtual bool Set(const json& j) override
{
this->MessageChain.Set(j["messageChain"]);
Subject.Set(j["subject"]);
return true;
}
virtual json ToJson() const override
{
json j = json::object();
j["messageChain"] = this->MessageChain.ToJson();
j["subject"] = this->Subject.ToJson();
return j;
}
};
}

#endif
4 changes: 4 additions & 0 deletions include/mirai/events/events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include "TempMessage.hpp"
#include "StrangerMessage.hpp"
#include "OtherClientMessage.hpp"
#include "FriendSyncMessage.hpp"
#include "GroupSyncMessage.hpp"
#include "TempSyncMessage.hpp"
#include "StrangerSyncMessage.hpp"
// 机器人相关事件
#include "BotOnlineEvent.hpp"
#include "BotOfflineEventActive.hpp"
Expand Down