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 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/// ThreadEntities.cpp - Source file for the thread-related stuff.
27/// Nov 29, 2021
28/// https://discordcoreapi.com
29/// \file ThreadEntities.cpp
30
35
36namespace jsonifier {
37
38 template<> struct core<discord_core_api::forum_thread_message_data> {
39 using value_type = discord_core_api::forum_thread_message_data;
40 static constexpr auto parseValue =
41 createValue("attachments", &value_type::attachments, "components", &value_type::components, "allowedMentions", &value_type::allowedMentions, "stickerIds",
42 &value_type::stickerIds, "embeds", &value_type::embeds, "content", &value_type::content, "flags", &value_type::flags);
43 };
44
45 template<> struct core<discord_core_api::start_thread_with_message_data> {
46 using value_type = discord_core_api::start_thread_with_message_data;
47 static constexpr auto parseValue = createValue("autoArchiveDuration", &value_type::autoArchiveDuration, "rateLimitPerUser", &value_type::rateLimitPerUser, "threadName",
48 &value_type::threadName, "messageId", &value_type::messageId, "channelId", &value_type::channelId);
49 };
50
51 template<> struct core<discord_core_api::start_thread_without_message_data> {
52 using value_type = discord_core_api::start_thread_without_message_data;
53 static constexpr auto parseValue =
54 createValue("autoArchiveDuration", &value_type::autoArchiveDuration, "type", &value_type::type, "rateLimitPerUser", &value_type::rateLimitPerUser, "threadName",
55 &value_type::threadName, "channelId", &value_type::channelId, "invitable", &value_type::invitable);
56 };
57
58 template<> struct core<discord_core_api::start_thread_in_forum_channel_data> {
59 using value_type = discord_core_api::start_thread_in_forum_channel_data;
60 static constexpr auto parseValue = createValue("autoArchiveDuration", &value_type::autoArchiveDuration, "message", &value_type::message, "rateLimitPerUser",
61 &value_type::rateLimitPerUser, "channelId", &value_type::channelId, "name", &value_type::name);
62 };
63
64}
65
66namespace discord_core_api {
67
68 void threads::initialize(discord_core_internal::https_client* client) {
69 threads::httpsClient = client;
70 }
71
73 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Post_Thread_With_Message };
75 workload.workloadClass = discord_core_internal::https_workload_class::Post;
76 workload.relativePath = "/channels/" + dataPackage.channelId + "/messages/" + dataPackage.messageId + "/threads";
77 parser.serializeJson(dataPackage, workload.content);
78 workload.callStack = "threads::startThreadWithMessageAsync()";
79 if (dataPackage.reason != "") {
80 workload.headersToInsert["x-audit-log-reason"] = dataPackage.reason;
81 }
82 thread_data returnData{};
83 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
84 co_return returnData;
85 }
86
88 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Post_Thread_Without_Message };
90 workload.workloadClass = discord_core_internal::https_workload_class::Post;
91 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads";
92 parser.serializeJson(dataPackage, workload.content);
93 workload.callStack = "threads::startThreadWithoutMessageAsync()";
94 if (dataPackage.reason != "") {
95 workload.headersToInsert["x-audit-log-reason"] = dataPackage.reason;
96 }
97 thread_data returnData{};
98 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
99 co_return returnData;
100 }
101
103 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Post_Thread_In_Forum_Channel };
105 workload.workloadClass = discord_core_internal::https_workload_class::Post;
106 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads";
107 parser.serializeJson(dataPackage, workload.content);
108 workload.callStack = "threads::startThreadInForumChannelAsync()";
109 if (dataPackage.reason != "") {
110 workload.headersToInsert["x-audit-log-reason"] = dataPackage.reason;
111 }
112 thread_data returnData{};
113 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
114 co_return returnData;
115 }
116
118 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Put_Self_In_Thread };
119 co_await newThreadAwaitable<void>();
120 workload.workloadClass = discord_core_internal::https_workload_class::Put;
121 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/@me";
122 workload.callStack = "threads::joinThreadAsync()";
123 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload));
124 co_return;
125 }
126
128 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Put_Thread_Member };
129 co_await newThreadAwaitable<void>();
130 workload.workloadClass = discord_core_internal::https_workload_class::Put;
131 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/" + dataPackage.userId;
132 workload.callStack = "threads::addThreadMemberAsync()";
133 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload));
134 co_return;
135 }
136
138 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Delete_Self_From_Thread };
139 co_await newThreadAwaitable<void>();
140 workload.workloadClass = discord_core_internal::https_workload_class::Delete;
141 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/@me";
142 workload.callStack = "threads::leaveThreadAsync()";
143 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload));
144 co_return;
145 }
146
148 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Delete_Thread_Member };
149 co_await newThreadAwaitable<void>();
150 workload.workloadClass = discord_core_internal::https_workload_class::Delete;
151 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/" + dataPackage.userId;
152 workload.callStack = "threads::removeThreadMemberAsync()";
153 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload));
154 co_return;
155 }
156
158 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Get_Thread_Member };
160 workload.workloadClass = discord_core_internal::https_workload_class::Get;
161 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members/" + dataPackage.userId;
162 workload.callStack = "threads::getThreadMemberAsync()";
163 thread_member_data returnData{};
164 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
165 co_return returnData;
166 }
167
169 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Get_Thread_Members };
171 workload.workloadClass = discord_core_internal::https_workload_class::Get;
172 workload.relativePath = "/channels/" + dataPackage.channelId + "/thread-members";
173 workload.callStack = "threads::getThreadMembersAsync()";
174 jsonifier::vector<thread_member_data> returnData{};
175 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
176 co_return returnData;
177 }
178
180 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Get_Active_Threads };
182 workload.workloadClass = discord_core_internal::https_workload_class::Get;
183 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads/active";
184 workload.callStack = "threads::getActiveThreadsAsync()";
185 active_threads_data returnData{};
186 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
187 co_return returnData;
188 }
189
191 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Get_Public_Archived_Threads };
193 workload.workloadClass = discord_core_internal::https_workload_class::Get;
194 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads/archived/public";
195 if (dataPackage.before != "") {
196 workload.relativePath += "?before=" + dataPackage.before;
197 if (dataPackage.limit != 0) {
198 workload.relativePath += "&limit=" + jsonifier::toString(dataPackage.limit);
199 }
200 } else if (dataPackage.limit != 0) {
201 workload.relativePath += "?limit=" + jsonifier::toString(dataPackage.limit);
202 }
203 workload.callStack = "threads::getPublicArchivedThreadsAsync()";
204 archived_threads_data returnData{};
205 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
206 co_return returnData;
207 }
208
210 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Get_Private_Archived_Threads };
212 workload.workloadClass = discord_core_internal::https_workload_class::Get;
213 workload.relativePath = "/channels/" + dataPackage.channelId + "/threads/archived/protected";
214 if (dataPackage.before != "") {
215 workload.relativePath += "?before=" + dataPackage.before;
216 if (dataPackage.limit != 0) {
217 workload.relativePath += "&limit=" + jsonifier::toString(dataPackage.limit);
218 }
219 } else if (dataPackage.limit != 0) {
220 workload.relativePath += "?limit=" + jsonifier::toString(dataPackage.limit);
221 }
222 workload.callStack = "threads::getPrivateArchivedThreadsAsync()";
223 archived_threads_data returnData{};
224 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
225 co_return returnData;
226 }
227
229 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Get_Joined_Private_Archived_Threads };
231 workload.workloadClass = discord_core_internal::https_workload_class::Get;
232 workload.relativePath = "/channels/" + dataPackage.channelId + "/users/@me/threads/archived/protected";
233 if (dataPackage.before != "") {
234 workload.relativePath += "?before=" + dataPackage.before;
235 if (dataPackage.limit != 0) {
236 workload.relativePath += "&limit=" + jsonifier::toString(dataPackage.limit);
237 }
238 } else if (dataPackage.limit != 0) {
239 workload.relativePath += "?limit=" + jsonifier::toString(dataPackage.limit);
240 }
241 workload.callStack = "threads::getJoinedPrivateArchivedThreadsAsync()";
242 archived_threads_data returnData{};
243 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
244 co_return returnData;
245 }
246
248 discord_core_internal::https_workload_data workload{ discord_core_internal::https_workload_type::Get_Guild_Active_Threads };
250 workload.workloadClass = discord_core_internal::https_workload_class::Get;
251 workload.relativePath = "/guilds/" + dataPackage.guildId + "/threads/active";
252 workload.callStack = "threads::listActiveThreadsAsync()";
253 active_threads_data returnData{};
254 threads::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
255 co_return returnData;
256 }
257
258 discord_core_internal::https_client* threads::httpsClient{};
259}
A co_routine - representing a potentially asynchronous operation/function.
static co_routine< jsonifier::vector< thread_member_data > > getThreadMembersAsync(get_thread_members_data dataPackage)
Collects a list of thread_members if they exist.
static co_routine< active_threads_data > getActiveGuildThreadsAsync(get_active_guild_threads_data dataPackage)
Lists all of the active threads of a chosen guild.
static co_routine< archived_threads_data > getJoinedPrivateArchivedThreadsAsync(get_joined_private_archived_threads_data dataPackage)
Collects a list of joined protected archived threads from a given channel.
static co_routine< thread_member_data > getThreadMemberAsync(get_thread_member_data dataPackage)
Collects a thread_member if they exist.
static co_routine< archived_threads_data > getPrivateArchivedThreadsAsync(get_private_archived_threads_data dataPackage)
Collects a list of protected archived threads from a given channel.
static co_routine< void > addThreadMemberAsync(add_thread_member_data dataPackage)
Adds a new user_data to a chosen thread_data.
static co_routine< void > leaveThreadAsync(leave_thread_data dataPackage)
Leaves a thread_data.
static co_routine< thread_data > startThreadWithoutMessageAsync(start_thread_without_message_data dataPackage)
Starts a thread_data, not based on a starting message_data.
static co_routine< void > joinThreadAsync(join_thread_data dataPackage)
Joins a thread_data.
static co_routine< void > removeThreadMemberAsync(remove_thread_member_data dataPackage)
Removes a user from a chosen thread_data.
static co_routine< thread_data > startThreadWithMessageAsync(start_thread_with_message_data dataPackage)
Starts a thread_data, based on a starting message_data.
static co_routine< archived_threads_data > getPublicArchivedThreadsAsync(get_public_archived_threads_data dataPackage)
Collects a list of public archived threads from a given channel.
static co_routine< active_threads_data > getActiveThreadsAsync(get_active_threads_data dataPackage)
Collects a list of threads from a given channel.
static co_routine< thread_data > startThreadInForumChannelAsync(start_thread_in_forum_channel_data dataPackage)
Starts a thread_data, in a forum channel.
DCA_INLINE auto newThreadAwaitable()
An awaitable that can be used to launch the co_routine onto a new thread - as well as return the hand...
The main namespace for the forward-facing interfaces.
For adding a chosen user_data to a chosen thread_data.
snowflake channelId
The id of the thread_data to join.
snowflake userId
The id of the user_data to add to the thread_data.
For listing the active threads in a chosen guild.
snowflake guildId
The guild from which to list the threads from.
For collecting the list of active threads.
snowflake channelId
The id of the channel_data to collect the threads from.
For collecting joined protected archived threads from a given channel.
snowflake channelId
The channel_data to acquire the threads from.
jsonifier::string before
Returns threads before this timeStamp.
int32_t limit
Maximum number of threads to return.
For collecting protected archived threads from a given channel.
jsonifier::string before
Returns threads before this timeStamp.
snowflake channelId
The channel_data to acquire the threads from.
int32_t limit
Maximum number of threads to return.
For collecting puiblic archived threads from a given channel.
snowflake channelId
The channel_data to acquire the threads from.
jsonifier::string before
Returns threads before this timeStamp.
int32_t limit
Maximum number of threads to return.
For collecting a thread_member responseData structure for a given thread_member.
snowflake channelId
The id of the thread_data to collect them from.
snowflake userId
The id of the user_data to collect from the thread_data.
For collecting the list of thread_members from a thread_data.
snowflake channelId
The id of the thread_data to collect them from.
For joining a thread_data.
snowflake channelId
The id of the thread_data to join.
snowflake channelId
The id of the thread_data to leave.
For removing a chosen user_data from a thread_data.
snowflake channelId
The id of the thread_data to remove them from.
snowflake userId
The id of the user_data to remove from the thread_data.
For starting a thread_data, in a forum channel.
jsonifier::string reason
Reason for starting the thread_data.
For starting a thread_data, based on a message.
snowflake channelId
The channel_data to start the thread_data in.
jsonifier::string reason
Reason for starting the thread_data.
snowflake messageId
The message snowflake to base the thread_data off of.
For starting a thread_data, not based on a message.
jsonifier::string reason
Reason for starting the thread_data.
snowflake channelId
The channel_data to start the thread_data in.