DiscordCoreAPI
A Discord bot library written in C++, with custom asynchronous coroutines.
Loading...
Searching...
No Matches
ThreadEntities.cpp
Go to the documentation of this file.
1/*
2 DiscordCoreAPI, A bot library for Discord, written in C++, and featuring explicit multithreading through the usage of custom, asynchronous C++ CoRoutines.
3
4 Copyright 2021, 2022 Chris M. (RealTimeChris)
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19 USA
20*/
21/// ThreadEntities.cpp - Source file for the thread-related stuff.
22/// Nov 29, 2021
23/// https://discordcoreapi.com
24/// \file ThreadEntities.cpp
25
28
29namespace DiscordCoreAPI {
30
31 Thread::Thread(Value jsonData) {
32 std::unique_ptr<Channel> channel{ std::make_unique<Channel>(jsonData) };
33 *this = *static_cast<Thread*>(channel.get());
34 }
35
36 StartThreadWithMessageData::operator Serializer() {
37 Serializer data{};
38 data["auto_archive_duration"] = this->autoArchiveDuration;
39 data["rate_limit_per_user"] = this->rateLimitPerUser;
40 data["name"] = this->threadName;
41 return data;
42 }
43
44 StartThreadWithoutMessageData::operator Serializer() {
45 Serializer data{};
46 data["auto_archive_duration"] = this->autoArchiveDuration;
47 data["rate_limit_per_user"] = this->rateLimitPerUser;
48 data["invitable"] = this->invitable;
49 data["name"] = this->threadName;
50 data["type"] = this->type;
51 return data;
52 }
53
54 StartThreadInForumChannelData::operator Serializer() {
55 Serializer data{};
56 if (this->message.allowedMentions.parse.size() > 0 || this->message.allowedMentions.roles.size() > 0 ||
57 this->message.allowedMentions.users.size() > 0) {
58 data["message"]["allowed_mentions"] = this->message.allowedMentions.operator Serializer();
59 }
60 for (auto& value: this->message.attachments) {
61 data["message"]["attachments"].emplaceBack(value.operator Serializer());
62 }
63 if (this->message.components.size() == 0) {
64 data["message"]["components"].emplaceBack(ActionRowData{});
65 data["message"]["components"].getValue<Jsonifier::Serializer::ArrayType>().clear();
66 } else {
67 for (auto& value: this->message.components) {
68 data["message"]["components"].emplaceBack(value.operator Serializer());
69 }
70 }
71 for (auto& value: this->message.stickerIds) {
72 data["message"]["sticker_ids"].emplaceBack(value);
73 }
74 if (this->message.embeds.size() == 0) {
75 data["message"]["embeds"].emplaceBack(EmbedData{});
76 data["message"]["embeds"].getValue<Jsonifier::Serializer::ArrayType>().clear();
77 } else {
78 for (auto& value: this->message.embeds) {
79 data["message"]["embeds"].emplaceBack(value.operator Serializer());
80 }
81 }
82 if (this->message.content != "") {
83 data["message"]["content"] = this->message.content;
84 }
85 data["message"]["flags"] = this->message.flags;
86 data["name"] = this->name;
87 data["auto_archive_duration"] = this->autoArchiveDuration;
88 data["rate_limit_per_user"] = this->rateLimitPerUser;
89 return data;
90 }
91
92 void Threads::initialize(DiscordCoreInternal::HttpsClient* client) {
93 Threads::httpsClient = client;
94 }
95
97 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Post_Thread_With_Message };
98 co_await NewThreadAwaitable<Thread>();
99 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
100 workload.relativePath = "/channels/" + dataPackage.channelId + "/messages/" + dataPackage.messageId + "/threads";
101 auto serializer = dataPackage.operator Serializer();
102 serializer.refreshString(JsonifierSerializeType::Json);
103 workload.content = serializer.operator std::string();
104 workload.callStack = "Threads::startThreadWithMessageAsync()";
105 if (dataPackage.reason != "") {
106 workload.headersToInsert["X-Audit-Log-Reason"] = dataPackage.reason;
107 }
108 Thread returnValue{};
109 co_return Threads::httpsClient->submitWorkloadAndGetResult<Thread>(workload, &returnValue);
110 }
111
113 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Post_Thread_Without_Message };
114 co_await NewThreadAwaitable<Thread>();
115 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
116 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads";
117 auto serializer = dataPackage.operator Serializer();
118 serializer.refreshString(JsonifierSerializeType::Json);
119 workload.content = serializer.operator std::string();
120 workload.callStack = "Threads::startThreadWithoutMessageAsync()";
121 if (dataPackage.reason != "") {
122 workload.headersToInsert["X-Audit-Log-Reason"] = dataPackage.reason;
123 }
124 Thread returnValue{};
125 co_return Threads::httpsClient->submitWorkloadAndGetResult<Thread>(workload, &returnValue);
126 }
127
129 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Post_Thread_In_Forum_Channel };
130 co_await NewThreadAwaitable<Thread>();
131 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
132 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads";
133 auto serializer = dataPackage.operator Serializer();
134 serializer.refreshString(JsonifierSerializeType::Json);
135 workload.content = serializer.operator std::string();
136 workload.callStack = "Threads::startThreadInForumChannelAsync()";
137 if (dataPackage.reason != "") {
138 workload.headersToInsert["X-Audit-Log-Reason"] = dataPackage.reason;
139 }
140 Thread returnValue{};
141 co_return Threads::httpsClient->submitWorkloadAndGetResult<Thread>(workload, &returnValue);
142 }
143
145 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Put_Self_In_Thread };
146 co_await NewThreadAwaitable<void>();
147 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Put;
148 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/@me";
149 workload.callStack = "Threads::joinThreadAsync()";
150 co_return Threads::httpsClient->submitWorkloadAndGetResult<void>(workload);
151 }
152
154 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Put_Thread_Member };
155 co_await NewThreadAwaitable<void>();
156 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Put;
157 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/" + dataPackage.userId;
158 workload.callStack = "Threads::addThreadMemberAsync()";
159 co_return Threads::httpsClient->submitWorkloadAndGetResult<void>(workload);
160 }
161
163 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_Self_From_Thread };
164 co_await NewThreadAwaitable<void>();
165 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
166 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/@me";
167 workload.callStack = "Threads::leaveThreadAsync()";
168 co_return Threads::httpsClient->submitWorkloadAndGetResult<void>(workload);
169 }
170
172 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_Thread_Member };
173 co_await NewThreadAwaitable<void>();
174 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
175 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/" + dataPackage.userId;
176 workload.callStack = "Threads::removeThreadMemberAsync()";
177 co_return Threads::httpsClient->submitWorkloadAndGetResult<void>(workload);
178 }
179
181 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Thread_Member };
182 co_await NewThreadAwaitable<ThreadMemberData>();
183 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
184 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/" + dataPackage.userId;
185 workload.callStack = "Threads::getThreadMemberAsync()";
186 ThreadMemberData returnValue{};
187 co_return Threads::httpsClient->submitWorkloadAndGetResult<ThreadMemberData>(workload, &returnValue);
188 }
189
191 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Thread_Members };
192 co_await NewThreadAwaitable<std::vector<ThreadMemberData>>();
193 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
194 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members";
195 workload.callStack = "Threads::getThreadMembersAsync()";
196 ThreadMemberDataVector returnValue{};
197 co_return Threads::httpsClient->submitWorkloadAndGetResult<ThreadMemberDataVector>(workload, &returnValue);
198 }
199
201 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Active_Threads };
202 co_await NewThreadAwaitable<ActiveThreadsData>();
203 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
204 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads/active";
205 workload.callStack = "Threads::getActiveThreadsAsync()";
206 ActiveThreadsData returnValue{};
207 co_return Threads::httpsClient->submitWorkloadAndGetResult<ActiveThreadsData>(workload, &returnValue);
208 }
209
211 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Public_Archived_Threads };
212 co_await NewThreadAwaitable<ArchivedThreadsData>();
213 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
214 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads/archived/public";
215 if (dataPackage.before != "") {
216 workload.relativePath += "?before=" + dataPackage.before;
217 if (dataPackage.limit != 0) {
218 workload.relativePath += "&limit=" + std::to_string(dataPackage.limit);
219 }
220 } else if (dataPackage.limit != 0) {
221 workload.relativePath += "?limit=" + std::to_string(dataPackage.limit);
222 }
223 workload.callStack = "Threads::getPublicArchivedThreadsAsync()";
224 ArchivedThreadsData returnValue{};
225 co_return Threads::httpsClient->submitWorkloadAndGetResult<ArchivedThreadsData>(workload, &returnValue);
226 }
227
229 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Private_Archived_Threads };
230 co_await NewThreadAwaitable<ArchivedThreadsData>();
231 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
232 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads/archived/private";
233 if (dataPackage.before != "") {
234 workload.relativePath += "?before=" + dataPackage.before;
235 if (dataPackage.limit != 0) {
236 workload.relativePath += "&limit=" + std::to_string(dataPackage.limit);
237 }
238 } else if (dataPackage.limit != 0) {
239 workload.relativePath += "?limit=" + std::to_string(dataPackage.limit);
240 }
241 workload.callStack = "Threads::getPrivateArchivedThreadsAsync()";
242 ArchivedThreadsData returnValue{};
243 co_return Threads::httpsClient->submitWorkloadAndGetResult<ArchivedThreadsData>(workload, &returnValue);
244 }
245
247 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Joined_Private_Archived_Threads };
248 co_await NewThreadAwaitable<ArchivedThreadsData>();
249 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
250 workload.relativePath = "/channels/" + dataPackage.channelId + "/users/@me/threads/archived/private";
251 if (dataPackage.before != "") {
252 workload.relativePath += "?before=" + dataPackage.before;
253 if (dataPackage.limit != 0) {
254 workload.relativePath += "&limit=" + std::to_string(dataPackage.limit);
255 }
256 } else if (dataPackage.limit != 0) {
257 workload.relativePath += "?limit=" + std::to_string(dataPackage.limit);
258 }
259 workload.callStack = "Threads::getJoinedPrivateArchivedThreadsAsync()";
260 ArchivedThreadsData returnValue{};
261 co_return Threads::httpsClient->submitWorkloadAndGetResult<ArchivedThreadsData>(workload, &returnValue);
262 }
263
265 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Guild_Active_Threads };
266 co_await NewThreadAwaitable<ActiveThreadsData>();
267 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
268 workload.relativePath = "/guilds/" + dataPackage.guildId + "/threads/active";
269 workload.callStack = "Threads::listActiveThreadsAsync()";
270 ActiveThreadsData returnValue{};
271 co_return Threads::httpsClient->submitWorkloadAndGetResult<ActiveThreadsData>(workload, &returnValue);
272 }
273 DiscordCoreInternal::HttpsClient* Threads::httpsClient{ nullptr };
274}
The main namespace for this library.
A CoRoutine - representing a potentially asynchronous operation/function.
Definition: CoRoutine.hpp:52
Data for a single member of a Thread.
Data representing an active Thread.
Data representing an archived Thread.
For starting a Thread, based on a Message.
Snowflake channelId
The Channel to start the Thread in.
Snowflake messageId
The Message Id to base the Thread off of.
std::string reason
Reason for starting the Thread.
For starting a Thread, not based on a Message.
std::string reason
Reason for starting the Thread.
Snowflake channelId
The Channel to start the Thread in.
For starting a Thread, in a forum channel.
std::string reason
Reason for starting the Thread.
Snowflake channelId
The id of the channel.
Snowflake channelId
The id of the Thread to join.
For adding a chosen User to a chosen Thread.
Snowflake userId
The id of the User to add to the Thread.
Snowflake channelId
The id of the Thread to join.
Snowflake channelId
The id of the Thread to leave.
For removing a chosen User from a Thread.
Snowflake channelId
The id of the Thread to remove them from.
Snowflake userId
The id of the User to remove from the Thread.
For collecting a ThreadMember responseData structure for a given ThreadMember.
Snowflake userId
The id of the User to collect from the Thread.
Snowflake channelId
The id of the Thread to collect them from.
For collecting the list of ThreadMembers from a Thread.
Snowflake channelId
The id of the Thread to collect them from.
For collecting the list of active Threads.
Snowflake channelId
The id of the Channel to collect the Threads from.
For collecting puiblic archived Threads from a given Channel.
std::string before
Returns threads before this timeStamp.
int32_t limit
Maximum number of threads to return.
Snowflake channelId
The Channel to acquire the Threads from.
For collecting private archived Threads from a given Channel.
std::string before
Returns threads before this timeStamp.
int32_t limit
Maximum number of threads to return.
Snowflake channelId
The Channel to acquire the Threads from.
For collecting joined private archived Threads from a given Channel.
int32_t limit
Maximum number of threads to return.
std::string before
Returns threads before this timeStamp.
Snowflake channelId
The Channel to acquire the Threads from.
For listing the active Threads in a chosen Guild.
Snowflake guildId
The Guild from which to list the Threads from.
Represents a single Thread.
static CoRoutine< ThreadMemberData > getThreadMemberAsync(GetThreadMemberData dataPackage)
Collects a ThreadMember if they exist.
static CoRoutine< ActiveThreadsData > getActiveThreadsAsync(GetActiveThreadsData dataPackage)
Collects a list of Threads from a given Channel.
static CoRoutine< ArchivedThreadsData > getJoinedPrivateArchivedThreadsAsync(GetJoinedPrivateArchivedThreadsData dataPackage)
Collects a list of joined private archived Threads from a given Channel.
static CoRoutine< Thread > startThreadWithMessageAsync(StartThreadWithMessageData dataPackage)
Starts a Thread, based on a starting Message.
static CoRoutine< ArchivedThreadsData > getPrivateArchivedThreadsAsync(GetPrivateArchivedThreadsData dataPackage)
Collects a list of private archived Threads from a given Channel.
static CoRoutine< Thread > startThreadWithoutMessageAsync(StartThreadWithoutMessageData dataPackage)
Starts a Thread, not based on a starting Message.
static CoRoutine< ArchivedThreadsData > getPublicArchivedThreadsAsync(GetPublicArchivedThreadsData dataPackage)
Collects a list of public archived Threads from a given Channel.
static CoRoutine< ActiveThreadsData > getActiveGuildThreadsAsync(GetActiveGuildThreadsData dataPackage)
Lists all of the active Threads of a chosen Guild.
static CoRoutine< void > addThreadMemberAsync(AddThreadMemberData dataPackage)
Adds a new User to a chosen Thread.
static CoRoutine< void > leaveThreadAsync(LeaveThreadData dataPackage)
Leaves a Thread.
static CoRoutine< void > removeThreadMemberAsync(RemoveThreadMemberData dataPackage)
Removes a User from a chosen Thread.
static CoRoutine< Thread > startThreadInForumChannelAsync(StartThreadInForumChannelData dataPackage)
Starts a Thread, in a forum channel.
static CoRoutine< std::vector< ThreadMemberData > > getThreadMembersAsync(GetThreadMembersData dataPackage)
Collects a list of ThreadMembers if they exist.
static CoRoutine< void > joinThreadAsync(JoinThreadData dataPackage)
Joins a Thread.