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

Commit 7a0f8fc

Browse files
committed
支持获取/设置群设置;
1 parent 456d2fa commit 7a0f8fc

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ api_exe(MessageType)
2828
api_exe(FetchEventsViaHTTP)
2929
api_exe(CompareMessage)
3030
api_exe(GroupMemberInfo)
31-
api_exe(GroupNameChange)
31+
api_exe(GroupNameChange)
32+
api_exe(GroupConfig)

examples/GroupConfig.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
// 使用静态库必须要在引入 mirai.h 前定义这个宏
3+
#define MIRAICPP_STATICLIB
4+
#include <mirai.h>
5+
6+
int main()
7+
{
8+
using namespace std;
9+
using namespace Cyan;
10+
system("chcp 65001");
11+
MiraiBot bot("127.0.0.1", 539);
12+
while (true)
13+
{
14+
try
15+
{
16+
bot.Auth("INITKEY7A3O1a9v", 1589588851_qq);
17+
break;
18+
}
19+
catch (const std::exception& ex)
20+
{
21+
cout << ex.what() << endl;
22+
}
23+
MiraiBot::SleepSeconds(1);
24+
}
25+
cout << "成功登录 bot。" << endl;
26+
27+
28+
GroupConfig group_config = bot.GetGroupConfig(1029259687_gid);
29+
30+
group_config.Name = "New Name 2";
31+
32+
bot.SetGroupConfig(1029259687_gid, group_config);
33+
34+
cout << group_config.Name << endl;
35+
36+
37+
bot.EventLoop();
38+
39+
40+
return 0;
41+
}

include/defs/defs.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
#include "group_member.hpp"
1111
#include "message_chain.hpp"
1212
#include "group_member_info.hpp"
13+
#include "group_config.hpp"
1314

1415
#endif // !mirai_cpp_defs_defs_hpp_H_

include/mirai_bot.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ namespace Cyan
231231
* \return 始终为 true 出错会抛出异常
232232
*/
233233
bool Quit(GID_t group);
234+
/**
235+
* \brief 获取群设置
236+
* \param group 群(GID_t)
237+
* \return 群设置
238+
*/
239+
GroupConfig GetGroupConfig(GID_t group);
240+
/**
241+
* \brief 设置群设置
242+
* \param group 群(GID_t)
243+
* \param groupConfig 群设置
244+
* \return 始终为 true 出错会抛出异常
245+
*/
246+
bool SetGroupConfig(GID_t group, GroupConfig groupConfig);
234247
/**
235248
* \brief 根据消息ID(MessageId)获取对应的好友消息
236249
* \param mid 消息ID(MessageId)

src/mirai_bot.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,48 @@ namespace Cyan
525525
throw runtime_error(msg);
526526
}
527527

528+
GroupConfig MiraiBot::GetGroupConfig(GID_t group)
529+
{
530+
stringstream api_url;
531+
api_url
532+
<< "/groupConfig?sessionKey="
533+
<< sessionKey_
534+
<< "&target="
535+
<< int64_t(group);
536+
auto res = http_client_.Get(api_url.str().data());
537+
if (!res)
538+
throw runtime_error("网络错误");
539+
if (res->status != 200)
540+
throw std::runtime_error("[mirai-http-api error]: " + res->body);
541+
json re_json = json::parse(res->body);
542+
GroupConfig group_config;
543+
group_config.Set(re_json);
544+
return group_config;
545+
546+
}
547+
548+
bool MiraiBot::SetGroupConfig(GID_t group, GroupConfig groupConfig)
549+
{
550+
json data =
551+
{
552+
{ "sessionKey", sessionKey_ },
553+
{ "target", int64_t(group) }
554+
};
555+
data["config"] = groupConfig.ToJson();
556+
557+
auto res = http_client_.Post("/groupConfig", data.dump(), "application/json;charset=UTF-8");
558+
if (!res)
559+
throw std::runtime_error("网络错误");
560+
if (res->status != 200)
561+
throw std::runtime_error("[mirai-api-http error]: " + res->body);
562+
json re_json = json::parse(res->body);
563+
int code = re_json["code"].get<int>();
564+
if (code == 0)
565+
return true;
566+
string msg = re_json["msg"].get<string>();
567+
throw runtime_error(msg);
568+
}
569+
528570
FriendMessage MiraiBot::GetFriendMessageFromId(MessageId mid)
529571
{
530572
stringstream api_url;

0 commit comments

Comments
 (0)