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

Commit 4d600bb

Browse files
committed
Merge branch 'master' of github.com:cyanray/mirai-cpp
2 parents 6190a40 + db30afc commit 4d600bb

File tree

11 files changed

+294
-28
lines changed

11 files changed

+294
-28
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
| FetchEventsViaHTTP.cpp| 设置通过 HTTP 短轮询获取事件和消息 |
4444
| GroupMemberInfo.cpp | 获取/设置群成员的群名片与群头衔 |
4545
| Command.cpp | 指令系统相关的操作 |
46+
| NudgeEvent.cpp | 戳一戳事件和发送相关的操作 |
4647

4748
## 0x03 如何使用
4849

doc/使用说明.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ MiraiBot 类提供了 On 方法和 OnEventReceived 方法,这两个方法是
116116
| BotLeaveEventActive | 机器人主动退群 |
117117
| BotOfflineEventActive | 机器人主动下线 |
118118
| MemberCardChangeEvent | 群昵称改变(不可靠) |
119+
| NudgeEvent | 戳一戳事件 |
119120

120121

121122

@@ -379,7 +380,7 @@ a += b;
379380
// 或者
380381
MessageChain c = a + b;
381382

382-
// MessageChain 特别处理了 Plain 消息,你可以做到
383+
// MessageChain 特别处理了 Plain 消息,你可以做到将字符串和 MessageChain 直接相加
383384
MessageChain c = "早安!" + b;
384385
```
385386

@@ -501,4 +502,5 @@ bot.On<GroupMessage>(
501502
| RichMessage.cpp | 发送 JSON、闪照等类型的消息 |
502503
| FetchEventsViaHTTP.cpp| 设置通过 HTTP 短轮询获取事件和消息 |
503504
| GroupMemberInfo.cpp | 获取/设置群成员的群名片与群头衔 |
504-
| Command.cpp | 指令系统相关的操作 |
505+
| Command.cpp | 指令系统相关的操作 |
506+
| NudgeEvent.cpp | 戳一戳事件和发送相关的操作 |

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ api_exe(GroupNameChange)
3535
api_exe(GroupConfig)
3636
api_exe(MemberCardChange)
3737
api_exe(Command)
38-
api_exe(VoiceMessage)
38+
api_exe(VoiceMessage)
39+
api_exe(NudgeEvent)

examples/NudgeEvent.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <iostream>
2+
// 使用静态库必须要在引入 mirai.h 前定义这个宏
3+
#define MIRAICPP_STATICLIB
4+
#include <mirai.h>
5+
6+
int main()
7+
{
8+
using namespace std;
9+
using namespace Cyan;
10+
11+
// 源文件使用 UTF-8 编码保存,在 Windows 上需要切换代码页才不会显示乱码
12+
#if defined(WIN32) || defined(_WIN32)
13+
system("chcp 65001");
14+
#endif
15+
16+
// 16 条事件处理线程
17+
MiraiBot bot("127.0.0.1", 8762, 16);
18+
19+
// 检查一下版本
20+
try
21+
{
22+
// 获取 mirai-api-http 插件的版本
23+
string mah_version = bot.GetMiraiApiHttpVersion();
24+
// 获取 mirai-cpp 的版本
25+
string mc_version = bot.GetMiraiCppVersion();
26+
cout << "! mirai-api-http 的版本: " << mah_version
27+
<< "; 当mirai-cpp 的版本: " << mc_version << "; " << endl;
28+
if (mah_version != mc_version)
29+
{
30+
cout << "! 警告: 你的 mirai-api-http 插件的版本与 mirai-cpp 的版本不同,可能存在兼容性问题。" << endl;
31+
}
32+
}
33+
catch (const std::exception& ex)
34+
{
35+
cout << ex.what() << endl;
36+
}
37+
38+
// 自动重试地进行 Auth
39+
while (true)
40+
{
41+
try
42+
{
43+
bot.Auth("AuthKeyASDEWQ", 1589588851_qq);
44+
break;
45+
}
46+
catch (const std::exception& ex)
47+
{
48+
cout << ex.what() << endl;
49+
}
50+
MiraiBot::SleepSeconds(1);
51+
}
52+
cout << "Bot Working..." << endl;
53+
54+
bot.On<NudgeEvent>(
55+
[&](NudgeEvent e)
56+
{
57+
// 注意: 使用 SendNudge 发送的戳一戳,也会触发该事件,
58+
// 注意: 因此必须过滤掉来自bot自己的戳一戳事件,不然会导致死循环
59+
if (e.FromId.ToInt64() == bot.GetBotQQ().ToInt64()) return;
60+
61+
cout << e.FromId.ToInt64() << " " << e.Action << " " << e.Target << " " << e.Suffix;
62+
// 如果别人戳机器人,那么就让机器人戳回去
63+
if (e.Target.ToInt64() != bot.GetBotQQ().ToInt64()) return;
64+
bot.SendNudge(e.FromId, e.SubjectId, e.FromKind);
65+
// 如果不喜欢上面这一行代码,也可以用下面的代码代替
66+
//if (e.FromKind == NudgeEvent::SubjectKind::Group)
67+
//{
68+
// bot.SendNudge(e.FromId, (GID_t)e.SubjectId);
69+
//}
70+
//else
71+
//{
72+
// bot.SendNudge(e.FromId, (QQ_t)e.SubjectId);
73+
//}
74+
});
75+
76+
77+
// 记录轮询事件时的错误
78+
bot.EventLoop([](const char* errMsg)
79+
{
80+
cout << "获取事件时出错: " << errMsg << endl;
81+
});
82+
83+
return 0;
84+
}

include/mirai/defs/qq_types.hpp

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,73 @@
33
#define mirai_cpp_defs_qq_types_hpp_H_
44

55
#include <exception>
6+
#include <iostream>
67
#include "serializable.hpp"
78
#include "mirai/exported.h"
89

910
namespace Cyan
1011
{
11-
// QQ 号码类型
12-
class QQ_t
12+
// QQ_t 和 GID_t 的基类型
13+
class UID_t
1314
{
1415
public:
15-
QQ_t() :QQ(-1) {}
16-
explicit QQ_t(int64_t qq) :QQ(qq) {}
17-
operator int64_t() const { return QQ; }
18-
int64_t ToInt64() const { return QQ; }
19-
private:
20-
int64_t QQ;
16+
int64_t ToInt64() const
17+
{
18+
return data_;
19+
}
20+
explicit operator int64_t() const
21+
{
22+
return data_;
23+
}
24+
bool operator==(const UID_t& a) const
25+
{
26+
return this->data_ == a.data_;
27+
}
28+
bool operator!=(const UID_t& a) const
29+
{
30+
return !operator==(a);
31+
}
32+
virtual ~UID_t() = default;
33+
protected:
34+
explicit UID_t(int64_t id) : data_(id) {}
35+
virtual void __avoiding_object_slicing() const = 0;
36+
int64_t data_;
2137
};
22-
23-
inline QQ_t operator "" _qq(unsigned long long int v)
38+
39+
// 储存 QQ 号码的类型
40+
class QQ_t : public UID_t
2441
{
25-
return QQ_t(int64_t(v));
26-
}
42+
public:
43+
QQ_t() : UID_t(-1) {}
44+
explicit QQ_t(int64_t id) : UID_t(id) {}
45+
private:
46+
virtual void __avoiding_object_slicing() const override {}
47+
};
2748

28-
// 群号码类型
29-
class GID_t
49+
// 储存群号码的类型
50+
class GID_t : public UID_t
3051
{
3152
public:
32-
GID_t() :GID(-1) {}
33-
explicit GID_t(int64_t gid) :GID(gid) {}
34-
operator int64_t() const { return GID; }
35-
int64_t ToInt64() const { return GID; }
53+
GID_t() : UID_t(-1) {}
54+
explicit GID_t(int64_t id) : UID_t(id) {}
3655
private:
37-
int64_t GID;
56+
virtual void __avoiding_object_slicing() const override {}
3857
};
3958

40-
inline GID_t operator "" _gid(unsigned long long int v)
59+
inline GID_t operator ""_gid(unsigned long long int id)
60+
{
61+
return (GID_t)(id);
62+
}
63+
64+
inline QQ_t operator ""_qq(unsigned long long int id)
65+
{
66+
return (QQ_t)(id);
67+
}
68+
69+
inline std::ostream& operator<<(std::ostream& o, const UID_t& uid)
4170
{
42-
return GID_t(int64_t(v));
71+
o << (int64_t)(uid);
72+
return o;
4373
}
4474

4575
// 消息 ID

include/mirai/events/command_event.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace Cyan
4040
{
4141
CommandName = j["name"].get<string>();
4242
Sender = (QQ_t)(j["friend"].get<int64_t>());
43-
GroupId = (GID_t)(QQ_t)(j["group"].get<int64_t>());
43+
GroupId = (GID_t)(j["group"].get<int64_t>());
4444
Args = j["args"].get<vector<string>>();
4545
return true;
4646
}

include/mirai/events/events.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "bot_leave_kick.hpp"
3434
#include "bot_invited_join_group_request_event.hpp"
3535
#include "group_name_change.hpp"
36+
// 其他事件
37+
#include "nudge_event.hpp"
3638
// 一些定义
3739
#include "event_processer.hpp"
3840
#include "mirai_event.hpp"

include/mirai/events/mirai_event.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ namespace Cyan
3737
Message, // 通用消息事件
3838
BotInvitedJoinGroupRequestEvent, // Bot被邀请入群申请
3939
MemberCardChangeEvent, // 群成员群名片被修改事件
40-
Command // 指令事件
40+
Command, // 指令事件
41+
NudgeEvent // 戳一戳(头像)事件
4142
};
4243

4344
inline MiraiEvent MiraiEventStr(const std::string& miraiEvent)
@@ -69,6 +70,7 @@ namespace Cyan
6970
if (miraiEvent == "BotInvitedJoinGroupRequestEvent") return MiraiEvent::BotInvitedJoinGroupRequestEvent;
7071
if (miraiEvent == "MemberCardChangeEvent") return MiraiEvent::MemberCardChangeEvent;
7172
if (miraiEvent == "Command") return MiraiEvent::Command;
73+
if (miraiEvent == "NudgeEvent") return MiraiEvent::NudgeEvent;
7274
return MiraiEvent::Default;
7375
}
7476

@@ -143,6 +145,9 @@ namespace Cyan
143145
case Cyan::MiraiEvent::Command:
144146
result = "Command";
145147
break;
148+
case Cyan::MiraiEvent::NudgeEvent:
149+
result = "NudgeEvent";
150+
break;
146151
default:
147152
result = "Default";
148153
break;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#pragma once
2+
#ifndef mirai_cpp_events_nudge_event_hpp_H_
3+
#define mirai_cpp_events_nudge_event_hpp_H_
4+
5+
#include <exception>
6+
#include "mirai/third-party/nlohmann/json.hpp"
7+
#include "mirai/defs/qq_types.hpp"
8+
#include "event_interface.hpp"
9+
10+
namespace Cyan
11+
{
12+
/**
13+
* \brief 戳一戳(头像)事件
14+
*/
15+
class NudgeEvent : public EventBase
16+
{
17+
public:
18+
enum class SubjectKind
19+
{
20+
Friend, Group
21+
};
22+
23+
QQ_t FromId;
24+
QQ_t Target;
25+
int64_t SubjectId;
26+
SubjectKind FromKind;
27+
string Action;
28+
string Suffix;
29+
30+
31+
static MiraiEvent GetMiraiEvent()
32+
{
33+
return MiraiEvent::NudgeEvent;
34+
}
35+
36+
static string SubjectKindStr(SubjectKind k)
37+
{
38+
switch (k)
39+
{
40+
case Cyan::NudgeEvent::SubjectKind::Friend:
41+
return "Friend";
42+
case Cyan::NudgeEvent::SubjectKind::Group:
43+
return "Group";
44+
}
45+
return "";
46+
}
47+
48+
static SubjectKind SubjectKindStr(const string& k)
49+
{
50+
if (k == "Friend") return SubjectKind::Friend;
51+
if (k == "Group") return SubjectKind::Group;
52+
throw std::runtime_error("Unknown SubjectKind.");
53+
}
54+
55+
virtual bool Set(const json& j) override
56+
{
57+
this->FromId = (QQ_t)(j["fromId"].get<int64_t>());
58+
this->Target = (QQ_t)(j["target"].get<int64_t>());
59+
this->SubjectId = j["subject"]["id"].get<int64_t>();
60+
this->FromKind = SubjectKindStr(j["subject"]["kind"].get<string>());
61+
this->Action = j["action"].get<string>();
62+
this->Suffix = j["suffix"].get<string>();
63+
return true;
64+
}
65+
66+
virtual json ToJson() const override
67+
{
68+
return
69+
{
70+
{ "type", "NudgeEvent" },
71+
{ "fromId", (int64_t)FromId },
72+
{ "target", (int64_t)Target },
73+
{ "subject", { {"id", SubjectId }, { "kind", SubjectKindStr(FromKind) } } },
74+
{ "action", Action },
75+
{ "suffix", Suffix }
76+
};
77+
}
78+
};
79+
80+
}
81+
82+
#endif // !mirai_cpp_events_nudge_event_hpp_H_

0 commit comments

Comments
 (0)