48 thread_local jsonifier::jsonifier_core parser{};
50 update_presence_data::update_presence_data(presence_update_state state) {
53 case presence_update_state::online: {
54 statusReal =
"online";
57 case presence_update_state::Do_Not_Disturb: {
61 case presence_update_state::idle: {
65 case presence_update_state::invisible: {
66 statusReal =
"invisible";
69 case presence_update_state::offline: {
70 statusReal =
"offline";
76 std::basic_ostream<char>& operator<<(std::basic_ostream<char>& outputSttream, jsonifier::string_view (*function)(
void)) {
77 outputSttream << function();
81 config_manager::config_manager(
const discord_core_client_config& configNew) {
85 bool config_manager::doWePrintWebSocketSuccessMessages()
const {
86 return config.logOptions.logWebSocketSuccessMessages;
89 bool config_manager::doWePrintWebSocketErrorMessages()
const {
90 return config.logOptions.logWebSocketErrorMessages;
93 bool config_manager::doWePrintHttpsSuccessMessages()
const {
94 return config.logOptions.logHttpsSuccessMessages;
97 bool config_manager::doWePrintHttpsErrorMessages()
const {
98 return config.logOptions.logHttpsErrorMessages;
101 bool config_manager::doWePrintGeneralSuccessMessages()
const {
102 return config.logOptions.logGeneralSuccessMessages;
105 bool config_manager::doWePrintGeneralErrorMessages()
const {
106 return config.logOptions.logGeneralErrorMessages;
109 bool config_manager::doWeCacheGuildMembers()
const {
110 return config.cacheOptions.cacheGuildMembers;
113 bool config_manager::doWeCacheChannels()
const {
114 return config.cacheOptions.cacheChannels;
117 bool config_manager::doWeCacheUsers()
const {
118 return config.cacheOptions.cacheUsers;
121 bool config_manager::doWeCacheVoiceStates()
const {
122 return config.cacheOptions.cacheVoiceStates;
125 bool config_manager::doWeCacheGuilds()
const {
126 return config.cacheOptions.cacheGuilds;
129 bool config_manager::doWeCacheRoles()
const {
130 return config.cacheOptions.cacheRoles;
133 update_presence_data config_manager::getPresenceData()
const {
134 return config.presenceData;
137 jsonifier::string config_manager::getBotToken()
const {
138 return config.botToken;
141 std::ostream* config_manager::getErrorStream()
const {
142 return config.logOptions.errorStream;
145 std::ostream* config_manager::getOutputStream()
const {
146 return config.logOptions.outputStream;
149 uint64_t config_manager::getTotalShardCount()
const {
150 return config.shardOptions.totalNumberOfShards;
153 uint64_t config_manager::getStartingShard()
const {
154 return config.shardOptions.startingShard;
157 uint64_t config_manager::getShardCountForThisProcess()
const {
158 return config.shardOptions.numberOfShardsForThisProcess;
161 jsonifier::string config_manager::getConnectionAddress()
const {
162 return config.connectionAddress;
165 void config_manager::setConnectionAddress(jsonifier::string_view connectionAddressNew) {
166 config.connectionAddress = connectionAddressNew;
169 uint16_t config_manager::getConnectionPort()
const {
170 return config.connectionPort;
173 void config_manager::setConnectionPort(
const uint16_t connectionPortNew) {
174 config.connectionPort = connectionPortNew;
177 jsonifier::vector<repeated_function_data> config_manager::getFunctionsToExecute()
const {
178 return config.functionsToExecute;
181 text_format config_manager::getTextFormat()
const {
182 return config.textFormat;
186 return config.intents;
193 audio_frame_data& audio_frame_data::operator+=(jsonifier::string_view_base<uint8_t> other) {
194 if (other.size() > 0) {
195 if (
data.size() < other.size()) {
196 data.resize(other.size());
198 std::memcpy(
data.data(), other.data(), other.size());
204 audio_frame_data& audio_frame_data::operator+=(jsonifier::vector<uint8_t> other) {
205 if (other.size() > 0) {
206 if (
data.size() < other.size()) {
207 data.resize(other.size());
209 std::memcpy(
data.data(), other.data(), other.size());
215 void audio_frame_data::clearData() {
221 color_value::color_value(uint32_t colorValue) {
225 color_value::color_value(jsonifier::string_view hexColorValue) {
226 jsonifier::string returnString{};
227 if (hexColorValue ==
"") {
228 returnString = jsonifier::string{
"fefefe" };
230 returnString = hexColorValue;
232 color =
static_cast<uint32_t
>(jsonifier::strToInt64<16>(returnString));
235 rgbcolor_value color_value::getRgbColorValue() {
236 uint8_t red =
static_cast<uint8_t
>(color >> 16);
237 uint8_t green =
static_cast<uint8_t
>(color >> 8);
238 uint8_t blue =
static_cast<uint8_t
>(color);
239 rgbcolor_value colorNew{};
240 colorNew.green = green;
241 colorNew.blue = blue;
246 hex_color_value color_value::getHexColorValue() {
247 std::stringstream
stream{};
248 stream << std::hex << color;
249 return jsonifier::string{
stream.str() };
252 uint32_t color_value::getIntColorValue() {
256 icon_hash& icon_hash::operator=(jsonifier::string_view
string) {
257 jsonifier::string newHash{
string };
258 if (newHash.empty() || newHash ==
"0") {
263 if (newHash.size() >= 32 && newHash.find(
"a_") != jsonifier::string::npos) {
264 newHash = newHash.substr(newHash.find(
"a_") + 2);
266 if (newHash.size() != 32) {
267 throw dca_exception{
"Sorry, but that is an incorrect icon_hash length, it must be 32 characters long." };
269 lowBits = fromString<uint64_t>(newHash.substr(0, 16), std::hex);
270 highBits = fromString<uint64_t>(newHash.substr(16, 16), std::hex);
274 icon_hash::icon_hash(jsonifier::string_view
string) {
278 icon_hash::operator jsonifier::string()
const {
279 if (highBits == 0 || lowBits == 0) {
282 return toHex(lowBits) + toHex(highBits);
286 jsonifier::string operator+(
const icon_hash& lhs, jsonifier::string_view rhs) {
287 jsonifier::string newString = lhs.operator jsonifier::string() += rhs;
291 bool icon_hash::operator==(
const icon_hash& rhs)
const {
292 return highBits == rhs.highBits && lowBits == rhs.lowBits;
295 bool icon_hash::operator==(jsonifier::string_view rhs)
const {
296 return operator jsonifier::string() == rhs;
300 jsonifier::string permissions_base<permissions>::computeOverwrites(jsonifier::string_view basePermissions,
const guild_member_data& guildMember,
const channel_data& channel) {
302 return getAllPermissions();
305 uint64_t permissions = jsonifier::strToUint64(basePermissions.data());
306 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
307 if (channel.permissionOverwrites.at(x).id == guildMember.guildId) {
308 permissions &= ~channel.permissionOverwrites.at(x).deny;
309 permissions |= channel.permissionOverwrites.at(x).allow;
313 jsonifier::vector<role_data> guildMemberRoles{};
314 for (
auto& value: guildMember.roles) {
315 guildMemberRoles.emplace_back(
roles::getCachedRole({ .guildId = guildMember.guildId, .roleId = value }));
319 for (
auto& value: guildMemberRoles) {
320 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
321 if (value.id == channel.permissionOverwrites.at(x).id) {
322 allow |= channel.permissionOverwrites.at(x).allow;
323 deny |= channel.permissionOverwrites.at(x).deny;
327 permissions &= ~deny;
328 permissions |= allow;
329 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
330 if (channel.permissionOverwrites.at(x).id == guildMember.user.id) {
331 permissions &= ~channel.permissionOverwrites.at(x).deny;
332 permissions |= channel.permissionOverwrites.at(x).allow;
336 return jsonifier::toString(permissions);
339 template<> jsonifier::string permissions_base<permissions>::computeBasePermissions(
const guild_member_data& guildMember) {
341 if (guild.ownerId == guildMember.user.id) {
342 return getAllPermissions();
344 jsonifier::vector<role_data> guildRoles{};
345 if (roles::doWeCacheRoles()) {
346 for (
auto& value: guild.roles) {
353 for (
auto& value: guildRoles) {
354 if (value.id == guild.id) {
355 roleEveryone = value;
358 uint64_t permissions{};
359 if (roleEveryone.permissions.operator std::basic_string_view<
char, std::char_traits<char>>() !=
"0") {
360 permissions = roleEveryone.permissions;
362 get_guild_member_roles_data getRolesData{};
363 getRolesData.guildMember = guildMember;
364 getRolesData.guildId = guildMember.guildId;
365 jsonifier::vector<role_data> guildMemberRoles{};
366 if (roles::doWeCacheRoles()) {
367 for (
auto& value: guildMember.roles) {
373 for (
auto& value: guildMemberRoles) {
374 permissions |= value.permissions.operator uint64_t();
377 return getAllPermissions();
380 return jsonifier::toString(permissions);
383 template<> jsonifier::string permissions_base<permissions_parse>::computeOverwrites(jsonifier::string_view basePermissions,
const guild_member_data& guildMember,
384 const channel_data& channel) {
386 return getAllPermissions();
389 uint64_t permissions = jsonifier::strToUint64(basePermissions.data());
390 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
391 if (channel.permissionOverwrites.at(x).id == guildMember.guildId) {
392 permissions &= ~channel.permissionOverwrites.at(x).deny;
393 permissions |= channel.permissionOverwrites.at(x).allow;
397 jsonifier::vector<role_data> guildMemberRoles{};
398 if (roles::doWeCacheRoles()) {
399 for (
auto& value: guildMember.roles) {
407 for (
auto& value: guildMemberRoles) {
408 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
409 if (value.id == channel.permissionOverwrites.at(x).id) {
410 allow |= channel.permissionOverwrites.at(x).allow;
411 deny |= channel.permissionOverwrites.at(x).deny;
415 permissions &= ~deny;
416 permissions |= allow;
417 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
418 if (channel.permissionOverwrites.at(x).id == guildMember.user.id) {
419 permissions &= ~channel.permissionOverwrites.at(x).deny;
420 permissions |= channel.permissionOverwrites.at(x).allow;
424 return jsonifier::toString(permissions);
427 template<> jsonifier::string permissions_base<permissions_parse>::computeBasePermissions(
const guild_member_data& guildMember) {
429 if (guild.ownerId == guildMember.user.id) {
430 return getAllPermissions();
432 jsonifier::vector<role_data> guildRoles{};
433 for (
auto& value: guild.roles) {
434 guildRoles.emplace_back(value);
437 for (
auto& value: guildRoles) {
438 if (value.id == guild.id) {
439 roleEveryone = value;
442 uint64_t permissions{};
443 if (roleEveryone.permissions.operator std::basic_string_view<
char, std::char_traits<char>>() !=
"0") {
444 permissions = roleEveryone.permissions;
446 get_guild_member_roles_data getRolesData{};
447 getRolesData.guildMember = guildMember;
448 getRolesData.guildId = guildMember.guildId;
449 jsonifier::vector<role_data> guildMemberRoles{};
450 for (
auto& value: guildMember.roles) {
451 guildMemberRoles.emplace_back(value);
453 for (
auto& value: guildMemberRoles) {
454 permissions |= value.permissions.operator uint64_t();
458 return getAllPermissions();
461 return jsonifier::toString(permissions);
464 jsonifier::string constructMultiPartData(jsonifier::string_view data,
const jsonifier::vector<file>& files) {
465 const jsonifier::string boundary(
"boundary25");
466 const jsonifier::string partStart(
"--" + boundary +
"\r\nContent-type: application/octet-stream\r\nContent-disposition: form-data; ");
468 jsonifier::string content(
"--" + boundary);
470 content +=
"\r\nContent-type: application/json\r\nContent-disposition: form-data; "
471 "name=\"payload_json\"\r\n\r\n";
472 content += data +
"\r\n";
473 if (files.size() == 1) {
474 content += partStart +
"name=\"file\"; filename=\"" + files.at(0).fileName +
"\"" +
"\r\n\r\n";
475 content += files.at(0).data;
477 for (uint64_t x = 0; x < files.size(); ++x) {
478 content += partStart +
"name=\"files[" + jsonifier::toString(x) +
"]\"; filename=\"" + files.at(x).fileName +
"\"\r\n\r\n";
479 content += files.at(x).data;
483 content +=
"\r\n--" + boundary +
"--";
487 jsonifier::string convertToLowerCase(jsonifier::string_view stringToConvert) {
488 jsonifier::string newString;
489 for (
auto& value: stringToConvert) {
490 if (isupper(
static_cast<uint8_t
>(value))) {
491 newString +=
static_cast<char>(tolower(
static_cast<uint8_t
>(value)));
499 jsonifier::string base64Encode(jsonifier::string_view
string,
bool url) {
500 const char* base64CharsArray[2] = {
"abcdefghijklmnopqrstuvwxyz"
501 "abcdefghijklmnopqrstuvwxyz"
505 "abcdefghijklmnopqrstuvwxyz"
506 "abcdefghijklmnopqrstuvwxyz"
510 uint64_t encodedLength = (
string.size() + 2) / 3 * 4;
512 char trailing_char = url ?
'.' :
'=';
514 const char* base64Chars = base64CharsArray[url];
516 jsonifier::string returnString{};
517 returnString.reserve(encodedLength);
518 stop_watch<milliseconds> stopWatch{ 1500ms };
521 while (pos <
string.size()) {
522 if (stopWatch.hasTimeElapsed()) {
525 returnString.pushBack(base64Chars[(
string[
static_cast<uint64_t
>(pos + 0)] & 0xfc) >> 2]);
527 if (
static_cast<uint64_t
>(pos + 1) <
string.size()) {
528 returnString.pushBack(base64Chars[((
string[
static_cast<uint64_t
>(pos + 0)] & 0x03) << 4) + ((
string[
static_cast<uint64_t
>(pos + 1)] & 0xf0) >> 4)]);
530 if (
static_cast<uint64_t
>(pos + 2) <
string.size()) {
531 returnString.pushBack(base64Chars[((
string[
static_cast<uint64_t
>(pos + 1)] & 0x0f) << 2) + ((
string[
static_cast<uint64_t
>(pos + 2)] & 0xc0) >> 6)]);
532 returnString.pushBack(base64Chars[
string[
static_cast<uint64_t
>(pos + 2)] & 0x3f]);
534 returnString.pushBack(base64Chars[(
string[
static_cast<uint64_t
>(pos + 1)] & 0x0f) << 2]);
535 returnString.pushBack(trailing_char);
538 returnString.pushBack(base64Chars[(
string[
static_cast<uint64_t
>(pos + 0)] & 0x03) << 4]);
539 returnString.pushBack(trailing_char);
540 returnString.pushBack(trailing_char);
549 jsonifier::string loadFileContents(jsonifier::string_view filePath) {
550 std::ifstream file(filePath.data(), std::ios::in | std::ios::binary);
551 std::ostringstream
stream{};
553 return jsonifier::string{
stream.str() };
556 jsonifier::string utf8MakeValid(jsonifier::string_view inputString) {
557 jsonifier::string returnString{};
558 for (
auto& value: inputString) {
559 if (
static_cast<uint8_t
>(value) >= 128 || value < 0) {
560 uint64_t difference = 0ULL - value;
561 if (value + difference ==
'\0') {
564 returnString.pushBack(value +
static_cast<char>(difference));
567 returnString.pushBack(value);
573 jsonifier::string urlEncode(jsonifier::string_view inputString) {
574 std::ostringstream escaped{};
578 for (jsonifier::string::const_iterator x = inputString.begin(), n = inputString.end(); x != n; ++x) {
579 jsonifier::string::value_type c = (*x);
581 if (isalnum(
static_cast<uint8_t
>(c)) || c ==
'-' || c ==
'_' || c ==
'.' || c ==
'~') {
586 escaped << std::uppercase;
587 escaped << '%' << std::setw(2) << uint64_t(static_cast<uint8_t>(c));
588 escaped << std::nouppercase;
590 return jsonifier::string{ escaped.str() };
593 void spinLock(uint64_t timeInNsToSpinLockFor) {
594 int64_t startTime = std::chrono::duration_cast<nanoseconds>(hrclock::now().time_since_epoch()).count();
595 int64_t timePassed{};
596 while (timePassed <
static_cast<int64_t
>(timeInNsToSpinLockFor)) {
597 timePassed = std::chrono::duration_cast<nanoseconds>(hrclock::now().time_since_epoch()).count() - startTime;
601 jsonifier::string generateBase64EncodedKey() {
602 jsonifier::string returnString{};
603 returnString.resize(16);
604 std::mt19937_64 randomEngine{
static_cast<uint64_t
>(hrclock::now().time_since_epoch().count()) };
605 for (uint64_t x = 0; x < 16; ++x) {
606 returnString.at(x) =
static_cast<char>((
static_cast<double>(randomEngine()) /
static_cast<double>(randomEngine.max())) * 255.0f);
608 returnString = base64Encode(returnString,
false);
612 bool nanoSleep(int64_t ns) {
614 HANDLE timer = CreateWaitableTimerExW(
nullptr,
nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
615 LARGE_INTEGER largeInt{ .QuadPart = -ns / 100 };
620 if (!SetWaitableTimerEx(timer, &largeInt, 0,
nullptr,
nullptr,
nullptr, 0)) {
624 WaitForSingleObjectEx(timer, INFINITE,
false);
627 std::this_thread::sleep_for(nanoseconds{ ns });
633 std::time_t time = std::time({});
634 time = std::chrono::current_zone()->to_sys(std::chrono::local_time<std::chrono::seconds>(std::chrono::seconds{ time })).time_since_epoch().count();
635 char timeString[std::size(
"yyyy-mm-ddThh:mm:ss")];
636 std::strftime(std::data(timeString), std::size(timeString),
"%FT%T", gmtime(&time));
static guild_cache_data getCachedGuild(const get_guild_data dataPackage)
Collects a guild from the library's cache.
static co_routine< jsonifier::vector< role_data > > getGuildMemberRolesAsync(const get_guild_member_roles_data dataPackage)
Collects the roles that a guild_member has.
static co_routine< jsonifier::vector< role_data > > getGuildRolesAsync(const get_guild_roles_data dataPackage)
Collects the roles that a guild has.
static role_cache_data getCachedRole(const get_role_data dataPackage)
Collects a given role from the library's cache.
text_format
Represents which text format to use for websocket transfer.
gateway_intents
Gateway intents.
DiscordCoreAPI_Dll jsonifier::string getTimeAndDate()
Acquires a timeStamp with the current time and date - suitable for use in message-embeds.
@ administrator
Allows all permissions and bypasses channel permission overwrites.
@ stream
Allows the user to go live.
audio_frame_type
Audio frame types.
The main namespace for the forward-facing interfaces.
audio_frame_type type
The type of audio frame.
jsonifier::vector< uint8_t > data
The audio data.
int64_t currentSize
The current size of the allocated memory.
presence_update_state status
current status.