48 update_presence_data::update_presence_data(presence_update_state state) {
51 case presence_update_state::online: {
52 statusReal =
"online";
55 case presence_update_state::Do_Not_Disturb: {
59 case presence_update_state::idle: {
63 case presence_update_state::invisible: {
64 statusReal =
"invisible";
67 case presence_update_state::offline: {
68 statusReal =
"offline";
74 std::basic_ostream<char>& operator<<(std::basic_ostream<char>& outputSttream, jsonifier::string_view (*function)(
void)) {
75 outputSttream << function();
83 bool config_manager::doWePrintWebSocketSuccessMessages()
const {
84 return config.logOptions.logWebSocketSuccessMessages;
87 bool config_manager::doWePrintWebSocketErrorMessages()
const {
88 return config.logOptions.logWebSocketErrorMessages;
91 bool config_manager::doWePrintHttpsSuccessMessages()
const {
92 return config.logOptions.logHttpsSuccessMessages;
95 bool config_manager::doWePrintHttpsErrorMessages()
const {
96 return config.logOptions.logHttpsErrorMessages;
99 bool config_manager::doWePrintGeneralSuccessMessages()
const {
100 return config.logOptions.logGeneralSuccessMessages;
103 bool config_manager::doWePrintGeneralErrorMessages()
const {
104 return config.logOptions.logGeneralErrorMessages;
107 bool config_manager::doWeCacheGuildMembers()
const {
108 return config.cacheOptions.cacheGuildMembers;
111 bool config_manager::doWeCacheChannels()
const {
112 return config.cacheOptions.cacheChannels;
115 bool config_manager::doWeCacheUsers()
const {
116 return config.cacheOptions.cacheUsers;
119 bool config_manager::doWeCacheVoiceStates()
const {
120 return config.cacheOptions.cacheVoiceStates;
123 bool config_manager::doWeCacheGuilds()
const {
124 return config.cacheOptions.cacheGuilds;
127 bool config_manager::doWeCacheRoles()
const {
128 return config.cacheOptions.cacheRoles;
132 return config.presenceData;
135 jsonifier::string config_manager::getBotToken()
const {
136 return config.botToken;
139 std::ostream* config_manager::getErrorStream()
const {
140 return config.logOptions.errorStream;
143 std::ostream* config_manager::getOutputStream()
const {
144 return config.logOptions.outputStream;
147 uint64_t config_manager::getTotalShardCount()
const {
148 return config.shardOptions.totalNumberOfShards;
151 uint64_t config_manager::getStartingShard()
const {
152 return config.shardOptions.startingShard;
155 uint64_t config_manager::getShardCountForThisProcess()
const {
156 return config.shardOptions.numberOfShardsForThisProcess;
159 jsonifier::string config_manager::getConnectionAddress()
const {
160 return config.connectionAddress;
163 void config_manager::setConnectionAddress(jsonifier::string_view connectionAddressNew) {
164 config.connectionAddress = connectionAddressNew;
167 uint16_t config_manager::getConnectionPort()
const {
168 return config.connectionPort;
171 void config_manager::setConnectionPort(
const uint16_t connectionPortNew) {
172 config.connectionPort = connectionPortNew;
175 jsonifier::vector<repeated_function_data> config_manager::getFunctionsToExecute()
const {
176 return config.functionsToExecute;
179 text_format config_manager::getTextFormat()
const {
180 return config.textFormat;
184 return config.intents;
191 audio_frame_data& audio_frame_data::operator+=(jsonifier::string_view_base<uint8_t> other) {
192 if (other.size() > 0) {
193 if (
data.size() < other.size()) {
194 data.resize(other.size());
196 std::memcpy(
data.data(), other.data(), other.size());
202 audio_frame_data& audio_frame_data::operator+=(jsonifier::vector<uint8_t> other) {
203 if (other.size() > 0) {
204 if (
data.size() < other.size()) {
205 data.resize(other.size());
207 std::memcpy(
data.data(), other.data(), other.size());
213 void audio_frame_data::clearData() {
219 color_value::color_value(uint32_t colorValue) {
223 color_value::color_value(jsonifier::string_view hexColorValue) {
224 jsonifier::string returnString{};
225 if (hexColorValue ==
"") {
226 returnString = jsonifier::string{
"fefefe" };
228 returnString = hexColorValue;
230 color =
static_cast<uint32_t
>(jsonifier::strToInt64<16>(returnString));
233 rgbcolor_value color_value::getRgbColorValue() {
234 uint8_t red =
static_cast<uint8_t
>(color >> 16);
235 uint8_t green =
static_cast<uint8_t
>(color >> 8);
236 uint8_t blue =
static_cast<uint8_t
>(color);
237 rgbcolor_value colorNew{};
238 colorNew.green = green;
239 colorNew.blue = blue;
244 hex_color_value color_value::getHexColorValue() {
245 std::stringstream
stream{};
246 stream << std::hex << color;
247 return jsonifier::string{
stream.str() };
250 uint32_t color_value::getIntColorValue() {
254 icon_hash& icon_hash::operator=(jsonifier::string_view
string) {
255 jsonifier::string newHash{
string };
256 if (newHash.empty() || newHash ==
"0") {
261 if (newHash.size() >= 32 && newHash.find(
"a_") != jsonifier::string::npos) {
262 newHash = newHash.substr(newHash.find(
"a_") + 2);
264 if (newHash.size() != 32) {
265 throw dca_exception{
"Sorry, but that is an incorrect icon_hash length, it must be 32 characters long." };
267 lowBits = fromString<uint64_t>(newHash.substr(0, 16), std::hex);
268 highBits = fromString<uint64_t>(newHash.substr(16, 16), std::hex);
272 icon_hash::icon_hash(jsonifier::string_view
string) {
276 icon_hash::operator jsonifier::string()
const {
277 if (highBits == 0 || lowBits == 0) {
280 return toHex(lowBits) + toHex(highBits);
284 jsonifier::string operator+(
const icon_hash& lhs, jsonifier::string_view rhs) {
285 jsonifier::string newString = lhs.operator jsonifier::string() += rhs;
289 bool icon_hash::operator==(
const icon_hash& rhs)
const {
290 return highBits == rhs.highBits && lowBits == rhs.lowBits;
293 bool icon_hash::operator==(jsonifier::string_view rhs)
const {
294 return operator jsonifier::string() == rhs;
298 jsonifier::string permissions_base<permissions>::computeOverwrites(jsonifier::string_view basePermissions,
const guild_member_data& guildMember,
const channel_data& channel) {
300 return getAllPermissions();
303 uint64_t permissions = jsonifier::strToUint64(basePermissions.data());
304 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
305 if (channel.permissionOverwrites.at(x).id == guildMember.guildId) {
306 permissions &= ~channel.permissionOverwrites.at(x).deny;
307 permissions |= channel.permissionOverwrites.at(x).allow;
311 jsonifier::vector<role_data> guildMemberRoles{};
312 for (
auto& value: guildMember.roles) {
313 guildMemberRoles.emplace_back(
roles::getCachedRole({ .guildId = guildMember.guildId, .roleId = value }));
317 for (
auto& value: guildMemberRoles) {
318 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
319 if (value.id == channel.permissionOverwrites.at(x).id) {
320 allow |= channel.permissionOverwrites.at(x).allow;
321 deny |= channel.permissionOverwrites.at(x).deny;
325 permissions &= ~deny;
326 permissions |= allow;
327 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
328 if (channel.permissionOverwrites.at(x).id == guildMember.user.id) {
329 permissions &= ~channel.permissionOverwrites.at(x).deny;
330 permissions |= channel.permissionOverwrites.at(x).allow;
334 return jsonifier::toString(permissions);
337 template<> jsonifier::string permissions_base<permissions>::computeBasePermissions(
const guild_member_data& guildMember) {
339 if (guild.ownerId == guildMember.user.id) {
340 return getAllPermissions();
342 jsonifier::vector<role_data> guildRoles{};
343 if (roles::doWeCacheRoles()) {
344 for (
auto& value: guild.roles) {
351 for (
auto& value: guildRoles) {
352 if (value.id == guild.id) {
353 roleEveryone = value;
356 uint64_t permissions{};
357 if (roleEveryone.permissions.operator std::basic_string_view<
char, std::char_traits<char>>() !=
"0") {
358 permissions = roleEveryone.permissions;
362 getRolesData.
guildId = guildMember.guildId;
363 jsonifier::vector<role_data> guildMemberRoles{};
364 if (roles::doWeCacheRoles()) {
365 for (
auto& value: guildMember.roles) {
371 for (
auto& value: guildMemberRoles) {
372 permissions |= value.permissions.operator uint64_t();
375 return getAllPermissions();
378 return jsonifier::toString(permissions);
381 template<> jsonifier::string permissions_base<permissions_parse>::computeOverwrites(jsonifier::string_view basePermissions,
const guild_member_data& guildMember,
384 return getAllPermissions();
387 uint64_t permissions = jsonifier::strToUint64(basePermissions.data());
388 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
389 if (channel.permissionOverwrites.at(x).id == guildMember.guildId) {
390 permissions &= ~channel.permissionOverwrites.at(x).deny;
391 permissions |= channel.permissionOverwrites.at(x).allow;
395 jsonifier::vector<role_data> guildMemberRoles{};
396 if (roles::doWeCacheRoles()) {
397 for (
auto& value: guildMember.roles) {
405 for (
auto& value: guildMemberRoles) {
406 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
407 if (value.id == channel.permissionOverwrites.at(x).id) {
408 allow |= channel.permissionOverwrites.at(x).allow;
409 deny |= channel.permissionOverwrites.at(x).deny;
413 permissions &= ~deny;
414 permissions |= allow;
415 for (uint64_t x = 0; x < channel.permissionOverwrites.size(); ++x) {
416 if (channel.permissionOverwrites.at(x).id == guildMember.user.id) {
417 permissions &= ~channel.permissionOverwrites.at(x).deny;
418 permissions |= channel.permissionOverwrites.at(x).allow;
422 return jsonifier::toString(permissions);
425 template<> jsonifier::string permissions_base<permissions_parse>::computeBasePermissions(
const guild_member_data& guildMember) {
427 if (guild.ownerId == guildMember.user.id) {
428 return getAllPermissions();
430 jsonifier::vector<role_data> guildRoles{};
431 for (
auto& value: guild.roles) {
432 guildRoles.emplace_back(value);
435 for (
auto& value: guildRoles) {
436 if (value.id == guild.id) {
437 roleEveryone = value;
440 uint64_t permissions{};
441 if (roleEveryone.permissions.operator std::basic_string_view<
char, std::char_traits<char>>() !=
"0") {
442 permissions = roleEveryone.permissions;
446 getRolesData.
guildId = guildMember.guildId;
447 jsonifier::vector<role_data> guildMemberRoles{};
448 for (
auto& value: guildMember.roles) {
449 guildMemberRoles.emplace_back(value);
451 for (
auto& value: guildMemberRoles) {
452 permissions |= value.permissions.operator uint64_t();
456 return getAllPermissions();
459 return jsonifier::toString(permissions);
462 jsonifier::string constructMultiPartData(jsonifier::string_view data,
const jsonifier::vector<file>& files) {
463 static constexpr jsonifier::string_view boundary{
"boundary25" };
464 static constexpr jsonifier::string_view partStart(
"--boundary25\r\nContent-type: application/octet-stream\r\nContent-disposition: form-data; ");
466 jsonifier::string content(
"--" + boundary);
468 content +=
"\r\nContent-type: application/json\r\nContent-disposition: form-data; "
469 "name=\"payload_json\"\r\n\r\n";
470 content += data +
"\r\n";
471 if (files.size() == 1) {
472 content += partStart +
"name=\"file\"; filename=\"" + files.at(0).fileName +
"\"" +
"\r\n\r\n";
473 content += files.at(0).data;
475 for (uint64_t x = 0; x < files.size(); ++x) {
476 content += partStart +
"name=\"files[" + jsonifier::toString(x) +
"]\"; filename=\"" + files.at(x).fileName +
"\"\r\n\r\n";
477 content += files.at(x).data;
481 content +=
"\r\n--" + boundary +
"--";
485 jsonifier::string convertToLowerCase(jsonifier::string_view stringToConvert) {
486 jsonifier::string newString;
487 for (
auto& value: stringToConvert) {
488 if (isupper(
static_cast<uint8_t
>(value))) {
489 newString +=
static_cast<char>(tolower(
static_cast<uint8_t
>(value)));
497 jsonifier::string base64Encode(jsonifier::string_view
string,
bool url) {
498 static constexpr const char* base64CharsArray[2] = {
"abcdefghijklmnopqrstuvwxyz"
499 "abcdefghijklmnopqrstuvwxyz"
503 "abcdefghijklmnopqrstuvwxyz"
504 "abcdefghijklmnopqrstuvwxyz"
508 uint64_t encodedLength = (
string.size() + 2) / 3 * 4;
510 char trailing_char = url ?
'.' :
'=';
512 const char* base64Chars = base64CharsArray[url];
514 jsonifier::string returnString{};
515 returnString.reserve(encodedLength);
516 stop_watch<milliseconds> stopWatch{ 1500ms };
519 while (pos <
string.size()) {
520 if (stopWatch.hasTimeElapsed()) {
523 returnString.emplace_back(base64Chars[(
string[
static_cast<uint64_t
>(pos + 0)] & 0xfc) >> 2]);
525 if (
static_cast<uint64_t
>(pos + 1) <
string.size()) {
526 returnString.emplace_back(base64Chars[((
string[
static_cast<uint64_t
>(pos + 0)] & 0x03) << 4) + ((
string[
static_cast<uint64_t
>(pos + 1)] & 0xf0) >> 4)]);
528 if (
static_cast<uint64_t
>(pos + 2) <
string.size()) {
529 returnString.emplace_back(base64Chars[((
string[
static_cast<uint64_t
>(pos + 1)] & 0x0f) << 2) + ((
string[
static_cast<uint64_t
>(pos + 2)] & 0xc0) >> 6)]);
530 returnString.emplace_back(base64Chars[
string[
static_cast<uint64_t
>(pos + 2)] & 0x3f]);
532 returnString.emplace_back(base64Chars[(
string[
static_cast<uint64_t
>(pos + 1)] & 0x0f) << 2]);
533 returnString.emplace_back(trailing_char);
536 returnString.emplace_back(base64Chars[(
string[
static_cast<uint64_t
>(pos + 0)] & 0x03) << 4]);
537 returnString.emplace_back(trailing_char);
538 returnString.emplace_back(trailing_char);
547 jsonifier::string loadFileContents(jsonifier::string_view filePath) {
548 std::ifstream
file(filePath.data(), std::ios::in | std::ios::binary);
549 std::ostringstream
stream{};
551 return jsonifier::string{
stream.str() };
554 jsonifier::string utf8MakeValid(jsonifier::string_view inputString) {
555 jsonifier::string returnString{};
556 for (
auto& value: inputString) {
557 if (
static_cast<uint8_t
>(value) >= 128 || value < 0) {
558 uint64_t difference = 0ULL - value;
559 if (value + difference ==
'\0') {
562 returnString.emplace_back(value +
static_cast<char>(difference));
565 returnString.emplace_back(value);
571 jsonifier::string urlEncode(jsonifier::string_view inputString) {
572 std::ostringstream escaped{};
576 for (jsonifier::string::const_iterator x = inputString.begin(), n = inputString.end(); x != n; ++x) {
577 jsonifier::string::value_type c = (*x);
579 if (isalnum(
static_cast<uint8_t
>(c)) || c ==
'-' || c ==
'_' || c ==
'.' || c ==
'~') {
584 escaped << std::uppercase;
585 escaped << '%' << std::setw(2) << uint64_t(static_cast<uint8_t>(c));
586 escaped << std::nouppercase;
588 return jsonifier::string{ escaped.str() };
591 void spinLock(uint64_t timeInNsToSpinLockFor) {
592 int64_t startTime = std::chrono::duration_cast<nanoseconds>(sys_clock::now().time_since_epoch()).count();
593 int64_t timePassed{};
594 while (timePassed <
static_cast<int64_t
>(timeInNsToSpinLockFor)) {
595 timePassed = std::chrono::duration_cast<nanoseconds>(sys_clock::now().time_since_epoch()).count() - startTime;
599 jsonifier::string generateBase64EncodedKey() {
600 jsonifier::string returnString{};
601 returnString.resize(16);
602 std::mt19937_64 randomEngine{
static_cast<uint64_t
>(sys_clock::now().time_since_epoch().count()) };
603 for (uint64_t x = 0; x < 16; ++x) {
604 returnString.at(x) =
static_cast<char>((
static_cast<double>(randomEngine()) /
static_cast<double>(randomEngine.max())) * 255.0f);
606 returnString = base64Encode(returnString,
false);
610 bool nanoSleep(int64_t ns) {
612 HANDLE timer = CreateWaitableTimerExW(
nullptr,
nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
613 LARGE_INTEGER largeInt{ .QuadPart = -ns / 100 };
618 if (!SetWaitableTimerEx(timer, &largeInt, 0,
nullptr,
nullptr,
nullptr, 0)) {
622 WaitForSingleObjectEx(timer, INFINITE,
false);
625 std::this_thread::sleep_for(nanoseconds{ ns });
631 std::time_t time = std::time({});
632 time = std::chrono::current_zone()->to_sys(std::chrono::local_time<std::chrono::seconds>(std::chrono::seconds{ time })).time_since_epoch().count();
633 char timeString[std::size(
"yyyy-mm-ddThh:mm:ss")];
634 tm timeNew{ getTime(time) };
635 std::strftime(std::data(timeString), std::size(timeString),
"%FT%T", &timeNew);
Data structure representing a single guild, for the purposes of populating the cache.
A discord guild. used to connect to/disconnect from voice.
Data structure representing a single guild_member_data.
snowflake guildId
The guild that this member belongs to.
static guild_cache_data getCachedGuild(get_guild_data dataPackage)
Collects a guild from the library's cache.
static co_routine< jsonifier::vector< role_data > > getGuildMemberRolesAsync(get_guild_member_roles_data dataPackage)
Collects the roles that a guild_member has.
static role_cache_data getCachedRole(get_role_data dataPackage)
Collects a given role from the library's cache.
static co_routine< jsonifier::vector< role_data > > getGuildRolesAsync(get_guild_roles_data dataPackage)
Collects the roles that a guild has.
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.
Represents a single frame of audio data.
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.
Configuration data for the library's main class, discord_core_client.
Data representing a file to be sent via multipart-form data.
For getting a chosen guild_member_data's roles.
guild_member_data guildMember
Which guild member to collect the roles from.
For updating a user's presence.
presence_update_state status
current status.