DiscordCoreAPI
A Discord bot library written in C++, with custom asynchronous coroutines.
Loading...
Searching...
No Matches
ReactionEntities.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/// ReactionEntities.cpp - Source file for the reaction related classes and structs.
22/// May 13, 2021
23/// https://discordcoreapi.com
24/// \file ReactionEntities.cpp
25
29#include <fstream>
30
31namespace DiscordCoreAPI {
32
33 Reaction::Reaction(simdjson::ondemand::value jsonObjectData) {
34 this->count = getUint32(jsonObjectData, "count");
35
36 this->me = getBool(jsonObjectData, "me");
37
38 simdjson::ondemand::value object{};
39 if (getObject(object, "emoji", jsonObjectData)) {
40 this->emoji = EmojiData{ object };
41 }
42
43 this->guildId = getId(jsonObjectData, "guild_id");
44
45 this->channelId = getId(jsonObjectData, "channel_id");
46
47 this->userId = getId(jsonObjectData, "user_id");
48
49 this->messageId = getId(jsonObjectData, "message_id");
50
51 if (jsonObjectData["member"].get(object) == simdjson::error_code::SUCCESS) {
52 this->member = GuildMemberData{ object };
53 }
54 }
55
56 ReactionVector::ReactionVector(simdjson::ondemand::value jsonObjectData) {
57 if (jsonObjectData.type() != simdjson::ondemand::json_type::null) {
58 simdjson::ondemand::array arrayValue{};
59 if (getArray(arrayValue, jsonObjectData)) {
60 for (simdjson::simdjson_result<simdjson::ondemand::value> value: arrayValue) {
61 Reaction newData{ value.value() };
62 this->reactions.emplace_back(std::move(newData));
63 }
64 }
65 }
66 }
67
68 CreateGuildEmojiData::operator Jsonifier() {
69 Jsonifier data{};
70 for (auto& value: this->roles) {
71 data["roles"].emplaceBack(value.operator std::string());
72 }
73 data["images"] = this->imageDataFinal;
74 data["name"] = this->name;
75 return data;
76 }
77
78 ModifyGuildEmojiData::operator Jsonifier() {
79 Jsonifier data{};
80 for (auto& value: this->roles) {
81 data["roles"].emplaceBack(value.operator std::string());
82 }
83 data["name"] = this->name;
84 return data;
85 }
86
87 ReactionVector::operator std::vector<Reaction>() {
88 return this->reactions;
89 }
90
91 void Reactions::initialize(DiscordCoreInternal::HttpsClient* client) {
92 Reactions::httpsClient = client;
93 }
94
96 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Put_Reaction };
97 co_await NewThreadAwaitable<Reaction>();
98 std::string emoji;
99 if (dataPackage.emojiId != 0) {
100 emoji += ":" + dataPackage.emojiName + ":" + dataPackage.emojiId;
101 } else {
102 emoji = dataPackage.emojiName;
103 }
104 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Put;
105 workload.relativePath =
106 "/channels/" + dataPackage.channelId + "/messages/" + dataPackage.messageId + "/reactions/" + urlEncode(emoji) + "/@me";
107 workload.callStack = "Reactions::createReactionAsync()";
108 Reaction returnValue{};
109 co_return Reactions::httpsClient->submitWorkloadAndGetResult<Reaction>(workload, &returnValue);
110 }
111
113 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_Own_Reaction };
114 co_await NewThreadAwaitable<void>();
115 std::string emoji;
116 if (dataPackage.emojiId != 0) {
117 emoji += ":" + dataPackage.emojiName + ":" + dataPackage.emojiId;
118 } else {
119 emoji = dataPackage.emojiName;
120 }
121 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
122 workload.relativePath =
123 "/channels/" + dataPackage.channelId + "/messages/" + dataPackage.messageId + "/reactions/" + urlEncode(emoji) + "/@me";
124 workload.callStack = "Reactions::deleteOwnReactionAsync()";
125 co_return Reactions::httpsClient->submitWorkloadAndGetResult<void>(workload);
126 }
127
129 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_User_Reaction };
130 co_await NewThreadAwaitable<void>();
131 std::string emoji;
132 if (dataPackage.emojiId != 0) {
133 emoji += ":" + dataPackage.emojiName + ":" + dataPackage.emojiId;
134
135 } else {
136 emoji = dataPackage.emojiName;
137 }
138 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
139 workload.relativePath =
140 "/channels/" + dataPackage.channelId + "/messages/" + dataPackage.messageId + "/reactions/" + urlEncode(emoji) + "/" + dataPackage.userId;
141 workload.callStack = "Reactions::deleteUserReactionAsync()";
142 co_return Reactions::httpsClient->submitWorkloadAndGetResult<void>(workload);
143 }
144
146 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Reactions };
147 co_await NewThreadAwaitable<UserVector>();
148 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
149 workload.relativePath = "/channels/" + dataPackage.channelId + "/messages/" + dataPackage.messageId + "/reactions/" + dataPackage.emoji;
150 if (dataPackage.afterId != 0) {
151 workload.relativePath += "?after=" + dataPackage.afterId;
152 if (dataPackage.limit != 0) {
153 workload.relativePath += "&limit=" + std::to_string(dataPackage.limit);
154 }
155 } else if (dataPackage.limit != 0) {
156 workload.relativePath += "?limit=" + std::to_string(dataPackage.limit);
157 }
158 workload.callStack = "Reactions::getReactionsAsync()";
159 UserVector returnValue{};
160 co_return Reactions::httpsClient->submitWorkloadAndGetResult<UserVector>(workload, &returnValue);
161 }
162
163
165 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_All_Reactions };
166 co_await NewThreadAwaitable<void>();
167 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
168 workload.relativePath = "/channels/" + dataPackage.channelId + "/messages/" + dataPackage.messageId + "/reactions";
169 workload.callStack = "Reactions::deleteAllReactionsAsync()";
170 co_return Reactions::httpsClient->submitWorkloadAndGetResult<void>(workload);
171 }
172
174 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_Reactions_By_Emoji };
175 co_await NewThreadAwaitable<void>();
176 std::string emoji;
177 if (dataPackage.emojiId != 0) {
178 emoji += ":" + dataPackage.emojiName + ":" + dataPackage.emojiId;
179 } else {
180 emoji = dataPackage.emojiName;
181 }
182 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
183 workload.relativePath = "/channels/" + dataPackage.channelId + "/messages/" + dataPackage.messageId + "/reactions/" + urlEncode(emoji);
184 workload.callStack = "Reactions::deleteReactionsByEmojiAsync()";
185 co_return Reactions::httpsClient->submitWorkloadAndGetResult<void>(workload);
186 }
187
189 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Emoji_List };
190 co_await NewThreadAwaitable<EmojiDataVector>();
191 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
192 workload.relativePath = "/guilds/" + dataPackage.guildId + "/emojis";
193 workload.callStack = "Reactions::getEmojiListAsync()";
194 EmojiDataVector returnValue{};
195 co_return Reactions::httpsClient->submitWorkloadAndGetResult<EmojiDataVector>(workload, &returnValue);
196 }
197
199 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Get_Guild_Emoji };
200 co_await NewThreadAwaitable<EmojiData>();
201 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Get;
202 workload.relativePath = "/guilds/" + dataPackage.guildId + "/emojis/" + dataPackage.emojiId;
203 workload.callStack = "Reactions::getGuildEmojiAsync()";
204 EmojiData returnValue{};
205 co_return Reactions::httpsClient->submitWorkloadAndGetResult<EmojiData>(workload, &returnValue);
206 }
207
209 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Post_Guild_Emoji };
210 co_await NewThreadAwaitable<EmojiData>();
211 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Post;
212 std::ifstream fin(dataPackage.imageFilePath, std::ios::binary);
213 fin.seekg(0, std::ios::end);
214 std::string data{};
215 data.resize(fin.tellg());
216 fin.seekg(0, std::ios::beg);
217 fin.read(data.data(), data.size());
218 std::string newerFile = base64Encode(data);
219 switch (dataPackage.type) {
220 case ImageType::Jpg: {
221 dataPackage.imageDataFinal = "data:image/jpeg;base64,";
222 dataPackage.imageDataFinal.insert(dataPackage.imageDataFinal.end(), newerFile.begin(), newerFile.end());
223 break;
224 }
225 case ImageType::Png: {
226 dataPackage.imageDataFinal = "data:image/png;base64,";
227 dataPackage.imageDataFinal.insert(dataPackage.imageDataFinal.end(), newerFile.begin(), newerFile.end());
228 break;
229 }
230 case ImageType::Gif: {
231 dataPackage.imageDataFinal = "data:image/gif;base64,";
232 dataPackage.imageDataFinal.insert(dataPackage.imageDataFinal.end(), newerFile.begin(), newerFile.end());
233 break;
234 }
235 }
236 workload.relativePath = "/guilds/" + dataPackage.guildId + "/emojis";
237 auto serializer = dataPackage.operator Jsonifier();
238 serializer.refreshString(JsonifierSerializeType::Json);
239 workload.content = serializer.operator std::string();
240 workload.callStack = "Reactions::createGuildEmojiAsync()";
241 if (dataPackage.reason != "") {
242 workload.headersToInsert["X-Audit-Log-Reason"] = dataPackage.reason;
243 }
244 EmojiData returnValue{};
245 co_return Reactions::httpsClient->submitWorkloadAndGetResult<EmojiData>(workload, &returnValue);
246 }
247
249 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Patch_Guild_Emoji };
250 co_await NewThreadAwaitable<EmojiData>();
251 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Patch;
252 workload.relativePath = "/guilds/" + dataPackage.guildId + "/emojis/" + dataPackage.emojiId;
253 auto serializer = dataPackage.operator Jsonifier();
254 serializer.refreshString(JsonifierSerializeType::Json);
255 workload.content = serializer.operator std::string();
256 workload.callStack = "Reactions::modifyGuildEmojiAsync()";
257 if (dataPackage.reason != "") {
258 workload.headersToInsert["X-Audit-Log-Reason"] = dataPackage.reason;
259 }
260 EmojiData returnValue{};
261 co_return Reactions::httpsClient->submitWorkloadAndGetResult<EmojiData>(workload, &returnValue);
262 }
263
265 DiscordCoreInternal::HttpsWorkloadData workload{ DiscordCoreInternal::HttpsWorkloadType::Delete_Guild_Emoji };
266 co_await NewThreadAwaitable<void>();
267 workload.workloadClass = DiscordCoreInternal::HttpsWorkloadClass::Delete;
268 workload.relativePath = "/guilds/" + dataPackage.guildId + "/emojis/" + dataPackage.emojiId;
269 workload.callStack = "Reactions::deleteGuildEmojiAsync()";
270 if (dataPackage.reason != "") {
271 workload.headersToInsert["X-Audit-Log-Reason"] = dataPackage.reason;
272 }
273 co_return Reactions::httpsClient->submitWorkloadAndGetResult<void>(workload);
274 }
275 DiscordCoreInternal::HttpsClient* Reactions::httpsClient{ nullptr };
276}
The main namespace for this library.
A CoRoutine - representing a potentially asynchronous operation/function.
Definition: CoRoutine.hpp:52
Data structure representing a single emoji.
bool me
Whether or not I (The bot) placed it.
Snowflake messageId
The id of the Message upon which it was placed.
Snowflake guildId
The id of the Guild where it was placed.
EmojiData emoji
The emoji that was placed as a reaction.
Snowflake channelId
The id of the Channel where it was placed.
GuildMemberData member
The GuildMember who placed the reaction.
Snowflake userId
The id of the User who placed the reaction.
int32_t count
The number of times this particular emoji was placed as a reaction to the given Message.
For creating a single Reaction.
std::string emojiName
The emoji name of the Reaction to add.
Snowflake messageId
The Message on which to add the Reaction.
Snowflake emojiId
The emoji id of the Reaction to add.
Snowflake channelId
The Channel for which to add the Reaction.
For deleting one's own Reaction.
Snowflake channelId
The Channel from which to remove the Reaction.
Snowflake messageId
The Message from which to remove the Reaction.
std::string emojiName
The emoji name for which to remove the emoji.
Snowflake emojiId
The emoji id for which to remove the emoji.
For deleting a reaction, by User.
std::string emojiName
The name of which emoji to remove.
Snowflake channelId
The Channel from which to remove the Reaction.
Snowflake emojiId
The id of which emoji to remove.
Snowflake userId
The User id for whom to remove their emoji.
Snowflake messageId
The Message from which to remove the Reaction.
For getting all of the Reactions.
int32_t limit
The maximum number of reactors to collect.
Snowflake channelId
The Channel from which to acquire the reactors.
Snowflake afterId
Get users after this user ID.
std::string emoji
The emoji name for which to acquire the reactors.
Snowflake messageId
The Message from which to acquire the reactors.
For deleting all of the Reactions on a particular Message.
Snowflake messageId
The Message from which you would like to remove the emoji.
Snowflake channelId
The Channel from which you would like to remove the emoji.
For deleting all of the Reactions of a particular Emoji.
Snowflake messageId
The Message from which you would like to remove the emoji.
Snowflake channelId
The Channel from which you would like to remove the emoji.
Snowflake emojiId
The id of which emoji to remove.
std::string emojiName
The name of which emoji to remove.
For collecting a list of Emoji from a Guild.
Snowflake guildId
The id of the chosen Guild.
For collecting a single Guild Emoji.
Snowflake emojiId
The id of the chosen Emoji.
Snowflake guildId
The id of the chosen Guild.
For creating a new Guild Emoji.
std::string reason
Reason for creating the new Emoji.
Snowflake guildId
The Guild within which to create the Emoji.
ImageType type
The type of image being uploaded.
std::string imageFilePath
The image responseData.
For modifying a Guild Emoji.
Snowflake emojiId
The id of the Emoji to modify.
Snowflake guildId
The Guild within which to modify the Emoji.
std::string reason
Reason for modifying the Emoji.
std::string reason
Reason for deleting the Emoji.
Snowflake guildId
The Guild within which to delete the Emoji.
Snowflake emojiId
The id of the Emoji to delete.
static CoRoutine< UserVector > getReactionsAsync(GetReactionsData dataPackage)
Get a list of users that reacted with this emoji. Returns an array of user objects on success.
static CoRoutine< void > deleteOwnReactionAsync(DeleteOwnReactionData dataPackage)
Deletes your own Reactions from a given Message.
static CoRoutine< void > deleteGuildEmojiAsync(DeleteGuildEmojiData dataPackage)
Deletes a single Guild Emoji within a chosen Guild.
static CoRoutine< EmojiDataVector > getEmojiListAsync(GetEmojiListData dataPackage)
Collects a list of Guild Emoji from a chosen Guild.
static CoRoutine< Reaction > createReactionAsync(CreateReactionData dataPackage)
Creates a Reaction on a Message.
static CoRoutine< EmojiData > getGuildEmojiAsync(GetGuildEmojiData dataPackage)
Collects a single Guild Emoji from a chosen Guild.
static CoRoutine< void > deleteReactionsByEmojiAsync(DeleteReactionsByEmojiData dataPackage)
Deletes all of the Reactions by a specific emoji from a given Message.
static CoRoutine< void > deleteAllReactionsAsync(DeleteAllReactionsData dataPackage)
Deletes all of the Reactions from a given Message.
static CoRoutine< EmojiData > createGuildEmojiAsync(CreateGuildEmojiData dataPackage)
Creates a single Guild Emoji within a chosen Guild.
static CoRoutine< void > deleteUserReactionAsync(DeleteUserReactionData dataPackage)
Deletes all of the Reactions by a specific User from a given Message.
static CoRoutine< EmojiData > modifyGuildEmojiAsync(ModifyGuildEmojiData dataPackage)
Modifies a single Guild Emoji within a chosen Guild.