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

Commit cfb48a1

Browse files
authored
Merge pull request #85 from cyanray/dev/cyanray
新增: 支持戳一戳事件和发送
2 parents 37692d6 + fb9b383 commit cfb48a1

File tree

7 files changed

+237
-4
lines changed

7 files changed

+237
-4
lines changed

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/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_

include/mirai/mirai_bot.hpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
#include "mirai/events/friend_message.hpp"
2626
#include "mirai/events/group_message.hpp"
2727
#include "mirai/events/message_event.hpp"
28-
#include <mirai/events/lost_connection.hpp>
28+
#include "mirai/events/nudge_event.hpp"
29+
#include "mirai/events/lost_connection.hpp"
2930

3031
using std::string;
3132
using std::vector;
@@ -73,7 +74,7 @@ namespace Cyan
7374
string GetMiraiCppVersion() const
7475
{
7576
// mirai-api-http v1.6.5
76-
return "1.9.7";
77+
return "1.9.10";
7778
}
7879

7980
/**
@@ -128,6 +129,25 @@ namespace Cyan
128129
* \return 用于引用或撤回的消息 ID (MessageId)
129130
*/
130131
MessageId_t SendMessage(GID_t gid, QQ_t qq, const MessageChain& messageChain, MessageId_t msgId = 0);
132+
/**
133+
* @brief 发送戳一戳
134+
* @param target 目标QQ,可以是好友或者Bot的QQ
135+
* @param subject_id 戳一戳接收主体,好友QQ
136+
*/
137+
void SendNudge(QQ_t target, QQ_t subject_id);
138+
/**
139+
* @brief 发送戳一戳
140+
* @param target 目标QQ,可以是好友或者Bot的QQ
141+
* @param subject_id 戳一戳接收主体,群号码
142+
*/
143+
void SendNudge(QQ_t target, GID_t subject_id);
144+
/**
145+
* @brief 发送戳一戳
146+
* @param target 目标QQ,可以是好友或者Bot的QQ
147+
* @param subject_id 戳一戳接收主体,可以是QQ好友或者群号码
148+
* @param kind 需要指定接收主体的类型
149+
*/
150+
void SendNudge(QQ_t target, int64_t subject_id, NudgeEvent::SubjectKind kind);
131151
/**
132152
* \brief 上传可以发送给好友的图片
133153
* \param fileName 文件名
@@ -362,6 +382,7 @@ namespace Cyan
362382
void EventLoop(function<void(const char*)> errLogger = nullptr);
363383
private:
364384
// 私有成员函数
385+
void SendNudge(int64_t target, int64_t subject_id, const string& kind);
365386
bool SessionVerify();
366387
bool SessionRelease();
367388
bool SessionConfigure(int cacheSize, bool enableWebsocket);

src/mirai_bot.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,44 @@ namespace Cyan
195195
throw runtime_error(msg);
196196
}
197197

198+
void MiraiBot::SendNudge(int64_t target, int64_t subject_id, const string& kind)
199+
{
200+
json data =
201+
{
202+
{ "sessionKey", sessionKey_ },
203+
{ "target", target },
204+
{ "subject", subject_id },
205+
{ "kind" , kind }
206+
};
207+
208+
auto res = http_client_.Post("/sendNudge", data.dump(), "application/json;charset=UTF-8");
209+
if (!res)
210+
throw std::runtime_error("网络错误");
211+
if (res->status != 200)
212+
throw std::runtime_error("[mirai-api-http error]: " + res->body);
213+
json re_json = json::parse(res->body);
214+
int code = re_json["code"].get<int>();
215+
if (code != 0)
216+
{
217+
string msg = re_json["msg"].get<string>();
218+
throw runtime_error(msg);
219+
}
220+
}
221+
222+
void MiraiBot::SendNudge(QQ_t target, QQ_t subject_id)
223+
{
224+
SendNudge(target.ToInt64(), subject_id.ToInt64(), "Friend");
225+
}
226+
227+
void MiraiBot::SendNudge(QQ_t target, GID_t subject_id)
228+
{
229+
SendNudge(target.ToInt64(), subject_id.ToInt64(), "Group");
230+
}
231+
232+
void MiraiBot::SendNudge(QQ_t target, int64_t subject_id, NudgeEvent::SubjectKind kind)
233+
{
234+
SendNudge(target.ToInt64(), subject_id, NudgeEvent::SubjectKindStr(kind));
235+
}
198236

199237
FriendImage MiraiBot::UploadFriendImage(const string& fileName)
200238
{

0 commit comments

Comments
 (0)