DiscordCoreAPI
A Discord bot library written in C++, with custom asynchronous coroutines.
Loading...
Searching...
No Matches
GuildMemberEntities.cpp
Go to the documentation of this file.
1/*
2 MIT License
3
4 DiscordCoreAPI, A bot library for Discord, written in C++, and featuring explicit multithreading through the usage of custom, asynchronous C++ CoRoutines.
5
6 Copyright 2022, 2023 Chris M. (RealTimeChris)
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25*/
26/// GuildMemberEntities.cpp - Source file for the GuildMemberData related classes and structs.
27/// May 13, 2021
28/// https://discordcoreapi.com
29/// \file GuildMemberEntities.cpp
30
35
36namespace jsonifier {
37
38 template<> struct core<DiscordCoreAPI::AddGuildMemberData> {
39 using ValueType = DiscordCoreAPI::AddGuildMemberData;
40 static constexpr auto parseValue = objectVal("roles", &ValueType::roles, "access_token", &ValueType::accessToken, "guild_id", &ValueType::guildId, "user_id",
41 &ValueType::userId, "nick", &ValueType::nick, "mute", &ValueType::mute, "deaf", &ValueType::deaf);
42 };
43
44 template<> struct core<DiscordCoreAPI::ModifyCurrentGuildMemberData> {
46 static constexpr auto parseValue = objectVal("guild_id", &ValueType::guildId, "nick", &ValueType::nick, "reason", &ValueType::reason);
47 };
48
49 template<> struct core<DiscordCoreAPI::ModifyGuildMemberData> {
51 static constexpr auto parseValue =
52 objectVal("channel_id", &ValueType::currentChannelId, "deaf", &ValueType::deaf, "guild_id", &ValueType::guildId, "mute", &ValueType::mute, "nick", &ValueType::nick,
53 "roles", &ValueType::roleIds, "user_id", &ValueType::guildMemberId, "voice_channel_id", &ValueType::newVoiceChannelId, "reason", &ValueType::reason);
54 };
55}
56
57namespace DiscordCoreAPI {
58
59 GuildMemberCacheData& GuildMemberCacheData::operator=(const GuildMemberData& other) {
60 setFlagValue(GuildMemberFlags::Pending, other.pending);
61 setFlagValue(GuildMemberFlags::Deaf, other.deaf);
62 setFlagValue(GuildMemberFlags::Mute, other.mute);
63 if (static_cast<int64_t>(other.flags) != 0) {
64 flags = other.flags;
65 }
66 if (other.permissions != "") {
67 permissions = other.permissions;
68 }
69 if (other.joinedAt != "") {
70 joinedAt = other.joinedAt;
71 }
72 if (other.user.id != 0) {
73 user.id = other.user.id;
74 }
75 if (other.guildId != 0) {
76 guildId = other.guildId;
77 }
78 if (other.avatar != "") {
79 avatar = other.avatar;
80 }
81 if (other.roles.size() > 0) {
82 roles = other.roles;
83 }
84 if (other.nick != "") {
85 nick = other.nick;
86 }
87 return *this;
88 };
89
90 GuildMemberCacheData::GuildMemberCacheData(const GuildMemberData& other) {
91 *this = other;
92 }
93
94 GuildMemberCacheData& GuildMemberCacheData::operator=(GuildMemberData&& other) {
95 setFlagValue(GuildMemberFlags::Pending, other.pending);
96 setFlagValue(GuildMemberFlags::Deaf, other.deaf);
97 setFlagValue(GuildMemberFlags::Mute, other.mute);
98 if (static_cast<int64_t>(other.flags) != 0) {
99 flags = other.flags;
100 }
101 if (other.permissions != "") {
102 permissions = std::move(other.permissions);
103 }
104 if (other.joinedAt != "") {
105 joinedAt = std::move(other.joinedAt);
106 }
107 if (other.avatar != "") {
108 avatar = std::move(other.avatar);
109 }
110 if (other.roles.size() > 0) {
111 roles = std::move(other.roles);
112 }
113 if (other.nick != "") {
114 nick = std::move(other.nick);
115 }
116 if (other.user.id != 0) {
117 user.id = other.user.id;
118 }
119 if (other.guildId != 0) {
120 guildId = other.guildId;
121 }
122 return *this;
123 };
124
125 GuildMemberCacheData::operator GuildMemberData() {
126 GuildMemberData returnData{};
127 returnData.pending = getFlagValue(GuildMemberFlags::Pending);
128 returnData.permissions = permissions.operator std::string();
129 returnData.deaf = getFlagValue(GuildMemberFlags::Deaf);
130 returnData.mute = getFlagValue(GuildMemberFlags::Mute);
131 returnData.joinedAt = joinedAt.operator std::string();
132 returnData.nick = nick.operator std::string();
133 returnData.guildId = guildId;
134 returnData.user.id = user.id;
135 returnData.avatar = avatar;
136 returnData.roles = roles;
137 returnData.flags = flags;
138 return returnData;
139 }
140
141 GuildMemberCacheData::GuildMemberCacheData(GuildMemberData&& other) {
142 *this = std::move(other);
143 }
144
145 void GuildMembers::initialize(DiscordCoreInternal::HttpsClient* client, ConfigManager* configManagerNew) {
146 GuildMembers::doWeCacheGuildMembersBool = configManagerNew->doWeCacheGuildMembers();
147 GuildMembers::httpsClient = client;
148 }
149
151 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Guild_Member };
152 co_await NewThreadAwaitable<GuildMemberData>();
153 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
154 workload.relativePath = "/guilds/" + dataPackage.guildId + "/members/" + dataPackage.guildMemberId;
155 workload.callStack = "GuildMembers::getGuildMemberAsync()";
156 GuildMemberData data{};
157 data.user.id = dataPackage.guildMemberId;
158 data.guildId = dataPackage.guildId;
159 TwoIdKey key{ data };
160 if (cache.contains(key)) {
161 data = cache[key];
162 }
163 GuildMembers::httpsClient->submitWorkloadAndGetResult(std::move(workload), data);
164 if (doWeCacheGuildMembersBool) {
165 insertGuildMember(static_cast<GuildMemberCacheData>(data));
166 }
167 co_return data;
168 }
169
172 data.user.id = dataPackage.guildMemberId;
173 data.guildId = dataPackage.guildId;
174 TwoIdKey key{ data };
175 if (cache.contains(key)) {
176 return cache[key];
177 } else {
178 }
179 return GuildMembers::getGuildMemberAsync(dataPackage).get();
180 }
181
183 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Guild_Members };
184 co_await NewThreadAwaitable<jsonifier::vector<GuildMemberData>>();
185 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
186 workload.relativePath = "/guilds/" + dataPackage.guildId + "/members";
187 if (dataPackage.after != 0) {
188 workload.relativePath += "?after=" + dataPackage.after;
189 if (dataPackage.limit != 0) {
190 workload.relativePath += "&limit=" + std::to_string(dataPackage.limit);
191 }
192 } else if (dataPackage.limit != 0) {
193 workload.relativePath += "?limit=" + std::to_string(dataPackage.limit);
194 }
195 workload.callStack = "GuildMembers::listGuildMembersAsync()";
196 jsonifier::vector<GuildMemberData> returnData{};
197 GuildMembers::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
198 co_return returnData;
199 }
200
202 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Search_Guild_Members };
203 co_await NewThreadAwaitable<jsonifier::vector<GuildMemberData>>();
204 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
205 workload.relativePath = "/guilds/" + dataPackage.guildId + "/members/search";
206 if (dataPackage.query != "") {
207 workload.relativePath += "?query=" + dataPackage.query;
208 if (dataPackage.limit != 0) {
209 workload.relativePath += "&limit=" + std::to_string(dataPackage.limit);
210 }
211 } else if (dataPackage.limit != 0) {
212 workload.relativePath += "?limit=" + std::to_string(dataPackage.limit);
213 }
214 workload.callStack = "GuildMembers::searchGuildMembersAsync()";
215 jsonifier::vector<GuildMemberData> returnData{};
216 GuildMembers::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
217 co_return returnData;
218 }
219
221 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Put_Guild_Member };
222 co_await NewThreadAwaitable<GuildMemberData>();
223 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Put;
224 workload.relativePath = "/guilds/" + dataPackage.guildId + "/members/" + dataPackage.userId;
225 parser.serializeJson(dataPackage, workload.content);
226 workload.callStack = "GuildMembers::addGuildMemberAsync()";
227 GuildMemberData returnData{};
228 GuildMembers::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
229 co_return returnData;
230 }
231
233 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Patch_Current_Guild_Member };
234 co_await NewThreadAwaitable<GuildMemberData>();
235 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Patch;
236 workload.relativePath = "/guilds/" + dataPackage.guildId + "/members/@me";
237 parser.serializeJson(dataPackage, workload.content);
238 workload.callStack = "GuildMembers::modifyCurrentGuildMemberAsync()";
239 if (dataPackage.reason != "") {
240 workload.headersToInsert["X-Audit-Log-Reason"] = dataPackage.reason;
241 }
242 GuildMemberData returnData{};
243 GuildMembers::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
244 co_return returnData;
245 }
246
248 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Patch_Guild_Member };
249 co_await NewThreadAwaitable<GuildMemberData>();
250 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Patch;
251 workload.relativePath = "/guilds/" + dataPackage.guildId + "/members/" + dataPackage.guildMemberId;
252 parser.serializeJson(dataPackage, workload.content);
253 workload.callStack = "GuildMembers::modifyGuildMemberAsync()";
254 if (dataPackage.reason != "") {
255 workload.headersToInsert["X-Audit-Log-Reason"] = dataPackage.reason;
256 }
257 GuildMemberData data{};
258 data.user.id = dataPackage.guildMemberId;
259 data.guildId = dataPackage.guildId;
260 TwoIdKey key{ data };
261 if (cache.contains(key)) {
262 data = cache[key];
263 }
264 GuildMembers::httpsClient->submitWorkloadAndGetResult(std::move(workload), data);
265 if (doWeCacheGuildMembersBool) {
266 insertGuildMember(static_cast<GuildMemberCacheData>(data));
267 }
268 co_return data;
269 }
270
272 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_Guild_Member };
273 co_await NewThreadAwaitable<void>();
274 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
275 workload.relativePath = "/guilds/" + dataPackage.guildId + "/members/" + dataPackage.guildMemberId;
276 workload.callStack = "GuildMembers::removeGuildMemberAsync()";
277 if (dataPackage.reason != "") {
278 workload.headersToInsert["X-Audit-Log-Reason"] = dataPackage.reason;
279 }
280 GuildMembers::httpsClient->submitWorkloadAndGetResult(std::move(workload));
281 co_return;
282 }
283
285 co_await NewThreadAwaitable<GuildMemberData>();
286 GuildMemberData guildMember = GuildMembers::getCachedGuildMember({ .guildMemberId = dataPackage.guildMemberId, .guildId = dataPackage.guildId });
287 ModifyGuildMemberData dataPackage01{};
288 dataPackage01.deaf = guildMember.getFlagValue(GuildMemberFlags::Deaf);
289 dataPackage01.guildId = guildMember.guildId;
290 dataPackage01.guildMemberId = guildMember.user.id;
291 dataPackage01.mute = guildMember.getFlagValue(GuildMemberFlags::Mute);
292 for (auto& value: guildMember.roles) {
293 dataPackage01.roleIds.emplace_back(value);
294 }
295 dataPackage01.nick = guildMember.nick;
296 dataPackage01.reason = dataPackage.reason;
297 TimeStamp timeStamp{};
298 switch (dataPackage.numOfMinutesToTimeoutFor) {
300 auto string = timeStamp.convertToFutureISO8601TimeStamp(0, 0, 1, 0, 0, TimeFormat::LongDateTime);
301 dataPackage01.communicationDisabledUntil = string;
302 break;
303 }
305 auto string = timeStamp.convertToFutureISO8601TimeStamp(5, 0, 0, 0, 0, TimeFormat::LongDateTime);
306 dataPackage01.communicationDisabledUntil = string;
307 break;
308 }
310 auto string = timeStamp.convertToFutureISO8601TimeStamp(0, 1, 0, 0, 0, TimeFormat::LongDateTime);
311 dataPackage01.communicationDisabledUntil = string;
312 break;
313 }
315 auto string = timeStamp.convertToFutureISO8601TimeStamp(10, 0, 0, 0, 0, TimeFormat::LongDateTime);
316 dataPackage01.communicationDisabledUntil = string;
317 break;
318 }
320 auto string = timeStamp.convertToFutureISO8601TimeStamp(0, 0, 7, 0, 0, TimeFormat::LongDateTime);
321 dataPackage01.communicationDisabledUntil = string;
322 break;
323 }
325 auto string = timeStamp.convertToFutureISO8601TimeStamp(1, 0, 0, 0, 0, TimeFormat::LongDateTime);
326 dataPackage01.communicationDisabledUntil = string;
327 break;
328 }
330 auto string = timeStamp.convertToFutureISO8601TimeStamp(0, 0, 0, 0, 0, TimeFormat::LongDateTime);
331 dataPackage01.communicationDisabledUntil = string;
332 break;
333 }
334 }
335 GuildMembers::modifyGuildMemberAsync(dataPackage01).get();
336 co_return guildMember;
337 }
338
339 VoiceStateDataLight GuildMembers::getVoiceStateData(const TwoIdKey& key) {
340 if (vsCache.contains(key)) {
341 return vsCache[key];
342 } else {
343 return {};
344 }
345 }
346
347 void GuildMembers::removeGuildMember(const TwoIdKey& key) {
348 cache.erase(key);
349 };
350
351 void GuildMembers::removeVoiceState(const TwoIdKey& key) {
352 vsCache.erase(key);
353 }
354
355 bool GuildMembers::doWeCacheGuildMembers() {
356 return GuildMembers::doWeCacheGuildMembersBool;
357 }
358
359 ObjectCache<VoiceStateDataLight> GuildMembers::vsCache{};
360 ObjectCache<GuildMemberCacheData> GuildMembers::cache{};
361 DiscordCoreInternal::HttpsClient* GuildMembers::httpsClient{};
362 bool GuildMembers::doWeCacheGuildMembersBool{};
363};
@ LongDateTime
"Tuesday, 20 April 2021 16:20" - Long Date/Time
@ None
None - erase timeout.
@ Ten_Minutes
10 Minute timeout.
@ Five_Minutes
5 Minute timeout.
A CoRoutine - representing a potentially asynchronous operation/function.
Definition: CoRoutine.hpp:88
Snowflake id
The user's id.
Data structure representing a single GuildMemberData.
std::string nick
This user's guild nickname.
Snowflake guildId
The guild that this member belongs to.
UserData user
The user this guild member represents.
jsonifier::vector< Snowflake > roles
Array of role object ids.
Data structure representing a single GuildMemberData.
Snowflake guildId
The current Guild's id.
GuildMemberFlags flags
GuildMemberData flags.
jsonifier::vector< Snowflake > roles
The Guild roGuildMemberDatales that they have.
IconHash avatar
This GuildMemberData's Guild Avatar.
jsonifier::string nick
Their nick/display name.
UserIdBase user
The user id for this GuildMemberData.
Permissions permissions
Their base-level Permissions in the Guild.
TimeStamp joinedAt
When they joined the Guild.
For getting a GuildMember, from the library's cache or Discord server.
Snowflake guildId
The id of the Guild from which you would like to acquire a member.
Snowflake guildMemberId
The user id of the desired GuildMemberData.
For listing the GuildMembers of a chosen Guild.
int32_t limit
Max number of members to return (1 - 1000).
Snowflake guildId
Guild from which to list the GuildMembers.
Snowflake after
The highest user id in the previous page.
For searching for one or more GuildMembers within a chosen Guild.
Snowflake guildId
Guild within which to search for the GuildMembers.
int32_t limit
Max number of members to return (1 - 1000).
std::string query
Query std::string to match username(s) and nickname(s) against.
For adding a new GuildMemberData to a chosen Guild.
Snowflake userId
The UserData id of the user you wish to add.
Snowflake guildId
The Guild to add the new GuildMemberData to.
For modifying the current GuildMemberData's values.
std::string reason
A reason for modifying the current user's values.
Snowflake guildId
The Guild within which to modify the current user's values.
For modifying a GuildMember's values.
std::string reason
Reason for modifying this GuildMemberData.
bool deaf
Whether or not to deafen them, in voice.
Snowflake guildId
The id of the Guild for which you would like to modify a member.
Snowflake guildMemberId
The user id of the desired Guild memeber.
For removing a GuildMember from a chosen Guild.
std::string reason
Reason for kicking the GuildMemberData.
Snowflake guildMemberId
Snowflake of the chosen GuildMemberData to kick.
Snowflake guildId
Guild from which to kick the chosen GuildMemberData.
Snowflake guildId
The id of the Guild from which you would like to acquire a member.
Snowflake guildMemberId
The id of the GuildMemberData to be timed-out.
std::string reason
Reason for timing them out.
TimeoutDurations numOfMinutesToTimeoutFor
The number of minutes to time-out the GuildMemberData for.
static CoRoutine< jsonifier::vector< GuildMemberData > > searchGuildMembersAsync(SearchGuildMembersData dataPackage)
Searches for a list of GuildMembers of a chosen Guild.
static CoRoutine< GuildMemberData > timeoutGuildMemberAsync(TimeoutGuildMemberData dataPackage)
Times-out a chosen GuildMemberData from a chosen Guild.
static CoRoutine< GuildMemberData > addGuildMemberAsync(AddGuildMemberData dataPackage)
Adds a GuildMember to a chosen Guild.
static CoRoutine< jsonifier::vector< GuildMemberData > > listGuildMembersAsync(ListGuildMembersData dataPackage)
Lists all of the GuildMembers of a chosen Guild.
static CoRoutine< GuildMemberData > getGuildMemberAsync(GetGuildMemberData dataPackage)
Collects a GuildMember from the Discord servers.
static CoRoutine< GuildMemberData > modifyGuildMemberAsync(ModifyGuildMemberData dataPackage)
Modifies a GuildMember's properties.
static CoRoutine< void > removeGuildMemberAsync(RemoveGuildMemberData dataPackage)
Removes a chosen GuildMemberData from a chosen Guild.
static CoRoutine< GuildMemberData > modifyCurrentGuildMemberAsync(ModifyCurrentGuildMemberData dataPackage)
Modifies the current GuildMemberData's properties.
static GuildMemberCacheData getCachedGuildMember(GetGuildMemberData dataPackage)
Collects a GuildMember from the library's cache.
A class that extends TimeStampBase to provide additional functionality.
Definition: Base.hpp:688