38 template<>
struct core<DiscordCoreAPI::MessageResponseBase> {
40 static constexpr auto parseValue = objectVal(
"components", &ValueType::components,
"allowed_mentions", &ValueType::allowedMentions,
"embeds", &ValueType::embeds,
"files",
41 &ValueType::files,
"custom_id", &ValueType::customId,
"content", &ValueType::content,
"title", &ValueType::title,
"flags", &ValueType::flags,
"tts", &ValueType::tts);
44 template<>
struct core<DiscordCoreAPI::CreateMessageData> {
46 static constexpr auto parseValue = objectVal(
"components", &ValueType::components,
"allowed_mentions", &ValueType::allowedMentions,
"embeds", &ValueType::embeds,
"files",
47 &ValueType::files,
"custom_id", &ValueType::customId,
"content", &ValueType::content,
"title", &ValueType::title,
"flags", &ValueType::flags,
"tts", &ValueType::tts);
50 template<>
struct core<DiscordCoreAPI::EditMessageData> {
52 static constexpr auto parseValue = objectVal(
"components", &ValueType::components,
"allowed_mentions", &ValueType::allowedMentions,
"embeds", &ValueType::embeds,
"files",
53 &ValueType::files,
"custom_id", &ValueType::customId,
"content", &ValueType::content,
"title", &ValueType::title,
"flags", &ValueType::flags,
"tts", &ValueType::tts);
56 template<>
struct core<DiscordCoreAPI::DeleteMessagesBulkData> {
58 static constexpr auto parseValue = objectVal(
"messages", &ValueType::messageIds);
62namespace DiscordCoreAPI {
64 template<> UnorderedMap<std::string, UnboundedMessageBlock<MessageData>*> ObjectCollector<MessageData>::objectsBuffersMap{};
66 template<> ObjectCollector<MessageData>::ObjectCollector() {
67 collectorId = std::to_string(std::chrono::duration_cast<Milliseconds>(HRClock::now().time_since_epoch()).count());
68 ObjectCollector::objectsBuffersMap[collectorId] = &objectsBuffer;
71 template<>
void ObjectCollector<MessageData>::run(
74 int64_t startingTime =
static_cast<int64_t
>(std::chrono::duration_cast<Milliseconds>(HRClock::now().time_since_epoch()).count());
75 int64_t elapsedTime{};
76 while (elapsedTime < msToCollectFor && !coroHandle.promise().stopRequested()) {
77 MessageData message{};
78 waitForTimeToPass<MessageData>(objectsBuffer, message,
static_cast<uint64_t
>(msToCollectFor -
static_cast<uint64_t
>(elapsedTime)));
79 if (filteringFunction(message)) {
80 objectReturnData.objects.emplace_back(message);
82 if (
static_cast<int32_t
>(objectReturnData.objects.size()) >= quantityOfObjectsToCollect) {
86 elapsedTime = std::chrono::duration_cast<Milliseconds>(HRClock::now().time_since_epoch()).count() - startingTime;
91 ObjectFilter<MessageData> filteringFunctionNew) {
92 auto coroHandle =
co_await NewThreadAwaitable<ObjectCollectorReturnData>();
93 quantityOfObjectsToCollect = quantityToCollect;
94 filteringFunction = filteringFunctionNew;
95 msToCollectFor = msToCollectForNew;
98 co_return std::move(objectReturnData);
101 template<> ObjectCollector<MessageData>::~ObjectCollector() {
102 if (ObjectCollector::objectsBuffersMap.contains(collectorId)) {
103 ObjectCollector::objectsBuffersMap.erase(collectorId);
107 CreateMessageData::CreateMessageData(
const Snowflake channelIdNew) {
108 channelId = channelIdNew;
111 CreateMessageData::CreateMessageData(RespondToInputEventData dataPackage) {
112 channelId = dataPackage.channelId;
114 for (
auto& value: dataPackage.components) {
115 components.emplace_back(value);
118 for (
auto& value: dataPackage.embeds) {
119 embeds.emplace_back(value);
121 tts = dataPackage.tts;
124 CreateMessageData::CreateMessageData(MessageData dataPackage) {
125 channelId = dataPackage.channelId;
126 messageReference.
channelId = dataPackage.channelId;
128 messageReference.
guildId = dataPackage.guildId;
129 tts = dataPackage.tts;
132 CreateMessageData::CreateMessageData(InputEventData dataPackage) {
133 channelId = dataPackage.getChannelData().
id;
136 SendDMData::SendDMData(RespondToInputEventData dataPackage) {
137 targetUserId = dataPackage.targetUserId;
139 for (
auto& value: dataPackage.components) {
140 components.emplace_back(value);
143 for (
auto& value: dataPackage.embeds) {
144 embeds.emplace_back(value);
146 channelId = dataPackage.targetUserId;
147 tts = dataPackage.tts;
150 EditMessageData::EditMessageData(InputEventData dataPackage) {
151 channelId = dataPackage.getChannelData().
id;
152 messageId = dataPackage.getMessageData().
id;
155 EditMessageData::EditMessageData(RespondToInputEventData dataPackage) {
156 allowedMentions = dataPackage.allowedMentions;
157 channelId = dataPackage.channelId;
158 messageId = dataPackage.messageId;
159 for (
auto& value: dataPackage.components) {
160 components.emplace_back(value);
162 content = dataPackage.content;
163 for (
auto& value: dataPackage.embeds) {
164 embeds.emplace_back(value);
168 DeleteMessageData::DeleteMessageData(
const MessageData& messageToDelete) {
174 void Messages::initialize(DiscordCoreInternal::HttpsClient* client) {
175 Messages::httpsClient = client;
179 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Messages };
180 co_await NewThreadAwaitable<jsonifier::vector<MessageData>>();
181 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
182 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/messages";
184 workload.relativePath +=
"?around=" + std::to_string(dataPackage.
aroundThisId);
185 if (dataPackage.
limit != 0) {
186 workload.relativePath +=
"&limit=" + std::to_string(dataPackage.
limit);
188 workload.relativePath +=
"&limit=1";
191 workload.relativePath +=
"?before=" + dataPackage.
beforeThisId;
192 if (dataPackage.
limit != 0) {
193 workload.relativePath +=
"&limit=" + std::to_string(dataPackage.
limit);
195 workload.relativePath +=
"&limit=1";
198 workload.relativePath +=
"?after=" + dataPackage.
afterThisId;
199 if (dataPackage.
limit != 0) {
200 workload.relativePath +=
"&limit=" + std::to_string(dataPackage.
limit);
202 workload.relativePath +=
"&limit=1";
205 if (dataPackage.
limit != 0) {
206 workload.relativePath +=
"?limit=" + std::to_string(dataPackage.
limit);
208 workload.relativePath +=
"&limit=1";
211 workload.callStack =
"Messages::getMessagesAsync()";
212 jsonifier::vector<MessageData> returnData{};
213 Messages::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
214 co_return returnData;
218 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Message };
219 co_await NewThreadAwaitable<MessageData>();
220 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
221 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/messages/" + dataPackage.
id;
222 workload.callStack =
"Messages::getMessageAsync()";
224 Messages::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
225 co_return returnData;
229 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Post_Message };
230 co_await NewThreadAwaitable<MessageData>();
231 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
232 workload.relativePath =
"/channels/" + dataPackage.channelId +
"/messages";
233 if (dataPackage.files.size() > 0) {
234 workload.payloadType = DiscordCoreInternal::PayloadType::Multipart_Form;
235 parser.serializeJson(dataPackage, workload.content);
237 parser.serializeJson(dataPackage, workload.content);
239 workload.callStack =
"Messages::createMessageAsync()";
241 Messages::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
242 co_return returnData;
246 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Crosspost_Message };
247 co_await NewThreadAwaitable<MessageData>();
248 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
249 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/messages/" + dataPackage.
messageId +
"/crosspost";
250 workload.callStack =
"Messages::crosspostMessageAsync()";
252 Messages::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
253 co_return returnData;
257 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Patch_Message };
258 co_await NewThreadAwaitable<MessageData>();
259 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Patch;
260 workload.relativePath =
"/channels/" + dataPackage.channelId +
"/messages/" + dataPackage.messageId;
261 if (dataPackage.files.size() > 0) {
262 workload.payloadType = DiscordCoreInternal::PayloadType::Multipart_Form;
263 parser.serializeJson(dataPackage, workload.content);
265 parser.serializeJson(dataPackage, workload.content);
267 workload.callStack =
"Messages::editMessageAsync()";
269 Messages::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
270 co_return returnData;
274 DiscordCoreInternal::HttpsWorkloadData workload{};
276 if (!hasTimeElapsedNew) {
277 workload = DiscordCoreInternal::HttpsWorkloadType::Delete_Message;
279 workload = DiscordCoreInternal::HttpsWorkloadType::Delete_Message_Old;
281 co_await NewThreadAwaitable<void>();
283 std::this_thread::sleep_for(Milliseconds{ dataPackage.
timeDelay });
285 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
286 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/messages/" + dataPackage.
messageId;
287 workload.callStack =
"Messages::deleteMessageAsync()";
288 if (dataPackage.
reason !=
"") {
289 workload.headersToInsert[
"X-Audit-Log-Reason"] = dataPackage.
reason;
291 Messages::httpsClient->submitWorkloadAndGetResult(std::move(workload));
296 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Bulk_Delete_Messages };
297 co_await NewThreadAwaitable<void>();
298 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
299 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/messages/bulk-delete";
300 parser.serializeJson(dataPackage, workload.content);
301 if (dataPackage.
reason !=
"") {
302 workload.headersToInsert[
"X-Audit-Log-Reason"] = dataPackage.
reason;
304 Messages::httpsClient->submitWorkloadAndGetResult(std::move(workload));
309 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Pinned_Messages };
310 co_await NewThreadAwaitable<jsonifier::vector<MessageData>>();
311 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
312 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/pins";
313 workload.callStack =
"Messages::getPinnedMessagesAsync()";
314 jsonifier::vector<MessageData> returnData{};
315 Messages::httpsClient->submitWorkloadAndGetResult(std::move(workload), returnData);
316 co_return returnData;
320 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Put_Pin_Message };
321 co_await NewThreadAwaitable<void>();
322 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Put;
323 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/pins/" + dataPackage.
messageId;
324 workload.callStack =
"Messages::pinMessageAsync()";
325 if (dataPackage.
reason !=
"") {
326 workload.headersToInsert[
"X-Audit-Log-Reason"] = dataPackage.
reason;
328 Messages::httpsClient->submitWorkloadAndGetResult(std::move(workload));
333 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_Pin_Message };
334 co_await NewThreadAwaitable<void>();
335 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
336 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/pins/" + dataPackage.
messageId;
337 workload.callStack =
"Messages::unpinMessageAsync()";
338 if (dataPackage.
reason !=
"") {
339 workload.headersToInsert[
"X-Audit-Log-Reason"] = dataPackage.
reason;
341 Messages::httpsClient->submitWorkloadAndGetResult(std::move(workload));
345 DiscordCoreInternal::HttpsClient* Messages::httpsClient{};
A CoRoutine - representing a potentially asynchronous operation/function.
Snowflake guildId
Snowflake of the Guild that the referenced Message was sent in.
Snowflake channelId
Snowflake of the ChannelData that the referenced Message was sent in.
Snowflake messageId
Snowflake of the Message to reference.
Data structure representing a single MessageData.
Message response base, for responding to messages.
MessageResponseBase & addContent(const std::string &dataPackage)
For setting the Message content in a response.
MessageResponseBase & addAllowedMentions(AllowedMentionsData dataPackage)
For setting the allowable mentions in a response.
CoRoutine< ObjectCollectorReturnData > collectObjects(int32_t quantityToCollect, int32_t msToCollectForNew, ObjectFilter< ValueType > filteringFunctionNew)
Begin waiting for Objects.
ObjectCollectorReturnData responseData.c.
For getting a collection of Messages.
Snowflake channelId
ChannelData from which to collect the Messages.
uint64_t aroundThisId
Around this id.
Snowflake beforeThisId
Before this id.
Snowflake afterThisId
After this id.
int32_t limit
Limit of Messages to collect.
Snowflake id
The id of the Message to collect.
Snowflake channelId
The ChannelData from which to collect the MessageData.
For crossposting a Message.
Snowflake messageId
Snowflake of the message to be crossposted.
Snowflake channelId
ChannelData within which to crosspost the Message from.
int32_t timeDelay
Number of Milliseconds to wait before deleting the MessageData.
Snowflake messageId
The message Snowflake of the Message to delete.
Snowflake channelId
The channel Snowflake of the Message to delete.
std::string reason
The reason for deleting the MessageData.
TimeStampParse timeStamp
The created-at timeStamp of the original message.
For deleting a bulk of Messages.
std::string reason
The reason for deleting the Messages.
Snowflake channelId
ChannelData within which to delete the Messages.
For getting a collection of pinned Messages.
Snowflake channelId
The ChannelData from which to collect pinned Messages.
For pinning a single MessageData.
Snowflake channelId
The ChannelData within which to pin the MessageData.
Snowflake messageId
The Message which you would like to pin.
std::string reason
Reason for pinning this MessageData.
For unpinning a single MessageData.
std::string reason
Reason for pinning this MessageData.
Snowflake messageId
The Message which you would like to unpin.
Snowflake channelId
The ChannelData within which to unpin the MessageData.
static CoRoutine< MessageData > createMessageAsync(CreateMessageData dataPackage)
Creates a new MessageData.
static CoRoutine< MessageData > crosspostMessageAsync(CrosspostMessageData dataPackage)
Crossposts a message from a News ChannelData to the following Channels.
static CoRoutine< void > deleteMessagesBulkAsync(DeleteMessagesBulkData dataPackage)
Deletes a collection of Messages.
static CoRoutine< jsonifier::vector< MessageData > > getPinnedMessagesAsync(GetPinnedMessagesData dataPackage)
Collects a collection of pinned Messages from the Discord servers.
static CoRoutine< MessageData > editMessageAsync(EditMessageData dataPackage)
Edit a Message.
static CoRoutine< void > deleteMessageAsync(DeleteMessageData dataPackage)
Deletes a Message.
static CoRoutine< MessageData > getMessageAsync(GetMessageData dataPackage)
Collects a Message from the Discord servers.
static CoRoutine< void > pinMessageAsync(PinMessageData dataPackage)
Pins a Message to a given Channel.
static CoRoutine< void > unpinMessageAsync(UnpinMessageData dataPackage)
Unpins a Message from a given Channel.
static CoRoutine< jsonifier::vector< MessageData > > getMessagesAsync(GetMessagesData dataPackage)
Collects a collection of Message from the Discord servers.
bool hasTimeElapsed(uint64_t days, uint64_t hours, uint64_t minutes) const
Checks if a certain time duration has elapsed.
uint64_t id
The Snowflake ID.