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(simdjson::ondemand::value jsonObjectData) {
32 std::unique_ptr<Channel> channel{ std::make_unique<Channel>(jsonObjectData) };
33 *this = *static_cast<Thread*>(channel.get());
34 }
35
36 StartThreadWithMessageData::operator Jsonifier() {
37 Jsonifier 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 Jsonifier() {
45 Jsonifier 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 Jsonifier() {
55 Jsonifier 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 DiscordCoreAPI::Jsonifier();
59 }
60 for (auto& value: this->message.attachments) {
61 data["message"]["attachments"].emplaceBack(value.operator DiscordCoreAPI::Jsonifier());
62 }
63 if (this->message.components.size() == 0) {
64 data["message"]["components"].emplaceBack(ActionRowData{});
65 data["message"]["components"].getValue<Jsonifier::ArrayType>().clear();
66 } else {
67 for (auto& value: this->message.components) {
68 data["message"]["components"].emplaceBack(value.operator DiscordCoreAPI::Jsonifier());
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::ArrayType>().clear();
77 } else {
78 for (auto& value: this->message.embeds) {
79 data["message"]["embeds"].emplaceBack(value.operator DiscordCoreAPI::Jsonifier());
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 DiscordCoreAPI::Jsonifier();
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 co_return Threads::httpsClient->submitWorkloadAndGetResult<Thread>(workload);
109 }
110
112 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Post_Thread_Without_Message };
113 co_await NewThreadAwaitable<Thread>();
114 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
115 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads";
116 auto serializer = dataPackage.operator DiscordCoreAPI::Jsonifier();
117 serializer.refreshString(JsonifierSerializeType::Json);
118 workload.content = serializer.operator std::string();
119 workload.callStack = "Threads::startThreadWithoutMessageAsync()";
120 if (dataPackage.reason != "") {
121 workload.headersToInsert["X-Audit-Log-Reason"] = dataPackage.reason;
122 }
123 co_return Threads::httpsClient->submitWorkloadAndGetResult<Thread>(workload);
124 }
125
127 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Post_Thread_In_Forum_Channel };
128 co_await NewThreadAwaitable<Thread>();
129 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
130 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads";
131 auto serializer = dataPackage.operator DiscordCoreAPI::Jsonifier();
132 serializer.refreshString(JsonifierSerializeType::Json);
133 workload.content = serializer.operator std::string();
134 workload.callStack = "Threads::startThreadInForumChannelAsync()";
135 if (dataPackage.reason != "") {
136 workload.headersToInsert["X-Audit-Log-Reason"] = dataPackage.reason;
137 }
138 co_return Threads::httpsClient->submitWorkloadAndGetResult<Thread>(workload);
139 }
140
142 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Put_Self_In_Thread };
143 co_await NewThreadAwaitable<void>();
144 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Put;
145 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/@me";
146 workload.callStack = "Threads::joinThreadAsync()";
147 co_return Threads::httpsClient->submitWorkloadAndGetResult<void>(workload);
148 }
149
151 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Put_Thread_Member };
152 co_await NewThreadAwaitable<void>();
153 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Put;
154 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/" + dataPackage.userId;
155 workload.callStack = "Threads::addThreadMemberAsync()";
156 co_return Threads::httpsClient->submitWorkloadAndGetResult<void>(workload);
157 }
158
160 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_Self_From_Thread };
161 co_await NewThreadAwaitable<void>();
162 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
163 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/@me";
164 workload.callStack = "Threads::leaveThreadAsync()";
165 co_return Threads::httpsClient->submitWorkloadAndGetResult<void>(workload);
166 }
167
169 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_Thread_Member };
170 co_await NewThreadAwaitable<void>();
171 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
172 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/" + dataPackage.userId;
173 workload.callStack = "Threads::removeThreadMemberAsync()";
174 co_return Threads::httpsClient->submitWorkloadAndGetResult<void>(workload);
175 }
176
178 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Thread_Member };
179 co_await NewThreadAwaitable<ThreadMemberData>();
180 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
181 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/" + dataPackage.userId;
182 workload.callStack = "Threads::getThreadMemberAsync()";
183 co_return Threads::httpsClient->submitWorkloadAndGetResult<ThreadMemberData>(workload);
184 }
185
187 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Thread_Members };
188 co_await NewThreadAwaitable<std::vector<ThreadMemberData>>();
189 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
190 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members";
191 workload.callStack = "Threads::getThreadMembersAsync()";
192 co_return Threads::httpsClient->submitWorkloadAndGetResult<ThreadMemberDataVector>(workload);
193 }
194
196 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Active_Threads };
197 co_await NewThreadAwaitable<ActiveThreadsData>();
198 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
199 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads/active";
200 workload.callStack = "Threads::getActiveThreadsAsync()";
201 co_return Threads::httpsClient->submitWorkloadAndGetResult<ActiveThreadsData>(workload);
202 }
203
205 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Public_Archived_Threads };
206 co_await NewThreadAwaitable<ArchivedThreadsData>();
207 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
208 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads/archived/public";
209 if (dataPackage.before != "") {
210 workload.relativePath += "?before=" + dataPackage.before;
211 if (dataPackage.limit != 0) {
212 workload.relativePath += "&limit=" + std::to_string(dataPackage.limit);
213 }
214 } else if (dataPackage.limit != 0) {
215 workload.relativePath += "?limit=" + std::to_string(dataPackage.limit);
216 }
217 workload.callStack = "Threads::getPublicArchivedThreadsAsync()";
218 co_return Threads::httpsClient->submitWorkloadAndGetResult<ArchivedThreadsData>(workload);
219 }
220
222 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Private_Archived_Threads };
223 co_await NewThreadAwaitable<ArchivedThreadsData>();
224 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
225 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads/archived/private";
226 if (dataPackage.before != "") {
227 workload.relativePath += "?before=" + dataPackage.before;
228 if (dataPackage.limit != 0) {
229 workload.relativePath += "&limit=" + std::to_string(dataPackage.limit);
230 }
231 } else if (dataPackage.limit != 0) {
232 workload.relativePath += "?limit=" + std::to_string(dataPackage.limit);
233 }
234 workload.callStack = "Threads::getPrivateArchivedThreadsAsync()";
235 co_return Threads::httpsClient->submitWorkloadAndGetResult<ArchivedThreadsData>(workload);
236 }
237
239 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Joined_Private_Archived_Threads };
240 co_await NewThreadAwaitable<ArchivedThreadsData>();
241 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
242 workload.relativePath = "/channels/" + dataPackage.channelId + "/users/@me/threads/archived/private";
243 if (dataPackage.before != "") {
244 workload.relativePath += "?before=" + dataPackage.before;
245 if (dataPackage.limit != 0) {
246 workload.relativePath += "&limit=" + std::to_string(dataPackage.limit);
247 }
248 } else if (dataPackage.limit != 0) {
249 workload.relativePath += "?limit=" + std::to_string(dataPackage.limit);
250 }
251 workload.callStack = "Threads::getJoinedPrivateArchivedThreadsAsync()";
252 co_return Threads::httpsClient->submitWorkloadAndGetResult<ArchivedThreadsData>(workload);
253 }
254
256 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Guild_Active_Threads };
257 co_await NewThreadAwaitable<ActiveThreadsData>();
258 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
259 workload.relativePath = "/guilds/" + dataPackage.guildId + "/threads/active";
260 workload.callStack = "Threads::listActiveThreadsAsync()";
261 co_return Threads::httpsClient->submitWorkloadAndGetResult<ActiveThreadsData>(workload);
262 }
263 DiscordCoreInternal::HttpsClient* Threads::httpsClient{ nullptr };
264}
The main namespace for this library.
A CoRoutine - representing a potentially asynchronous operation/function.
Definition: CoRoutine.hpp:59
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.