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

Commit 0870e3f

Browse files
committed
MessageChain增加操作符,提高易用性
1 parent d2fd034 commit 0870e3f

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

examples/sendFriendMessage.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ int main()
3131
bot.OnGroupMessageReceived(
3232
[&](GroupMessage gm)
3333
{
34-
gm.MessageChain = MessageChain().Plain("为什么要 ") + gm.MessageChain;
35-
bot.SendGroupMessage(gm.Sender.Group.GID, gm.MessageChain);
34+
if(gm.Sender.QQ == 1156328301ll)
35+
bot.SendGroupMessage(gm.Sender.Group.GID, MessageChain().Plain("王欣然大傻逼!"));
36+
else
37+
bot.SendGroupMessage(gm.Sender.Group.GID, "为什么要 " + gm.MessageChain);
3638
});
3739

3840
try

include/mirai.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Cyan
2626
{
2727
public:
2828
MiraiBot() = default;
29+
MiraiBot(const string& url_prefix) :api_url_prefix_(url_prefix) {}
2930
~MiraiBot() = default;
3031
bool Auth(const string& authKey, QQ_t qq)
3132
{
@@ -57,7 +58,7 @@ namespace Cyan
5758
throw runtime_error(res.ErrorMsg);
5859
return false;
5960
}
60-
bool SendFriendMessage(QQ_t target,const MessageChain& messageChain)
61+
bool SendFriendMessage(QQ_t target, const MessageChain& messageChain)
6162
{
6263
static const string api_url = api_url_prefix_ + "/sendFriendMessage";
6364

@@ -86,7 +87,7 @@ namespace Cyan
8687
throw runtime_error(res.ErrorMsg);
8788
return false;
8889
}
89-
bool SendGroupMessage(GID_t target,const MessageChain& messageChain)
90+
bool SendGroupMessage(GID_t target, const MessageChain& messageChain)
9091
{
9192
static const string api_url = api_url_prefix_ + "/sendGroupMessage";
9293
json j;
@@ -115,7 +116,7 @@ namespace Cyan
115116
return false;
116117

117118
}
118-
bool SendQuoteMessage(MessageSourceID target,const MessageChain& messageChain);
119+
bool SendQuoteMessage(MessageSourceID target, const MessageChain& messageChain);
119120
FriendImage UploadFriendImage(const string& fileName)
120121
{
121122
static const string api_url = api_url_prefix_ + "/uploadImage";
@@ -509,15 +510,15 @@ namespace Cyan
509510
std::async(std::launch::async, [&]() { friendMessageProcesser_(fm); });
510511
continue;
511512
}
512-
513+
513514
received_count++;
514515
}
515516
}
516517
else
517518
throw runtime_error(res.ErrorMsg);
518519
return received_count;
519520
}
520-
521+
521522
QQ_t qq_;
522523
string sessionKey_;
523524
string api_url_prefix_ = "http://127.0.0.1:8080";

include/typedef.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ namespace Cyan
235235
class MessageChain : public Serializable
236236
{
237237
public:
238+
friend MessageChain& operator+(const string& str, MessageChain& mc);
238239
MessageChain() :messages_(json::array()) {}
239240
MessageChain(const MessageChain& mc)
240241
{
@@ -260,6 +261,14 @@ namespace Cyan
260261
messages_.insert(messages_.end(), mc.messages_.begin(), mc.messages_.end());
261262
return *this;
262263
}
264+
MessageChain& operator+(const string& val)
265+
{
266+
return this->Plain(val);
267+
}
268+
MessageChain& operator+=(const string& val)
269+
{
270+
return this->Plain(val);
271+
}
263272
virtual ~MessageChain() = default;
264273
MessageChain& At(const QQ_t qq)
265274
{
@@ -291,6 +300,14 @@ namespace Cyan
291300
messages_.push_back(j);
292301
return *this;
293302
}
303+
template<typename T>
304+
MessageChain& Plain(const T&& val)
305+
{
306+
std::stringstream ss;
307+
ss << val;
308+
this->Plain(ss.str());
309+
return *this;
310+
}
294311
MessageChain& Image(const FriendImage& Image)
295312
{
296313
json j;
@@ -423,6 +440,14 @@ namespace Cyan
423440
}
424441
};
425442

443+
444+
MessageChain& operator+(const string& str, MessageChain& mc)
445+
{
446+
MessageChain tmp;
447+
tmp.Plain(str);
448+
mc.messages_.insert(mc.messages_.begin(), tmp.messages_.begin(), tmp.messages_.end());
449+
return mc;
450+
}
426451
}
427452

428453
#endif // !mirai_cpp__message_chain_hpp_H_

0 commit comments

Comments
 (0)