35 co_await NewThreadAwaitable<MessageCollectorReturnData>();
36 this->quantityOfMessageToCollect = quantityToCollect;
37 this->filteringFunction = filteringFunctionNew;
38 this->msToCollectFor = msToCollectForNew;
39 this->collectorId = std::to_string(std::chrono::duration_cast<Milliseconds>(HRClock::now().time_since_epoch()).count());
40 MessageCollector::objectsBuffersMap[this->collectorId] = &this->objectsBuffer;
42 co_return std::move(this->objectReturnData);
45 void MessageCollector::run() {
46 int64_t startingTime =
static_cast<int64_t
>(std::chrono::duration_cast<Milliseconds>(HRClock::now().time_since_epoch()).count());
47 int64_t elapsedTime{};
48 while (elapsedTime < this->msToCollectFor) {
50 waitForTimeToPass<MessageData>(this->objectsBuffer, message,
static_cast<int32_t
>(this->msToCollectFor - elapsedTime));
51 if (this->filteringFunction(message)) {
52 this->objectReturnData.
messages.emplace_back(message);
54 if (
static_cast<int32_t
>(this->objectReturnData.
messages.size()) >= this->quantityOfMessageToCollect) {
58 elapsedTime = std::chrono::duration_cast<Milliseconds>(HRClock::now().time_since_epoch()).count() - startingTime;
62 MessageCollector::~MessageCollector() {
63 if (MessageCollector::objectsBuffersMap.contains(this->collectorId)) {
64 MessageCollector::objectsBuffersMap.erase(this->collectorId);
68 Message::Message(Value jsonData) {
71 this->
content = getString(jsonData,
"content");
73 this->
channelId = getId(jsonData,
"channel_id");
75 this->
guildId = getId(jsonData,
"guild_id");
77 this->
id = getId(jsonData,
"id");
80 if (!getObject(
object,
"author", jsonData)) {
81 this->
author = UserData{
object };
84 if (jsonData[
"member"].get(
object) == ErrorCode::Success) {
85 this->
member = GuildMemberData{
object };
88 this->
timeStamp = getString(jsonData,
"timestamp");
92 this->
tts = getBool(jsonData,
"tts");
97 if (!getArray(arrayValue,
"mentions", jsonData)) {
98 for (JsonifierResult<Value> value: arrayValue) {
99 UserData newData{ value.value() };
100 this->
mentions.emplace_back(std::move(newData));
104 if (!getArray(arrayValue,
"mention_roles", jsonData)) {
105 for (JsonifierResult<Value> value: arrayValue) {
106 this->
mentionRoles.emplace_back(value.getString().value());
110 if (!getArray(arrayValue,
"mention_channels", jsonData)) {
111 for (JsonifierResult<Value> value: arrayValue) {
112 ChannelMentionData newData{ value.value() };
117 if (!getArray(arrayValue,
"attachments", jsonData)) {
118 for (JsonifierResult<Value> value: arrayValue) {
119 AttachmentData newData{ value.value() };
120 this->
attachments.emplace_back(std::move(newData));
124 if (!getArray(arrayValue,
"embeds", jsonData)) {
125 for (JsonifierResult<Value> value: arrayValue) {
126 EmbedData newData{ value.value() };
127 this->
embeds.emplace_back(std::move(newData));
131 if (!getArray(arrayValue,
"reactions", jsonData)) {
132 for (JsonifierResult<Value> value: arrayValue) {
133 ReactionData newData{ value.value() };
134 this->reactions.emplace_back(std::move(newData));
138 this->
nonce = getString(jsonData,
"nonce");
140 this->
pinned = getBool(jsonData,
"pinned");
142 this->
webHookId = getId(jsonData,
"webhook_id");
146 if (!getObject(
object,
"activity", jsonData)) {
147 this->
activity = MessageActivityData{
object };
150 if (!getObject(
object,
"application", jsonData)) {
156 if (!getObject(
object,
"message_reference", jsonData)) {
160 this->
flags = getUint32(jsonData,
"flags");
162 if (!getArray(arrayValue,
"sticker_items", jsonData)) {
163 for (JsonifierResult<Value> value: arrayValue) {
164 StickerItemData newData{ value.value() };
169 if (!getArray(arrayValue,
"stickers", jsonData)) {
170 for (JsonifierResult<Value> value: arrayValue) {
171 StickerData newData{ value.value() };
172 this->
stickers.emplace_back(std::move(newData));
176 if (!getObject(
object,
"interaction", jsonData)) {
177 this->
interaction = MessageInteractionData{
object };
180 if (!getArray(arrayValue,
"components", jsonData)) {
181 for (JsonifierResult<Value> value: arrayValue) {
182 ActionRowData newData{ value.value() };
183 this->
components.emplace_back(std::move(newData));
187 if (!getObject(
object,
"thread", jsonData)) {
188 this->
thread = ChannelData{
object };
192 MessageVector::MessageVector(Value jsonData) {
193 if (jsonData.type().value() != JsonType::Null) {
195 if (!getArray(arrayValue, jsonData)) {
196 for (JsonifierResult<Value> value: arrayValue) {
197 Message newData{ value.value() };
198 this->messages.emplace_back(std::move(newData));
204 CreateMessageData::CreateMessageData(
const Snowflake channelIdNew) {
205 this->channelId = channelIdNew;
208 CreateMessageData::CreateMessageData(RespondToInputEventData dataPackage) {
209 this->channelId = dataPackage.channelId;
211 for (
auto& value: dataPackage.components) {
212 this->components.emplace_back(value);
215 for (
auto& value: dataPackage.embeds) {
216 this->embeds.emplace_back(value);
218 this->tts = dataPackage.tts;
221 CreateMessageData::CreateMessageData(InputEventData dataPackage) {
222 this->channelId = dataPackage.getChannelData().id;
225 CreateMessageData::operator Serializer() {
227 for (
auto& value: this->attachments) {
228 data[
"attachments"].emplaceBack(value.operator Serializer());
230 if (this->messageReference.messageId != 0) {
231 data[
"message_reference"] = this->messageReference.operator Serializer();
233 if (this->components.size() == 0) {
234 data[
"components"].emplaceBack(ActionRowData{});
235 data[
"components"].getValue<Jsonifier::Serializer::ArrayType>().clear();
237 for (
auto& value: this->components) {
238 data[
"components"].emplaceBack(value.operator Serializer());
241 if (this->allowedMentions.parse.size() > 0 || this->allowedMentions.roles.size() > 0 || this->allowedMentions.users.size() > 0) {
242 data[
"allowed_mentions"] = this->allowedMentions.operator Serializer();
244 for (
auto& value: this->stickerIds) {
245 data[
"sticker_ids"].emplaceBack(value);
247 if (this->embeds.size() == 0) {
248 data[
"embeds"].emplaceBack(EmbedData{});
249 data[
"embeds"].getValue<Jsonifier::Serializer::ArrayType>().clear();
251 for (
auto& value: this->embeds) {
252 data[
"embeds"].emplaceBack(value.operator Serializer());
255 if (this->content !=
"") {
256 data[
"content"] = this->content;
258 data[
"flags"] = this->flags;
259 data[
"tts"] = this->tts;
263 SendDMData::SendDMData(RespondToInputEventData dataPackage) {
264 this->targetUserId = dataPackage.targetUserId;
266 for (
auto& value: dataPackage.components) {
267 this->components.emplace_back(value);
270 for (
auto& value: dataPackage.embeds) {
271 this->embeds.emplace_back(value);
273 this->channelId = dataPackage.targetUserId;
274 this->tts = dataPackage.tts;
277 EditMessageData::EditMessageData(InputEventData dataPackage) {
278 this->channelId = dataPackage.getChannelData().id;
279 this->messageId = dataPackage.getMessageData().id;
282 EditMessageData::EditMessageData(RespondToInputEventData dataPackage) {
283 this->allowedMentions = dataPackage.allowedMentions;
284 this->channelId = dataPackage.channelId;
285 this->messageId = dataPackage.messageId;
286 for (
auto& value: dataPackage.components) {
287 this->components.emplace_back(value);
289 this->content = dataPackage.content;
290 for (
auto& value: dataPackage.embeds) {
291 this->embeds.emplace_back(value);
295 EditMessageData::operator Serializer() {
297 for (
auto& value: this->attachments) {
298 data[
"attachments"].emplaceBack(value.operator Serializer());
300 if (this->components.size() == 0) {
301 data[
"components"].emplaceBack(ActionRowData{});
302 data[
"components"].getValue<Jsonifier::Serializer::ArrayType>().clear();
304 for (
auto& value: this->components) {
305 data[
"components"].emplaceBack(value.operator Serializer());
308 if (this->allowedMentions.parse.size() > 0 || this->allowedMentions.roles.size() > 0 || this->allowedMentions.users.size() > 0) {
309 data[
"allowed_mentions"] = this->allowedMentions.operator Serializer();
311 if (this->embeds.size() == 0) {
312 data[
"embeds"].emplaceBack(EmbedData{});
313 data[
"embeds"].getValue<Jsonifier::Serializer::ArrayType>().clear();
315 for (
auto& value: this->embeds) {
316 data[
"embeds"].emplaceBack(value.operator Serializer());
319 if (this->content !=
"") {
320 data[
"content"] = this->content;
322 data[
"flags"] = this->flags;
323 data[
"tts"] = this->tts;
327 DeleteMessagesBulkData::operator Serializer() {
329 for (
auto& value: this->messageIds) {
330 data[
"messages"].emplaceBack(value.operator std::string());
335 MessageVector::operator std::vector<Message>() {
336 return this->messages;
339 void Messages::initialize(DiscordCoreInternal::HttpsClient* client) {
340 Messages::httpsClient = client;
344 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Messages };
345 co_await NewThreadAwaitable<std::vector<Message>>();
346 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
347 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/messages";
349 workload.relativePath +=
"?around=" + std::to_string(dataPackage.
aroundThisId);
350 if (dataPackage.
limit != 0) {
351 workload.relativePath +=
"&limit=" + std::to_string(dataPackage.
limit);
353 workload.relativePath +=
"&limit=1";
356 workload.relativePath +=
"?before=" + dataPackage.
beforeThisId;
357 if (dataPackage.
limit != 0) {
358 workload.relativePath +=
"&limit=" + std::to_string(dataPackage.
limit);
360 workload.relativePath +=
"&limit=1";
363 workload.relativePath +=
"?after=" + dataPackage.
afterThisId;
364 if (dataPackage.
limit != 0) {
365 workload.relativePath +=
"&limit=" + std::to_string(dataPackage.
limit);
367 workload.relativePath +=
"&limit=1";
370 if (dataPackage.
limit != 0) {
371 workload.relativePath +=
"?limit=" + std::to_string(dataPackage.
limit);
373 workload.relativePath +=
"&limit=1";
376 workload.callStack =
"Messages::getMessagesAsync()";
377 MessageVector returnValue{};
378 co_return Messages::httpsClient->submitWorkloadAndGetResult<MessageVector>(workload, &returnValue);
382 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Message };
383 co_await NewThreadAwaitable<Message>();
384 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
385 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/messages/" + dataPackage.
id;
386 workload.callStack =
"Messages::getMessageAsync()";
388 co_return Messages::httpsClient->submitWorkloadAndGetResult<
Message>(workload, &returnValue);
392 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Post_Message };
393 co_await NewThreadAwaitable<Message>();
394 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
395 workload.relativePath =
"/channels/" + dataPackage.channelId +
"/messages";
396 if (dataPackage.files.size() > 0) {
397 workload.payloadType = DiscordCoreInternal::PayloadType::Multipart_Form;
398 auto serializer = dataPackage.operator Serializer();
399 serializer.refreshString(JsonifierSerializeType::Json);
400 workload.content = constructMultiPartData(serializer.operator std::string(), dataPackage.files);
402 auto serializer = dataPackage.operator Serializer();
403 serializer.refreshString(JsonifierSerializeType::Json);
404 workload.content = serializer.operator std::string();
406 workload.callStack =
"Messages::createMessageAsync()";
408 co_return Messages::httpsClient->submitWorkloadAndGetResult<
Message>(workload, &returnValue);
412 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Crosspost_Message };
413 co_await NewThreadAwaitable<Message>();
414 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
415 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/messages/" + dataPackage.
messageId +
"/crosspost";
416 workload.callStack =
"Messages::crosspostMessageAsync()";
418 co_return Messages::httpsClient->submitWorkloadAndGetResult<
Message>(workload, &returnValue);
422 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Patch_Message };
423 co_await NewThreadAwaitable<Message>();
424 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Patch;
425 workload.relativePath =
"/channels/" + dataPackage.channelId +
"/messages/" + dataPackage.messageId;
426 if (dataPackage.files.size() > 0) {
427 workload.payloadType = DiscordCoreInternal::PayloadType::Multipart_Form;
428 auto serializer = dataPackage.operator Serializer();
429 serializer.refreshString(JsonifierSerializeType::Json);
430 workload.content = constructMultiPartData(serializer.operator std::string(), dataPackage.files);
432 auto serializer = dataPackage.operator Serializer();
433 serializer.refreshString(JsonifierSerializeType::Json);
434 workload.content = serializer.operator std::string();
436 workload.callStack =
"Messages::editMessageAsync()";
438 co_return Messages::httpsClient->submitWorkloadAndGetResult<
Message>(workload, &returnValue);
442 DiscordCoreInternal::HttpsWorkloadData workload{};
443 bool hasTimeElapsedNew = dataPackage.
timeStamp.hasTimeElapsed(14, 0, 0);
444 if (!hasTimeElapsedNew) {
445 workload = DiscordCoreInternal::HttpsWorkloadType::Delete_Message;
447 workload = DiscordCoreInternal::HttpsWorkloadType::Delete_Message_Old;
449 co_await NewThreadAwaitable<void>();
451 std::this_thread::sleep_for(Milliseconds{ dataPackage.
timeDelay });
453 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
454 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/messages/" + dataPackage.
messageId;
455 workload.callStack =
"Messages::deleteMessageAsync()";
456 if (dataPackage.
reason !=
"") {
457 workload.headersToInsert[
"X-Audit-Log-Reason"] = dataPackage.
reason;
459 Messages::httpsClient->submitWorkloadAndGetResult<
void>(workload);
464 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Bulk_Delete_Messages };
465 co_await NewThreadAwaitable<void>();
466 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
467 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/messages/bulk-delete";
468 auto serializer = dataPackage.operator Serializer();
469 serializer.refreshString(JsonifierSerializeType::Json);
470 workload.content = serializer.operator std::string();
471 workload.callStack =
"Messages::deleteMessagesBulkAsync()";
472 if (dataPackage.
reason !=
"") {
473 workload.headersToInsert[
"X-Audit-Log-Reason"] = dataPackage.
reason;
475 co_return Messages::httpsClient->submitWorkloadAndGetResult<
void>(workload);
479 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Pinned_Messages };
480 co_await NewThreadAwaitable<std::vector<Message>>();
481 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
482 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/pins";
483 workload.callStack =
"Messages::getPinnedMessagesAsync()";
484 MessageVector returnValue{};
485 co_return Messages::httpsClient->submitWorkloadAndGetResult<MessageVector>(workload, &returnValue);
489 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Put_Pin_Message };
490 co_await NewThreadAwaitable<void>();
491 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Put;
492 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/pins/" + dataPackage.
messageId;
493 workload.callStack =
"Messages::pinMessageAsync()";
494 if (dataPackage.
reason !=
"") {
495 workload.headersToInsert[
"X-Audit-Log-Reason"] = dataPackage.
reason;
497 co_return Messages::httpsClient->submitWorkloadAndGetResult<
void>(workload);
501 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_Pin_Message };
502 co_await NewThreadAwaitable<void>();
503 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
504 workload.relativePath =
"/channels/" + dataPackage.
channelId +
"/pins/" + dataPackage.
messageId;
505 workload.callStack =
"Messages::unpinMessageAsync()";
506 if (dataPackage.
reason !=
"") {
507 workload.headersToInsert[
"X-Audit-Log-Reason"] = dataPackage.
reason;
509 co_return Messages::httpsClient->submitWorkloadAndGetResult<
void>(workload);
512 DiscordCoreInternal::HttpsClient* Messages::httpsClient{
nullptr };
std::function< bool(OTy)> ObjectFilter
Typedef for the message filter.
MessageType
Message types.
The main namespace for this library.
A CoRoutine - representing a potentially asynchronous operation/function.
TimeStamp editedTimestamp
The time at which it was edited.
std::vector< std::string > mentionRoles
std::vector of "mention roles" ids.
std::vector< UserData > mentions
Array of User data, for individual's that were mentioned.
MessageType type
Message type.
bool mentionEveryone
Does the Message mention everyone?
Snowflake webHookId
WebHook id of the Message, if applicable.
MessageInteractionData interaction
Message Interaction data.
MessageReferenceData messageReference
Message reference data.
ChannelData thread
The Thread that the Message was sent in, if applicable.
TimeStamp timeStamp
The timeStamp of when the Message was created.
GuildMemberData member
The author's Guild member data.
std::string content
The Message's content.
Snowflake channelId
The Channel it was sent in.
Snowflake guildId
The id of the Guild the Message was sent in.
std::vector< StickerItemData > stickerItems
Array of Message Sticker item data.
std::vector< ChannelMentionData > mentionChannels
array of Channel mention data.
UserData author
The author's User data.
std::vector< ActionRowData > components
Array of action row data.
std::vector< AttachmentData > attachments
Array of attachment data.
std::vector< EmbedData > embeds
Array of Message embeds.
std::vector< StickerData > stickers
Array of Message Sticker data.
Snowflake applicationId
Application id.
bool tts
Is it a text-to-speech Message?
ApplicationData application
Application data.
MessageActivityData activity
Message activity data.
std::unique_ptr< MessageDataOld > referencedMessage
The referenced Message, to reply to.
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.
std::vector< MessageData > messages
A vector of collected Objects.
CoRoutine< MessageCollectorReturnData > collectMessages(int32_t quantityToCollect, int32_t msToCollectForNew, ObjectFilter< MessageData > filteringFunctionNew)
Begin waiting for Messages.
For getting a collection of Messages.
Snowflake channelId
Channel 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 Channel from which to collect the Message.
For crossposting a Message.
Snowflake messageId
Id of the message to be crossposted.
Snowflake channelId
Channel within which to crosspost the Message from.
int32_t timeDelay
Number of Milliseconds to wait before deleting the Message.
Snowflake messageId
The message Id of the Message to delete.
Snowflake channelId
The channel Id of the Message to delete.
TimeStamp timeStamp
The created-at timeStamp of the original message.
std::string reason
The reason for deleting the Message.
For deleting a bulk of Messages.
std::string reason
The reason for deleting the Messages.
Snowflake channelId
Channel within which to delete the Messages.
For getting a collection of pinned Messages.
Snowflake channelId
The Channel from which to collect pinned Messages.
For pinning a single Message.
Snowflake channelId
The Channel within which to pin the Message.
Snowflake messageId
The Message which you would like to pin.
std::string reason
Reason for pinning this Message.
For unpinning a single Message.
std::string reason
Reason for pinning this Message.
Snowflake messageId
The Message which you would like to unpin.
Snowflake channelId
The Channel within which to unpin the Message.
static CoRoutine< void > deleteMessagesBulkAsync(DeleteMessagesBulkData dataPackage)
Deletes a collection of Messages.
static CoRoutine< std::vector< Message > > getMessagesAsync(GetMessagesData dataPackage)
Collects a collection of Message from the Discord servers.
static CoRoutine< Message > createMessageAsync(CreateMessageData dataPackage)
Creates a new Message.
static CoRoutine< std::vector< Message > > getPinnedMessagesAsync(GetPinnedMessagesData dataPackage)
Collects a collection of pinned Messages from the Discord servers.
static CoRoutine< void > deleteMessageAsync(DeleteMessageData dataPackage)
Deletes a Message.
static CoRoutine< Message > 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< Message > crosspostMessageAsync(CrosspostMessageData dataPackage)
Crossposts a message from a News Channel to the following Channels.
static CoRoutine< void > unpinMessageAsync(UnpinMessageData dataPackage)
Unpins a Message from a given Channel.
static CoRoutine< Message > editMessageAsync(EditMessageData dataPackage)
Edit a Message.