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

Commit ee3cff1

Browse files
authored
Merge pull request #89 from cyanray/dev/cyanray
更新: SendNudge 函数以及相关代码
2 parents a9e3f26 + e116339 commit ee3cff1

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

examples/NudgeEvent.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ int main()
5858
// 注意: 因此必须过滤掉来自bot自己的戳一戳事件,不然会导致死循环
5959
if (e.FromId.ToInt64() == bot.GetBotQQ().ToInt64()) return;
6060

61-
cout << e.FromId.ToInt64() << " " << e.Action << " " << e.Target << " " << e.Suffix;
61+
cout << e.FromId.ToInt64() << " " << e.Action << " " << e.Target << " " << e.Suffix << endl;
6262
// 如果别人戳机器人,那么就让机器人戳回去
6363
if (e.Target.ToInt64() != bot.GetBotQQ().ToInt64()) return;
64-
bot.SendNudge(e.FromId, e.SubjectId, e.FromKind);
64+
bot.SendNudge(e.FromId, *e.GetSubjectId());
6565
// 如果不喜欢上面这一行代码,也可以用下面的代码代替
6666
//if (e.FromKind == NudgeEvent::SubjectKind::Group)
6767
//{
68-
// bot.SendNudge(e.FromId, (GID_t)e.SubjectId);
68+
// bot.SendNudge(e.FromId, (GID_t)e.RawSubjectId);
6969
//}
7070
//else
7171
//{
72-
// bot.SendNudge(e.FromId, (QQ_t)e.SubjectId);
72+
// bot.SendNudge(e.FromId, (QQ_t)e.RawSubjectId);
7373
//}
7474
});
7575

include/mirai/events/nudge_event.hpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,22 @@ namespace Cyan
2222

2323
QQ_t FromId;
2424
QQ_t Target;
25-
int64_t SubjectId;
25+
int64_t RawSubjectId;
2626
SubjectKind FromKind;
2727
string Action;
2828
string Suffix;
2929

30+
std::shared_ptr<UID_t> GetSubjectId()
31+
{
32+
if (FromKind == SubjectKind::Group)
33+
{
34+
return std::make_shared<GID_t>(RawSubjectId);
35+
}
36+
else
37+
{
38+
return std::make_shared<QQ_t>(RawSubjectId);
39+
}
40+
}
3041

3142
static MiraiEvent GetMiraiEvent()
3243
{
@@ -56,7 +67,7 @@ namespace Cyan
5667
{
5768
this->FromId = (QQ_t)(j["fromId"].get<int64_t>());
5869
this->Target = (QQ_t)(j["target"].get<int64_t>());
59-
this->SubjectId = j["subject"]["id"].get<int64_t>();
70+
this->RawSubjectId = j["subject"]["id"].get<int64_t>();
6071
this->FromKind = SubjectKindStr(j["subject"]["kind"].get<string>());
6172
this->Action = j["action"].get<string>();
6273
this->Suffix = j["suffix"].get<string>();
@@ -70,7 +81,7 @@ namespace Cyan
7081
{ "type", "NudgeEvent" },
7182
{ "fromId", (int64_t)FromId },
7283
{ "target", (int64_t)Target },
73-
{ "subject", { {"id", SubjectId }, { "kind", SubjectKindStr(FromKind) } } },
84+
{ "subject", { {"id", RawSubjectId }, { "kind", SubjectKindStr(FromKind) } } },
7485
{ "action", Action },
7586
{ "suffix", Suffix }
7687
};

include/mirai/mirai_bot.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
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/nudge_event.hpp"
2928
#include "mirai/events/lost_connection.hpp"
3029

3130
using std::string;
@@ -144,10 +143,9 @@ namespace Cyan
144143
/**
145144
* @brief 发送戳一戳
146145
* @param target 目标QQ,可以是好友或者Bot的QQ
147-
* @param subject_id 戳一戳接收主体,可以是QQ好友或者群号码
148-
* @param kind 需要指定接收主体的类型
146+
* @param subject_id 戳一戳接收主体
149147
*/
150-
void SendNudge(QQ_t target, int64_t subject_id, NudgeEvent::SubjectKind kind);
148+
void SendNudge(QQ_t target, const UID_t& subject_id);
151149
/**
152150
* \brief 上传可以发送给好友的图片
153151
* \param fileName 文件名

src/mirai_bot.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,16 @@ namespace Cyan
229229
SendNudge(target.ToInt64(), subject_id.ToInt64(), "Group");
230230
}
231231

232-
void MiraiBot::SendNudge(QQ_t target, int64_t subject_id, NudgeEvent::SubjectKind kind)
232+
void MiraiBot::SendNudge(QQ_t target, const UID_t& subject_id)
233233
{
234-
SendNudge(target.ToInt64(), subject_id, NudgeEvent::SubjectKindStr(kind));
234+
if (typeid(subject_id) == typeid(GID_t))
235+
{
236+
SendNudge(target, (const GID_t&)(subject_id));
237+
}
238+
else
239+
{
240+
SendNudge(target, (const QQ_t&)(subject_id));
241+
}
235242
}
236243

237244
FriendImage MiraiBot::UploadFriendImage(const string& fileName)

0 commit comments

Comments
 (0)