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

Commit 2db843c

Browse files
committed
重构
1 parent b41a03e commit 2db843c

File tree

7 files changed

+496
-520
lines changed

7 files changed

+496
-520
lines changed

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ project (mirai-cpp)
88
set(CMAKE_CXX_STANDARD 11)
99

1010
add_subdirectory(CURLWrapper)
11-
find_package(RapidJSON CONFIG REQUIRED)
11+
find_package(nlohmann_json CONFIG REQUIRED)
12+
1213

1314
add_library(${PROJECT_NAME} INTERFACE)
1415
target_include_directories(${PROJECT_NAME} INTERFACE include)
1516
target_link_libraries(${PROJECT_NAME} INTERFACE CURLWrapper)
16-
target_include_directories(${PROJECT_NAME} INTERFACE ${RAPIDJSON_INCLUDE_DIRS})
17+
target_link_libraries(${PROJECT_NAME} INTERFACE nlohmann_json nlohmann_json::nlohmann_json)
1718

1819
option(MIRAI_CPP_BUILD_EXAMPLES "Build examples" ON)
1920
if(MIRAI_CPP_BUILD_EXAMPLES)

examples/sendFriendMessage.cpp

Lines changed: 22 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,78 +7,42 @@ int main()
77
using namespace Cyan;
88
system("chcp 65001");
99
MiraiBot bot;
10-
try
11-
{
12-
bot.Auth("InitKeyVl0CEUzZ", 211795583ll);
13-
}
14-
catch (const std::exception & ex)
10+
while (true)
1511
{
16-
cout << ex.what() << endl;
12+
try
13+
{
14+
bot.Auth("InitKeyVl0CEUzZ", 211795583ll);
15+
break;
16+
}
17+
catch (const std::exception & ex)
18+
{
19+
cout << ex.what() << endl;
20+
}
1721
}
1822
cout << "成功登录 bot。" << endl;
1923

20-
Group_t group;
21-
group.GID = 1234567ll;
22-
group.Name = "HELLP";
23-
group.Permission = GroupPermission::Member;
24-
25-
cout << group.ToString() << endl;
26-
27-
string str = R"( {"id":1234567,"name":"HELLP","permission":"MEMBER"} )";
28-
JsonDoc j; j.Parse(str.data());
29-
30-
Group_t g2;
31-
g2.Set(j);
32-
33-
cout << g2.ToString() << endl;
34-
35-
try
36-
{
37-
//bot.Kick(1013323391ll, 484599279ll, "gundan");
38-
}
39-
catch (const std::exception & ex)
40-
{
41-
cout << ex.what() << endl;
42-
}
43-
44-
//vector<Group_t> fs = bot.GetGroupList();
45-
46-
//for (auto f : fs)
47-
//{
48-
// try
49-
// {
50-
// bot.MuteAll(f.GID);
51-
// }
52-
// catch (const std::exception & ex)
53-
// {
54-
// cout << ex.what() << endl;
55-
// }
56-
57-
// vector<GroupMember_t> gm = bot.GetGroupMembers(f.GID);
58-
// for (auto m : gm)
59-
// {
60-
// cout << m.QQ << " " << m.MemberName << " " << m.Group.Name << endl;
61-
// }
62-
// cout << endl;
63-
64-
//}
65-
66-
FriendImage fImg = bot.UploadFriendImage("D:/b.jpg");
24+
GroupImage gImg = bot.UploadGroupImage("D:/QQ20200223215250.jpg");
6725

6826
MessageChain messageChain;
6927
messageChain
70-
.Face(3)
71-
//.Plain("伞兵一号得得得得得得得得得得")
28+
.Face(4)
7229
.Plain("\n")
73-
.Plain("Hello World!")
30+
.Plain("群消息测试")
7431
.Plain("🤣🤣🤣")
75-
.Image(fImg);
32+
.Image(gImg);
7633

7734
cout << messageChain.ToString() << endl;
7835

36+
bot.OnGroupMessageReceived(
37+
[&](GroupMessage gm)
38+
{
39+
bot.SendGroupMessage(gm.Sender.Group.GID, gm.MessageChain);
40+
});
41+
7942
try
8043
{
81-
bot.SendFriendMessage(484599279ll, messageChain);
44+
bot.EventLoop();
45+
//bot.SendFriendMessage(484599279ll, messageChain);
8246
//bot.UnMute(1013323391ll, 484599279ll);
8347
//bot.SendGroupMessage(1013323391ll, messageChain);
8448
}

include/GroupMessage.h

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

include/message_chain.hpp

Lines changed: 43 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <iostream>
88
#include "serializable.hpp"
99
#include "typedef.hpp"
10-
#include <rapidjson/document.h>
10+
#include <nlohmann/json.hpp>
1111
using std::string;
1212
using std::stringstream;
1313
using std::runtime_error;
@@ -16,100 +16,81 @@ namespace Cyan
1616
class MessageChain : public Serializable
1717
{
1818
public:
19-
MessageChain()
19+
MessageChain() :messages_(json::array()) {}
20+
MessageChain(const MessageChain& mc)
2021
{
21-
messages_.SetArray();
22+
messages_ = mc.messages_;
23+
}
24+
MessageChain& operator=(const MessageChain& mc)
25+
{
26+
MessageChain tmp(mc);
27+
std::swap(messages_, tmp.messages_);
28+
return *this;
2229
}
2330
virtual ~MessageChain() = default;
2431
MessageChain& At(const QQ_t qq)
2532
{
26-
using namespace rapidjson;
27-
static const char* const jsonStr = R"( { "type":"At" , "target":0 , "display":"@123" } )";
28-
JsonDoc jdoc;
29-
if (jdoc.Parse(jsonStr).HasParseError()) throw runtime_error("未知错误");
30-
jdoc["target"].SetInt64(qq);
31-
Value v(jdoc, messages_.GetAllocator());
32-
messages_.PushBack(v, messages_.GetAllocator());
33+
json j;
34+
j["type"] = "At";
35+
j["target"] = qq;
36+
j["display"] = "@@";
37+
messages_.push_back(j);
3338
return *this;
3439
}
3540
MessageChain& AtAll()
3641
{
37-
using namespace rapidjson;
38-
static const char* const jsonStr = R"( { "type":"AtAll" } )";
39-
JsonDoc jdoc;
40-
if (jdoc.Parse(jsonStr).HasParseError()) throw runtime_error("未知错误");
41-
Value v(jdoc, messages_.GetAllocator());
42-
messages_.PushBack(v, messages_.GetAllocator());
42+
messages_.push_back({ {"type","AtAll"} });
4343
return *this;
4444
}
4545
MessageChain& Face(int faceID)
4646
{
47-
using namespace rapidjson;
48-
static const char* const jsonStr = R"( { "type":"Face" , "faceId":0 } )";
49-
JsonDoc jdoc;
50-
if (jdoc.Parse(jsonStr).HasParseError()) throw runtime_error("未知错误");
51-
jdoc["faceId"].SetInt(faceID);
52-
Value v(jdoc, messages_.GetAllocator());
53-
messages_.PushBack(v, messages_.GetAllocator());
47+
json j;
48+
j["type"] = "Face";
49+
j["faceId"] = faceID;
50+
messages_.push_back(j);
5451
return *this;
5552
}
5653
MessageChain& Plain(const string& plainText)
5754
{
58-
using namespace rapidjson;
59-
static const char* const jsonStr = R"( { "type":"Plain" , "text":"" } )";
60-
JsonDoc jdoc;
61-
if (jdoc.Parse(jsonStr).HasParseError()) throw runtime_error("未知错误");
62-
jdoc["text"].SetString(plainText.data(), plainText.size(),messages_.GetAllocator());
63-
Value v(jdoc, messages_.GetAllocator());
64-
messages_.PushBack(v, messages_.GetAllocator());
55+
json j;
56+
j["type"] = "Plain";
57+
j["text"] = plainText;
58+
messages_.push_back(j);
6559
return *this;
6660
}
67-
MessageChain& Image(const FriendImage& ImageID)
61+
MessageChain& Image(const FriendImage& Image)
6862
{
69-
using namespace rapidjson;
70-
static const char* const jsonStr = R"( { "type":"Image" , "imageId":"" } )";
71-
JsonDoc jdoc;
72-
if (jdoc.Parse(jsonStr).HasParseError()) throw runtime_error("未知错误");
73-
jdoc["imageId"].SetString(ImageID.ID.data(), ImageID.ID.size(),messages_.GetAllocator());
74-
Value v(jdoc, messages_.GetAllocator());
75-
messages_.PushBack(v, messages_.GetAllocator());
63+
json j;
64+
j["type"] = "Image";
65+
j["imageId"] = Image.ID;
66+
messages_.push_back(j);
7667
return *this;
7768
}
78-
MessageChain& Image(const GroupImage& ImageID)
69+
MessageChain& Image(const GroupImage& Image)
7970
{
80-
using namespace rapidjson;
81-
static const char* const jsonStr = R"( { "type":"Image" , "imageId":"" } )";
82-
JsonDoc jdoc;
83-
if (jdoc.Parse(jsonStr).HasParseError()) throw runtime_error("未知错误");
84-
jdoc["imageId"].SetString(ImageID.ID.data(), ImageID.ID.size(),messages_.GetAllocator());
85-
Value v(jdoc, messages_.GetAllocator());
86-
messages_.PushBack(v, messages_.GetAllocator());
71+
json j;
72+
j["type"] = "Image";
73+
j["imageId"] = Image.ID;
74+
messages_.push_back(j);
8775
return *this;
8876
}
89-
90-
// 通过 JsonDoc 设置对象的值,
91-
// 不检查传入 JsonDoc 的值是否符合要求
92-
virtual bool Set(JsonVal& json) override
77+
78+
virtual bool Set(const json& j) override
9379
{
94-
if (!json.IsArray()) return false;
95-
JsonDoc newJsonDoc;
96-
messages_.Swap(newJsonDoc);
97-
messages_.SetArray();
98-
for (rapidjson::SizeType i = 0; i < json.Size(); i++)
99-
{
100-
messages_.PushBack(json[i], messages_.GetAllocator());
101-
}
80+
messages_ = j;
81+
return true;
10282
}
103-
virtual JsonDoc& ToJson() override
83+
virtual json ToJson() const override
10484
{
10585
return messages_;
10686
}
107-
virtual string ToString() override
87+
virtual string ToString() const override
10888
{
109-
return JsonDoc2String(messages_);
89+
return messages_.dump();
11090
}
91+
11192
private:
112-
JsonDoc messages_;
93+
json messages_;
11394
};
11495

11596

0 commit comments

Comments
 (0)