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

Commit fb9b383

Browse files
committed
新增: 支持发送戳一戳.
1 parent 90ed628 commit fb9b383

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
lines changed

examples/NudgeEvent.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,23 @@ int main()
5454
bot.On<NudgeEvent>(
5555
[&](NudgeEvent e)
5656
{
57-
stringstream ss;
58-
ss << e.FromId.ToInt64() << " " << e.Action << " " << e.Target << " " << e.Suffix;
59-
if (e.FromKind == NudgeEvent::SubjectKind::Group)
60-
{
61-
bot.SendMessage(GID_t(e.SubjectId), MessageChain().Plain(ss.str()));
62-
}
63-
else
64-
{
65-
bot.SendMessage(QQ_t(e.SubjectId), MessageChain().Plain(ss.str()));
66-
}
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+
//}
6774
});
6875

6976

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)