-
Notifications
You must be signed in to change notification settings - Fork 2
/
Bot.cpp
241 lines (199 loc) · 10.2 KB
/
Bot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "Bot.h"
#include <memory>
#include <utility>
#include <chrono>
using namespace std;
atomic_int32_t seq = 0;
Bot::Bot(const Bot &a) {
this->conn = a.conn;
this->selfId = a.selfId;
this->promiseMap = a.promiseMap;
}
Bot::Bot(int64 selfId, WebSocketConnectionPtr connection) : conn(std::move(connection)) {
this->selfId = selfId;
this->promiseMap = make_shared<PromiseMap>(PromiseMap());
}
Bot::~Bot() {
printf("~\n");
}
shared_ptr<onebot::Frame> Bot::sendAndWait(shared_ptr<onebot::Frame> reqFrame) {
string echo = to_string(seq++);
*reqFrame->mutable_echo() = echo;
auto promisePtr = make_shared<promise<shared_ptr<onebot::Frame>>>();
(*this->promiseMap)[echo] = promisePtr;
auto reqData = reqFrame->SerializeAsString();
conn_mtx.lock();
this->conn->send(reqData, WebSocketMessageType::Binary);
conn_mtx.unlock();
auto respFuture = promisePtr->get_future();
respFuture.wait_for(60000ms);
this->promiseMap->erase(echo);
return respFuture.get();
}
shared_ptr<SendPrivateMsgResp> Bot::sendPrivateMsg(int64 user_id, const string &message, bool auto_escape) {
return sendPrivateMsg(user_id, Text(message), auto_escape);
}
shared_ptr<SendPrivateMsgResp> Bot::sendPrivateMsg(int64 user_id, const Msg &msg, bool auto_escape) {
auto req = new SendPrivateMsgReq(); // 不用delete,frame自动delete
req->set_user_id(user_id);
req->set_auto_escape(auto_escape);
*req->mutable_message() = {msg.messageList.begin(), msg.messageList.end()};
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TSendPrivateMsgReq);
reqFrame->set_allocated_send_private_msg_req(req);
auto respFrame = sendAndWait(reqFrame);
auto resp = make_shared<SendPrivateMsgResp>(respFrame->send_private_msg_resp());
return resp;
}
shared_ptr<SendGroupMsgResp> Bot::sendGroupMsg(int64 group_id, const string &message, bool auto_escape) {
return sendGroupMsg(group_id, Text(message), auto_escape);
}
shared_ptr<SendGroupMsgResp> Bot::sendGroupMsg(int64 group_id, const Msg &msg, bool auto_escape) {
auto req = new SendGroupMsgReq(); // 不用delete,frame自动delete
req->set_group_id(group_id);
req->set_auto_escape(auto_escape);
*req->mutable_message() = {msg.messageList.begin(), msg.messageList.end()};
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TSendGroupMsgReq);
reqFrame->set_allocated_send_group_msg_req(req);
return make_shared<SendGroupMsgResp>(sendAndWait(reqFrame)->send_group_msg_resp());
}
shared_ptr<DeleteMsgResp> Bot::deleteMsg(int message_id) {
auto req = new DeleteMsgReq(); // 不用delete,frame自动delete
req->set_message_id(message_id);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TDeleteMsgReq);
reqFrame->set_allocated_delete_msg_req(req);
return make_shared<DeleteMsgResp>(sendAndWait(reqFrame)->delete_msg_resp());
}
shared_ptr<GetMsgResp> Bot::getMsg(int message_id) {
auto req = new GetMsgReq(); // 不用delete,frame自动delete
req->set_message_id(message_id);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TGetMsgReq);
reqFrame->set_allocated_get_msg_req(req);
return make_shared<GetMsgResp>(sendAndWait(reqFrame)->get_msg_resp());
}
shared_ptr<SetGroupKickResp> Bot::setGroupKick(int64 group_id, int64 user_id, bool reject_add_request) {
auto req = new SetGroupKickReq(); // 不用delete,frame自动delete
req->set_group_id(group_id);
req->set_user_id(user_id);
req->set_reject_add_request(reject_add_request);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TSetGroupKickReq);
reqFrame->set_allocated_set_group_kick_req(req);
return make_shared<SetGroupKickResp>(sendAndWait(reqFrame)->set_group_kick_resp());
}
shared_ptr<SetGroupBanResp> Bot::setGroupBan(int64 group_id, int64 user_id, int duration) {
auto req = new SetGroupBanReq(); // 不用delete,frame自动delete
req->set_group_id(group_id);
req->set_user_id(user_id);
req->set_duration(duration);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TSetGroupBanReq);
reqFrame->set_allocated_set_group_ban_req(req);
return make_shared<SetGroupBanResp>(sendAndWait(reqFrame)->set_group_ban_resp());
}
shared_ptr<SetGroupWholeBanResp> Bot::setGroupWholeBan(int64 group_id, bool enable) {
auto req = new SetGroupWholeBanReq(); // 不用delete,frame自动delete
req->set_group_id(group_id);
req->set_enable(enable);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TSetGroupWholeBanReq);
reqFrame->set_allocated_set_group_whole_ban_req(req);
return make_shared<SetGroupWholeBanResp>(sendAndWait(reqFrame)->set_group_whole_ban_resp());
}
shared_ptr<SetGroupCardResp> Bot::setGroupCard(int64 group_id, int64 user_id, string card) {
auto req = new SetGroupCardReq(); // 不用delete,frame自动delete
req->set_group_id(group_id);
req->set_user_id(user_id);
req->set_card(card);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TSetGroupCardReq);
reqFrame->set_allocated_set_group_card_req(req);
return make_shared<SetGroupCardResp>(sendAndWait(reqFrame)->set_group_card_resp());
}
shared_ptr<SetGroupLeaveResp> Bot::setGroupLeave(int64 group_id, bool is_dismiss) {
auto req = new SetGroupLeaveReq(); // 不用delete,frame自动delete
req->set_group_id(group_id);
req->set_is_dismiss(is_dismiss);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TSetGroupLeaveReq);
reqFrame->set_allocated_set_group_leave_req(req);
return make_shared<SetGroupLeaveResp>(sendAndWait(reqFrame)->set_group_leave_resp());
}
shared_ptr<SetFriendAddRequestResp> Bot::setFriendAddRequest(string flag, bool approve, string remark) {
auto req = new SetFriendAddRequestReq(); // 不用delete,frame自动delete
req->set_flag(flag);
req->set_approve(approve);
req->set_remark(remark);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TSetFriendAddRequestReq);
reqFrame->set_allocated_set_friend_add_request_req(req);
return make_shared<SetFriendAddRequestResp>(sendAndWait(reqFrame)->set_friend_add_request_resp());
}
shared_ptr<SetGroupAddRequestResp> Bot::setGroupAddRequest(string flag, string sub_type, string type, bool approve, string reason) {
auto req = new SetGroupAddRequestReq(); // 不用delete,frame自动delete
req->set_flag(flag);
req->set_sub_type(sub_type);
req->set_type(type);
req->set_approve(approve);
req->set_reason(reason);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TSetGroupAddRequestReq);
reqFrame->set_allocated_set_group_add_request_req(req);
return make_shared<SetGroupAddRequestResp>(sendAndWait(reqFrame)->set_group_add_request_resp());
}
shared_ptr<GetLoginInfoResp> Bot::getLoginInfo() {
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TGetLoginInfoReq);
reqFrame->set_allocated_get_login_info_req(new GetLoginInfoReq());
return make_shared<GetLoginInfoResp>(sendAndWait(reqFrame)->get_login_info_resp());
}
shared_ptr<GetStrangerInfoResp> Bot::getStrangerInfo(int64 user_id) {
auto req = new GetStrangerInfoReq(); // 不用delete,frame自动delete
req->set_user_id(user_id);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TGetStrangerInfoReq);
reqFrame->set_allocated_get_stranger_info_req(req);
return make_shared<GetStrangerInfoResp>(sendAndWait(reqFrame)->get_stranger_info_resp());
}
shared_ptr<GetFriendListResp> Bot::getFriendList() {
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TGetFriendListReq);
reqFrame->set_allocated_get_friend_list_req(new GetFriendListReq());
return make_shared<GetFriendListResp>(sendAndWait(reqFrame)->get_friend_list_resp());
}
shared_ptr<GetGroupListResp> Bot::getGroupList() {
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TGetGroupListReq);
reqFrame->set_allocated_get_group_list_req(new GetGroupListReq());
return make_shared<GetGroupListResp>(sendAndWait(reqFrame)->get_group_list_resp());
}
shared_ptr<GetGroupInfoResp> Bot::getGroupInfo(int64 group_id, bool no_cache) {
auto req = new GetGroupInfoReq(); // 不用delete,frame自动delete
req->set_group_id(group_id);
req->set_no_cache(no_cache);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TGetGroupInfoReq);
reqFrame->set_allocated_get_group_info_req(req);
return make_shared<GetGroupInfoResp>(sendAndWait(reqFrame)->get_group_info_resp());
}
shared_ptr<GetGroupMemberInfoResp> Bot::getGroupMemberInfo(int64 group_id, int64 user_id, bool no_cache) {
auto req = new GetGroupMemberInfoReq(); // 不用delete,frame自动delete
req->set_group_id(group_id);
req->set_user_id(user_id);
req->set_no_cache(no_cache);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TGetGroupMemberInfoReq);
reqFrame->set_allocated_get_group_member_info_req(req);
return make_shared<GetGroupMemberInfoResp>(sendAndWait(reqFrame)->get_group_member_info_resp());
}
shared_ptr<GetGroupMemberListResp> Bot::getGroupMemberList(int64 group_id) {
auto req = new GetGroupMemberListReq(); // 不用delete,frame自动delete
req->set_group_id(group_id);
auto reqFrame = make_shared<onebot::Frame>(onebot::Frame());
reqFrame->set_frame_type(Frame_FrameType_TGetGroupMemberListReq);
reqFrame->set_allocated_get_group_member_list_req(req);
return make_shared<GetGroupMemberListResp>(sendAndWait(reqFrame)->get_group_member_list_resp());
}