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

Commit 20e97b1

Browse files
committed
MessageChain 增加 operator+ 操作符
1 parent 2db843c commit 20e97b1

File tree

6 files changed

+93
-28
lines changed

6 files changed

+93
-28
lines changed

examples/sendFriendMessage.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,29 @@ int main()
2121
}
2222
cout << "成功登录 bot。" << endl;
2323

24-
GroupImage gImg = bot.UploadGroupImage("D:/QQ20200223215250.jpg");
2524

26-
MessageChain messageChain;
27-
messageChain
28-
.Face(4)
29-
.Plain("\n")
30-
.Plain("群消息测试")
31-
.Plain("🤣🤣🤣")
32-
.Image(gImg);
33-
34-
cout << messageChain.ToString() << endl;
25+
bot.OnFriendMessageReceived(
26+
[&](FriendMessage fm)
27+
{
28+
bot.SendFriendMessage(fm.Sender.QQ, fm.MessageChain);
29+
});
3530

3631
bot.OnGroupMessageReceived(
3732
[&](GroupMessage gm)
3833
{
34+
gm.MessageChain = MessageChain().Plain("为什么要 ") + gm.MessageChain;
3935
bot.SendGroupMessage(gm.Sender.Group.GID, gm.MessageChain);
4036
});
4137

4238
try
4339
{
4440
bot.EventLoop();
45-
//bot.SendFriendMessage(484599279ll, messageChain);
46-
//bot.UnMute(1013323391ll, 484599279ll);
47-
//bot.SendGroupMessage(1013323391ll, messageChain);
4841
}
4942
catch (const std::exception & ex)
5043
{
5144
cout << ex.what() << endl;
5245
}
5346

54-
5547
bot.Release();
5648
return 0;
5749
}

include/FriendMessage.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
#ifndef mirai_cpp__FriendMessage_h_H_
3+
#define mirai_cpp__FriendMessage_h_H_
4+
5+
#include "typedef.hpp"
6+
#include "message_chain.hpp"
7+
namespace Cyan
8+
{
9+
10+
class FriendMessage : public Serializable
11+
{
12+
public:
13+
MessageChain MessageChain;
14+
Friend_t Sender;
15+
16+
FriendMessage() = default;
17+
FriendMessage(const FriendMessage& gm)
18+
{
19+
MessageChain = gm.MessageChain;
20+
Sender = gm.Sender;
21+
}
22+
FriendMessage& operator=(const FriendMessage& t)
23+
{
24+
FriendMessage tmp(t);
25+
std::swap(this->MessageChain, tmp.MessageChain);
26+
std::swap(this->Sender, tmp.Sender);
27+
return *this;
28+
}
29+
virtual ~FriendMessage() = default;
30+
virtual bool Set(const json& j) override
31+
{
32+
this->MessageChain.Set(j["messageChain"]);
33+
Sender.Set(j["sender"]);
34+
return true;
35+
}
36+
virtual json ToJson() const override
37+
{
38+
json j = json::object();
39+
j["messageChain"] = this->MessageChain.ToJson();
40+
j["sender"] = this->Sender.ToJson();
41+
return j;
42+
}
43+
virtual string ToString() const override
44+
{
45+
return ToJson().dump();
46+
}
47+
};
48+
} // namespace Cyan
49+
50+
#endif // !mirai_cpp__GroupMessage_h_H_

include/GroupMessage.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#pragma once
2+
#ifndef mirai_cpp__GroupMessage_h_H_
3+
#define mirai_cpp__GroupMessage_h_H_
4+
25
#include "typedef.hpp"
36
#include "message_chain.hpp"
47
namespace Cyan
@@ -42,4 +45,6 @@ namespace Cyan
4245
return ToJson().dump();
4346
}
4447
};
45-
} // namespace Cyan
48+
} // namespace Cyan
49+
50+
#endif // !mirai_cpp__GroupMessage_h_H_

include/message_chain.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,26 @@ namespace Cyan
2121
{
2222
messages_ = mc.messages_;
2323
}
24+
MessageChain(MessageChain&& mc)
25+
{
26+
std::swap(messages_, mc.messages_);
27+
}
2428
MessageChain& operator=(const MessageChain& mc)
2529
{
2630
MessageChain tmp(mc);
2731
std::swap(messages_, tmp.messages_);
2832
return *this;
2933
}
34+
MessageChain& operator=(MessageChain&& mc)
35+
{
36+
std::swap(messages_, mc.messages_);
37+
return *this;
38+
}
39+
MessageChain& operator+(const MessageChain& mc)
40+
{
41+
messages_.insert(messages_.end(), mc.messages_.begin(), mc.messages_.end());
42+
return *this;
43+
}
3044
virtual ~MessageChain() = default;
3145
MessageChain& At(const QQ_t qq)
3246
{

include/mirai.hpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include <functional>
1111
#include <nlohmann/json.hpp>
1212
#include "typedef.hpp"
13+
#include "FriendMessage.h"
1314
#include "GroupMessage.h"
1415
#include "message_chain.hpp"
15-
#include <iostream>
1616
using std::string;
1717
using std::runtime_error;
1818
using std::vector;
@@ -22,16 +22,13 @@ using nlohmann::json;
2222

2323
namespace Cyan
2424
{
25-
25+
typedef std::function<void(FriendMessage)> FriendMessageProcesser;
2626
typedef std::function<void(GroupMessage)> GroupMessageProcesser;
2727

2828
class MiraiBot
2929
{
3030
public:
31-
MiraiBot()
32-
{
33-
groupMessageProcesser_ = [](GroupMessage) {};
34-
}
31+
MiraiBot() = default;
3532
~MiraiBot() = default;
3633
bool Auth(const string& authKey, QQ_t qq)
3734
{
@@ -400,15 +397,18 @@ namespace Cyan
400397

401398
}
402399

403-
void OnFriendMessageReceived();
400+
void OnFriendMessageReceived(FriendMessageProcesser friendMessageProcesser)
401+
{
402+
friendMessageProcesser_ = friendMessageProcesser;
403+
}
404404
void OnGroupMessageReceived(GroupMessageProcesser groupMessageProcesser)
405405
{
406406
groupMessageProcesser_ = groupMessageProcesser;
407407
}
408408

409409
void EventLoop()
410410
{
411-
unsigned count_per_loop = 10;
411+
unsigned count_per_loop = 20;
412412
unsigned time_interval = 100;
413413
while (true)
414414
{
@@ -476,9 +476,6 @@ namespace Cyan
476476
return false;
477477
}
478478

479-
480-
481-
482479
unsigned int FetchMessagesAndEvents(unsigned int count = 10)
483480
{
484481
stringstream api_url;
@@ -501,13 +498,20 @@ namespace Cyan
501498
for (const auto& ele : reJson)
502499
{
503500
MiraiEvent type = MiraiEventStr(ele["type"].get<string>());
504-
if (type == MiraiEvent::GroupMessage)
501+
if (groupMessageProcesser_ && type == MiraiEvent::GroupMessage)
505502
{
506503
GroupMessage gm;
507504
gm.Set(ele);
508505
std::async(std::launch::async, [&]() { groupMessageProcesser_(gm); });
509506
continue;
510507
}
508+
if (friendMessageProcesser_ && type == MiraiEvent::FriendMessage)
509+
{
510+
FriendMessage fm;
511+
fm.Set(ele);
512+
std::async(std::launch::async, [&]() { friendMessageProcesser_(fm); });
513+
continue;
514+
}
511515

512516
received_count++;
513517
}
@@ -521,6 +525,7 @@ namespace Cyan
521525
string sessionKey_;
522526
string api_url_prefix_ = "http://127.0.0.1:8080";
523527
GroupMessageProcesser groupMessageProcesser_;
528+
FriendMessageProcesser friendMessageProcesser_;
524529

525530
};
526531
} // namespace Cyan

include/typedef.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace Cyan
1818
// 消息源 ID
1919
typedef int64_t MessageSourceID;
2020

21-
2221
enum class GroupPermission
2322
{
2423
Member,

0 commit comments

Comments
 (0)