diff --git a/docpages/example_code/coro_simple_commands2.cpp b/docpages/example_code/coro_simple_commands2.cpp index 5858286eed..d0197a48ca 100644 --- a/docpages/example_code/coro_simple_commands2.cpp +++ b/docpages/example_code/coro_simple_commands2.cpp @@ -20,8 +20,9 @@ int main() { // If we have the guild member in the command's resolved data, return it const auto &member_map = event.command.resolved.members; - if (auto member = member_map.find(user_id); member != member_map.end()) + if (auto member = member_map.find(user_id); member != member_map.end()) { co_return member->second; + } // Try looking in guild cache dpp::guild *guild = dpp::find_guild(event.command.guild_id); if (guild) { diff --git a/docpages/example_code/join_voice.cpp b/docpages/example_code/join_voice.cpp index 266917afde..28d5103e18 100644 --- a/docpages/example_code/join_voice.cpp +++ b/docpages/example_code/join_voice.cpp @@ -46,7 +46,7 @@ int main(int argc, char const *argv[]) } /* If we need to join a vc at all, join it here if join_vc == true */ - if(join_vc) { + if (join_vc) { /* Attempt to connect to a voice channel, returns false if we fail to connect. */ /* The user issuing the command is not on any voice channel, we can't do anything */ diff --git a/docpages/example_code/oggopus.cpp b/docpages/example_code/oggopus.cpp index 6dd6ca4498..afcbb980ed 100644 --- a/docpages/example_code/oggopus.cpp +++ b/docpages/example_code/oggopus.cpp @@ -87,8 +87,9 @@ int main(int argc, char const *argv[]) const long read_bytes = oggz_read(track_og, CHUNK_READ); // break on eof - if (!read_bytes) + if (!read_bytes) { break; + } } // don't forget to free the memory diff --git a/src/dpp/channel.cpp b/src/dpp/channel.cpp index ddbdc35dd8..00a4db96a6 100644 --- a/src/dpp/channel.cpp +++ b/src/dpp/channel.cpp @@ -581,12 +581,14 @@ std::string channel::build_json(bool with_id) const { } permission channel::get_user_permissions(const user* user) const { - if (user == nullptr) + if (user == nullptr) { return 0; + } guild* g = dpp::find_guild(guild_id); - if (g == nullptr) + if (g == nullptr) { return 0; + } return g->permission_overwrites(g->base_permissions(user), user, this); } @@ -594,8 +596,9 @@ permission channel::get_user_permissions(const user* user) const { permission channel::get_user_permissions(const guild_member &member) const { guild* g = dpp::find_guild(guild_id); - if (g == nullptr) + if (g == nullptr) { return 0; + } return g->permission_overwrites(member, *this); } diff --git a/src/dpp/cluster.cpp b/src/dpp/cluster.cpp index d4a55c4532..89bb07f964 100644 --- a/src/dpp/cluster.cpp +++ b/src/dpp/cluster.cpp @@ -232,8 +232,9 @@ void cluster::start(bool return_after) { log(ll_debug, "Shards started."); - if (!return_after) + if (!return_after) { block_calling_thread(); + } } void cluster::shutdown() { diff --git a/src/dpp/discordvoiceclient.cpp b/src/dpp/discordvoiceclient.cpp index 4f2d3f1281..0dfeb03e5f 100644 --- a/src/dpp/discordvoiceclient.cpp +++ b/src/dpp/discordvoiceclient.cpp @@ -418,13 +418,13 @@ void discord_voice_client::thread_run() * this gives us time to see if it's an actual disconnect, or an error. * This will prevent us from looping too much, meaning error codes do not cause an infinite loop. */ - if(current_time - last_loop_time >= 3) + if (current_time - last_loop_time >= 3) times_looped = 0; /* This does mean we'll always have times_looped at a minimum of 1, this is intended. */ times_looped++; /* If we've looped 5 or more times, abort the loop. */ - if(times_looped >= 5) { + if (times_looped >= 5) { log(dpp::ll_warning, "Reached max loops whilst attempting to read from the websocket. Aborting websocket."); break; } @@ -488,10 +488,8 @@ bool discord_voice_client::handle_frame(const std::string &data) switch (op) { /* Client Disconnect */ - case 13: - { - if (j.find("d") != j.end() && j["d"].find("user_id") != j["d"].end() && !j["d"]["user_id"].is_null()) - { + case 13: { + if (j.find("d") != j.end() && j["d"].find("user_id") != j["d"].end() && !j["d"]["user_id"].is_null()) { snowflake u_id = snowflake_not_null(&j["d"], "user_id"); auto it = std::find_if(ssrc_map.begin(), ssrc_map.end(), [&u_id](const auto & p) { return p.second == u_id; }); @@ -500,8 +498,7 @@ bool discord_voice_client::handle_frame(const std::string &data) ssrc_map.erase(it); } - if (!creator->on_voice_client_disconnect.empty()) - { + if (!creator->on_voice_client_disconnect.empty()) { voice_client_disconnect_t vcd(nullptr, data); vcd.voice_client = this; vcd.user_id = u_id; @@ -513,12 +510,10 @@ bool discord_voice_client::handle_frame(const std::string &data) /* Speaking */ case 5: /* Client Connect (doesn't seem to work) */ - case 12: - { + case 12: { if (j.find("d") != j.end() && j["d"].find("user_id") != j["d"].end() && !j["d"]["user_id"].is_null() - && j["d"].find("ssrc") != j["d"].end() && !j["d"]["ssrc"].is_null() && j["d"]["ssrc"].is_number_integer()) - { + && j["d"].find("ssrc") != j["d"].end() && !j["d"]["ssrc"].is_null() && j["d"]["ssrc"].is_number_integer()) { uint32_t u_ssrc = j["d"]["ssrc"].get(); snowflake u_id = snowflake_not_null(&j["d"], "user_id"); ssrc_map[u_ssrc] = u_id; @@ -683,8 +678,9 @@ float discord_voice_client::get_secs_remaining() { std::lock_guard lock(this->stream_mutex); float ret = 0; - for (const auto& packet : outbuf) + for (const auto& packet : outbuf) { ret += packet.duration * (timescale / 1000000000.0f); + } return ret; } @@ -799,8 +795,7 @@ void discord_voice_client::read_ready() std::lock_guard lk(voice_courier_shared_state.mtx); auto& [range, payload_queue, pending_decoder_ctls, decoder] = voice_courier_shared_state.parked_voice_payloads[vp.vr->user_id]; - if (!decoder) - { + if (!decoder) { /* * Most likely this is the first time we encounter this speaker. * Do some initialization for not only the decoder but also the range. @@ -852,8 +847,9 @@ void discord_voice_client::write_ready() if (outbuf[0].packet.size() == 2 && (*((uint16_t*)(outbuf[0].packet.data()))) == AUDIO_TRACK_MARKER) { outbuf.erase(outbuf.begin()); track_marker_found = true; - if (tracks > 0) + if (tracks > 0) { tracks--; + } } if (outbuf.size()) { if (this->udp_send(outbuf[0].packet.data(), outbuf[0].packet.length()) == (int)outbuf[0].packet.length()) { @@ -890,9 +886,9 @@ void discord_voice_client::write_ready() sleep_time -= std::chrono::duration_cast(end_sleep - start_sleep); } while (std::chrono::nanoseconds(overshoot_accumulator.count() / samples_count) + sleep_increment < sleep_time); last_sleep_remainder = sleep_time; - } - else + } else { last_sleep_remainder = std::chrono::nanoseconds(0); + } } last_timestamp = std::chrono::high_resolution_clock::now(); @@ -1157,10 +1153,11 @@ discord_voice_client& discord_voice_client::insert_marker(const std::string& met uint32_t discord_voice_client::get_tracks_remaining() { std::lock_guard lock(this->stream_mutex); - if (outbuf.empty()) + if (outbuf.empty()) { return 0; - else + } else { return tracks + 1; + } } discord_voice_client& discord_voice_client::skip_to_next_marker() { @@ -1173,8 +1170,9 @@ discord_voice_client& discord_voice_client::skip_to_next_marker() { /* Remove the actual track marker out of the buffer */ outbuf.erase(outbuf.begin()); } - if (tracks > 0) + if (tracks > 0) { tracks--; + } if (!track_meta.empty()) { track_meta.erase(track_meta.begin()); } diff --git a/src/dpp/emoji.cpp b/src/dpp/emoji.cpp index 78f9a9dad5..f33a636ad2 100644 --- a/src/dpp/emoji.cpp +++ b/src/dpp/emoji.cpp @@ -41,14 +41,18 @@ emoji& emoji::fill_from_json(nlohmann::json* j) { json & user = (*j)["user"]; user_id = snowflake_not_null(&user, "id"); } - if (bool_not_null(j, "require_colons")) + if (bool_not_null(j, "require_colons")) { flags |= e_require_colons; - if (bool_not_null(j, "managed")) + } + if (bool_not_null(j, "managed")) { flags |= e_managed; - if (bool_not_null(j, "animated")) + } + if (bool_not_null(j, "animated")) { flags |= e_animated; - if (bool_not_null(j, "available")) + } + if (bool_not_null(j, "available")) { flags |= e_available; + } return *this; } diff --git a/src/dpp/etf.cpp b/src/dpp/etf.cpp index 8b4ed91832..27b04c5070 100644 --- a/src/dpp/etf.cpp +++ b/src/dpp/etf.cpp @@ -574,7 +574,7 @@ json etf_parser::decode_export() { json etf_parser::inner_parse() { /* Decode one value into json from ETF */ - if(offset >= size) { + if (offset >= size) { throw dpp::parse_exception("Read past end of ETF buffer"); } diff --git a/src/dpp/events/voice_state_update.cpp b/src/dpp/events/voice_state_update.cpp index d869d23b15..a6545e19b2 100644 --- a/src/dpp/events/voice_state_update.cpp +++ b/src/dpp/events/voice_state_update.cpp @@ -70,8 +70,7 @@ void voice_state_update::handle(discord_client* client, json &j, const std::stri } } - if (vsu.state.user_id == client->creator->me.id) - { + if (vsu.state.user_id == client->creator->me.id) { if (vsu.state.channel_id.empty()) { /* Instruction to disconnect from vc */ client->disconnect_voice_internal(vsu.state.guild_id, false); diff --git a/src/dpp/guild.cpp b/src/dpp/guild.cpp index fe5d73fbbc..27ba737ecf 100644 --- a/src/dpp/guild.cpp +++ b/src/dpp/guild.cpp @@ -138,7 +138,7 @@ guild_member& guild_member::set_communication_disabled_until(const time_t disabl } bool guild_member::operator == (guild_member const& other_member) const { - if((this->user_id == other_member.user_id && this->user_id.empty()) || (this->guild_id == other_member.guild_id && this->guild_id.empty())) + if ((this->user_id == other_member.user_id && this->user_id.empty()) || (this->guild_id == other_member.guild_id && this->guild_id.empty())) return false; return this->user_id == other_member.user_id && this->guild_id == other_member.guild_id; } @@ -690,12 +690,14 @@ std::string guild_widget::build_json(bool with_id) const { permission guild::base_permissions(const user* user) const { - if (user == nullptr) + if (user == nullptr) { return 0; + } auto mi = members.find(user->id); - if (mi == members.end()) + if (mi == members.end()) { return 0; + } guild_member gm = mi->second; return base_permissions(gm); @@ -705,12 +707,14 @@ permission guild::base_permissions(const guild_member &member) const { /* this method is written with the help of discord's pseudocode available here https://discord.com/developers/docs/topics/permissions#permission-overwrites */ - if (owner_id == member.user_id) + if (owner_id == member.user_id) { return ~0; // return all permissions if it's the owner of the guild + } role* everyone = dpp::find_role(id); - if (everyone == nullptr) + if (everyone == nullptr) { return 0; + } permission permissions = everyone->permissions; @@ -721,21 +725,24 @@ permission guild::base_permissions(const guild_member &member) const { } } - if (permissions & p_administrator) + if (permissions & p_administrator) { return ~0; + } return permissions; } permission guild::permission_overwrites(const uint64_t base_permissions, const user* user, const channel* channel) const { - if (user == nullptr || channel == nullptr) + if (user == nullptr || channel == nullptr) { return 0; + } /* this method is written with the help of discord's pseudocode available here https://discord.com/developers/docs/topics/permissions#permission-overwrites */ // ADMINISTRATOR overrides any potential permission overwrites, so there is nothing to do here. - if (base_permissions & p_administrator) + if (base_permissions & p_administrator) { return ~0; + } permission permissions = base_permissions; @@ -749,8 +756,9 @@ permission guild::permission_overwrites(const uint64_t base_permissions, const u } auto mi = members.find(user->id); - if (mi == members.end()) + if (mi == members.end()) { return 0; + } guild_member gm = mi->second; // Apply role specific overwrites. @@ -760,8 +768,9 @@ permission guild::permission_overwrites(const uint64_t base_permissions, const u for (auto& rid : gm.roles) { /* Skip \@everyone role to not break the hierarchy. It's calculated above */ - if (rid == this->id) + if (rid == this->id) { continue; + } for (auto it = channel->permission_overwrites.begin(); it != channel->permission_overwrites.end(); ++it) { if (rid == it->id && it->type == ot_role) { @@ -815,8 +824,9 @@ permission guild::permission_overwrites(const guild_member &member, const channe for (auto& rid : member.roles) { /* Skip \@everyone role to not break the hierarchy. It's calculated above */ - if (rid == this->id) + if (rid == this->id) { continue; + } for (auto it = channel.permission_overwrites.begin(); it != channel.permission_overwrites.end(); ++it) { if (rid == it->id && it->type == ot_role) { diff --git a/src/dpp/integration.cpp b/src/dpp/integration.cpp index eb394c8e6d..15da5f3305 100644 --- a/src/dpp/integration.cpp +++ b/src/dpp/integration.cpp @@ -57,16 +57,21 @@ integration& integration::fill_from_json(nlohmann::json* j) this->id = snowflake_not_null(j, "id"); this->name = string_not_null(j, "name"); this->type = type_map[string_not_null(j, "type")]; - if (bool_not_null(j, "enabled")) + if (bool_not_null(j, "enabled")) { this->flags |= if_enabled; - if (bool_not_null(j, "syncing")) + } + if (bool_not_null(j, "syncing")) { this->flags |= if_syncing; - if (bool_not_null(j, "enable_emoticons")) + } + if (bool_not_null(j, "enable_emoticons")) { this->flags |= if_emoticons; - if (bool_not_null(j, "revoked")) + } + if (bool_not_null(j, "revoked")) { this->flags |= if_revoked; - if (int8_not_null(j, "expire_behavior")) + } + if (int8_not_null(j, "expire_behavior")) { this->flags |= if_expire_kick; + } this->expire_grace_period = int32_not_null(j, "expire_grace_period"); if (j->contains("user")) { auto t = (*j)["user"]; diff --git a/src/dpp/invite.cpp b/src/dpp/invite.cpp index 24e2b41d78..a11f2e2417 100644 --- a/src/dpp/invite.cpp +++ b/src/dpp/invite.cpp @@ -72,14 +72,18 @@ std::string invite::build_json(bool with_id) const { json j; j["max_age"] = max_age; j["max_uses"] = max_uses; - if (target_user_id > 0) + if (target_user_id > 0) { j["target_user"] = target_user_id; - if (target_type != itt_none) + } + if (target_type != itt_none) { j["target_type"] = target_type; - if (temporary) + } + if (temporary) { j["temporary"] = temporary; - if (unique) + } + if (unique) { j["unique"] = unique; + } return j.dump(); } diff --git a/src/dpp/message.cpp b/src/dpp/message.cpp index 9e08c62d8d..b753c88696 100644 --- a/src/dpp/message.cpp +++ b/src/dpp/message.cpp @@ -123,7 +123,7 @@ component& component::set_type(component_type ct) } else if (type == cot_selectmenu) { label = dpp::utility::utf8substr(label, 0, 100); } - if(type == cot_text) { + if (type == cot_text) { placeholder = dpp::utility::utf8substr(placeholder, 0, 100); } else if (type == cot_selectmenu || type == cot_user_selectmenu || type == cot_role_selectmenu || type == cot_mentionable_selectmenu || type == cot_channel_selectmenu) { placeholder = dpp::utility::utf8substr(placeholder, 0, 150); @@ -412,7 +412,7 @@ select_option& select_option::fill_from_json(nlohmann::json* j) { } component& component::set_placeholder(const std::string &_placeholder) { - if(type == cot_text) { + if (type == cot_text) { placeholder = dpp::utility::utf8substr(_placeholder, 0, 100); } else if (type == cot_selectmenu || type == cot_user_selectmenu || type == cot_role_selectmenu || type == cot_mentionable_selectmenu || type == cot_channel_selectmenu) { placeholder = dpp::utility::utf8substr(_placeholder, 0, 150); @@ -834,7 +834,7 @@ std::string message::build_json(bool with_id, bool is_interaction_response) cons j["id"] = std::to_string(id); } - if(!author.username.empty()) { + if (!author.username.empty()) { /* Used for webhooks */ j["username"] = author.username; } @@ -907,12 +907,15 @@ std::string message::build_json(bool with_id, bool is_interaction_response) cons j["embeds"] = json::array(); for (auto& embed : embeds) { json e; - if (!embed.description.empty()) + if (!embed.description.empty()) { e["description"] = embed.description; - if (!embed.title.empty()) + } + if (!embed.title.empty()) { e["title"] = embed.title; - if (!embed.url.empty()) + } + if (!embed.url.empty()) { e["url"] = embed.url; + } e["color"] = embed.color; if (embed.footer.has_value()) { e["footer"]["text"] = embed.footer->text; diff --git a/src/dpp/presence.cpp b/src/dpp/presence.cpp index 5a74fc8b93..a2229687ce 100644 --- a/src/dpp/presence.cpp +++ b/src/dpp/presence.cpp @@ -78,7 +78,7 @@ presence::presence(presence_status status, activity_type type, const std::string a.name = activity_description; /* If the type is custom, set the state as "activity_description" */ - if(type == activity_type::at_custom) { + if (type == activity_type::at_custom) { a.state = activity_description; } @@ -86,24 +86,26 @@ presence::presence(presence_status status, activity_type type, const std::string activities.clear(); activities.emplace_back(a); flags &= PF_CLEAR_STATUS; - if (status == ps_online) + if (status == ps_online) { flags |= p_status_online; - else if (status == ps_idle) + } else if (status == ps_idle) { flags |= p_status_idle; - else if (status == ps_dnd) + } else if (status == ps_dnd) { flags |= p_status_dnd; + } } presence::presence(presence_status status, const activity &a) { activities.clear(); activities.emplace_back(a); flags &= PF_CLEAR_STATUS; - if (status == ps_online) + if (status == ps_online) { flags |= p_status_online; - else if (status == ps_idle) + } else if (status == ps_idle) { flags |= p_status_idle; - else if (status == ps_dnd) + } else if (status == ps_dnd) { flags |= p_status_dnd; + } } presence::~presence() = default; @@ -133,44 +135,48 @@ presence& presence::fill_from_json(nlohmann::json* j) { if (update_desktop) { flags &= PF_CLEAR_DESKTOP; - if (desktop == "online") + if (desktop == "online") { flags |= p_desktop_online; - else if (desktop == "idle") + } else if (desktop == "idle") { flags |= p_desktop_idle; - else if (desktop == "dnd") + } else if (desktop == "dnd") { flags |= p_desktop_dnd; + } } if (update_mobile) { flags &= PF_CLEAR_MOBILE; - if (mobile == "online") + if (mobile == "online") { flags |= p_mobile_online; - else if (mobile == "idle") + } else if (mobile == "idle") { flags |= p_mobile_idle; - else if (mobile == "dnd") + } else if (mobile == "dnd") { flags |= p_mobile_dnd; + } } if (update_web) { flags &= PF_CLEAR_WEB; - if (web == "online") + if (web == "online") { flags |= p_web_online; - else if (web == "idle") + } else if (web == "idle") { flags |= p_web_idle; - else if (web == "dnd") + } else if (web == "dnd") { flags |= p_web_dnd; + } } } if (j->contains("status")) { flags &= PF_CLEAR_STATUS; std::string main = string_not_null(j, "status"); - if (main == "online") + if (main == "online") { flags |= p_status_online; - else if (main == "idle") + } else if (main == "idle") { flags |= p_status_idle; - else if (main == "dnd") + } else if (main == "dnd") { flags |= p_status_dnd; + } } @@ -204,8 +210,9 @@ presence& presence::fill_from_json(nlohmann::json* j) { if (act.find("emoji") != act.end()) { a.emoji.name = string_not_null(&act["emoji"], "name"); a.emoji.id = snowflake_not_null(&act["emoji"], "id"); - if (bool_not_null(&act["emoji"], "animated")) + if (bool_not_null(&act["emoji"], "animated")) { a.emoji.flags |= e_animated; + } } if (act.find("party") != act.end()) { a.party.id = snowflake_not_null(&act["party"], "id"); @@ -264,7 +271,7 @@ std::string presence::build_json(bool with_id) const { }); if (!i.url.empty()) j2["url"] = i.url; - if(i.type == activity_type::at_custom) { + if (i.type == activity_type::at_custom) { if (!i.state.empty()) j2["state"] = i.state; /* When type is custom, bot needs to use "state" */ } else { if (!i.state.empty()) j2["details"] = i.state; /* Otherwise, the bot needs to use "details" */ diff --git a/src/dpp/queues.cpp b/src/dpp/queues.cpp index 4625020a46..ba230f1d4a 100644 --- a/src/dpp/queues.cpp +++ b/src/dpp/queues.cpp @@ -35,12 +35,15 @@ namespace dpp { http_request::http_request(const std::string &_endpoint, const std::string &_parameters, http_completion_event completion, const std::string &_postdata, http_method _method, const std::string &audit_reason, const std::string &filename, const std::string &filecontent, const std::string &filemimetype) : complete_handler(completion), completed(false), non_discord(false), endpoint(_endpoint), parameters(_parameters), postdata(_postdata), method(_method), reason(audit_reason), mimetype("application/json"), waiting(false) { - if (!filename.empty()) - file_name.push_back(filename); - if (!filecontent.empty()) - file_content.push_back(filecontent); - if (!filemimetype.empty()) + if (!filename.empty()) { + file_name.push_back(filename); + } + if (!filecontent.empty()) { + file_content.push_back(filecontent); + } + if (!filemimetype.empty()) { file_mimetypes.push_back(filemimetype); + } } http_request::http_request(const std::string &_endpoint, const std::string &_parameters, http_completion_event completion, const std::string &_postdata, http_method method, const std::string &audit_reason, const std::vector &filename, const std::vector &filecontent, const std::vector &filemimetypes) @@ -58,8 +61,9 @@ http_request::~http_request() = default; void http_request::complete(const http_request_completion_t &c) { /* Call completion handler only if the request has been completed */ - if (is_completed() && complete_handler) + if (is_completed() && complete_handler) { complete_handler(c); + } } /* Fill a http_request_completion_t from a HTTP result */ @@ -399,8 +403,9 @@ void in_thread::post_request(http_request* req) inline uint32_t hash(const char *s) { uint32_t hashval; - for (hashval = 17; *s != 0; s++) + for (hashval = 17; *s != 0; s++) { hashval = *s + 31 * hashval; + } return hashval; } diff --git a/src/dpp/slashcommand.cpp b/src/dpp/slashcommand.cpp index 923cf633fa..336e43e831 100644 --- a/src/dpp/slashcommand.cpp +++ b/src/dpp/slashcommand.cpp @@ -243,7 +243,7 @@ void to_json(json& j, const slashcommand& p) { } } - if(p.permissions.size()) { + if (p.permissions.size()) { j["permissions"] = json(); for(const auto& perm : p.permissions) { diff --git a/src/dpp/sslclient.cpp b/src/dpp/sslclient.cpp index a3dc1c40be..bf6906c7a2 100644 --- a/src/dpp/sslclient.cpp +++ b/src/dpp/sslclient.cpp @@ -322,20 +322,23 @@ void ssl_client::connect() /* Create SSL context */ openssl_context = SSL_CTX_new(method); - if (openssl_context == nullptr) + if (openssl_context == nullptr) { throw dpp::connection_exception("Failed to create SSL client context!"); + } /* Do not allow SSL 3.0, TLS 1.0 or 1.1 * https://www.packetlabs.net/posts/tls-1-1-no-longer-secure/ */ - if (!SSL_CTX_set_min_proto_version(openssl_context, TLS1_2_VERSION)) + if (!SSL_CTX_set_min_proto_version(openssl_context, TLS1_2_VERSION)) { throw dpp::connection_exception("Failed to set minimum SSL version!"); + } } /* Create SSL session */ ssl->ssl = SSL_new(openssl_context); - if (ssl->ssl == nullptr) + if (ssl->ssl == nullptr) { throw dpp::connection_exception("SSL_new failed!"); + } SSL_set_fd(ssl->ssl, (int)sfd); @@ -463,8 +466,9 @@ void ssl_client::read_loop() poll_time = poll_time > 400 ? 1000 : poll_time + poll_time / 3 + 1; r = poll(pfd, sockets, now / 1000 == (int64_t)last_tick ? poll_time : 0); - if (r == 0) + if (r == 0) { continue; + } if (custom_writeable_fd && custom_writeable_fd() >= 0 && pfd[1].revents & POLLOUT) { custom_writeable_ready(); @@ -477,7 +481,7 @@ void ssl_client::read_loop() } /* Now check if there's data to read */ - if(((pfd[0].revents & POLLIN) && !write_blocked_on_read) || (read_blocked_on_write && (pfd[0].revents & POLLOUT))) { + if (((pfd[0].revents & POLLIN) && !write_blocked_on_read) || (read_blocked_on_write && (pfd[0].revents & POLLOUT))) { if (plaintext) { read_blocked_on_write = false; read_blocked = false; @@ -511,17 +515,18 @@ void ssl_client::read_loop() const std::string http_reponse(data_lines[0]); /* Does the first line begin with a http code? */ - if(http_reponse.rfind("HTTP/1.1", 0) != std::string::npos) { + if (http_reponse.rfind("HTTP/1.1", 0) != std::string::npos) { /* Now let's split the first line by every space, meaning we can check the actual HTTP code. */ const std::vector line_split_by_space = utility::tokenize(data_lines[0], " "); /* We need to make sure there's at least 3 elements in line_split_by_space. */ - if(line_split_by_space.size() >= 3) { + if (line_split_by_space.size() >= 3) { const int http_code = std::stoi(line_split_by_space[1]); /* If the http_code isn't 204, 101, or 200, log it. */ - if(http_code != 204 && http_code != 101 && http_code != 200) + if (http_code != 204 && http_code != 101 && http_code != 200) { log(ll_warning, "Received unhandled code: " + http_reponse); + } } } diff --git a/src/dpp/utility.cpp b/src/dpp/utility.cpp index 04db275526..3c526df04c 100644 --- a/src/dpp/utility.cpp +++ b/src/dpp/utility.cpp @@ -210,8 +210,9 @@ namespace dpp { */ clean_hash = hash.substr(2); } - if (clean_hash.length() != 32) + if (clean_hash.length() != 32) { throw std::length_error("iconhash must be exactly 32 characters in length, passed value is: '" + clean_hash + "'"); + } this->first = from_string(clean_hash.substr(0, 16), std::hex); this->second = from_string(clean_hash.substr(16, 16), std::hex); } @@ -330,8 +331,9 @@ namespace dpp { { size_t i = 0, iBefore = 0, count = 0; const char* s = str.c_str(); - if (*s == 0) + if (*s == 0) { return 0; + } while (s[i] > 0) { ascii: @@ -373,29 +375,33 @@ namespace dpp { return str; } std::string::size_type i, ix, q, min = std::string::npos, max = std::string::npos; - for (q = 0, i = 0, ix = str.length(); i < ix; i++, q++) - { - if (q == start) + for (q = 0, i = 0, ix = str.length(); i < ix; i++, q++) { + if (q == start) { min = i; - if (q <= start + leng || leng == std::string::npos) + } + if (q <= start + leng || leng == std::string::npos) { max = i; + } unsigned char c = (unsigned char)str[i]; - if (c < 0x80) + if (c < 0x80) { i += 0; - else if ((c & 0xE0) == 0xC0) + } else if ((c & 0xE0) == 0xC0) { i += 1; - else if ((c & 0xF0) == 0xE0) + } else if ((c & 0xF0) == 0xE0) { i += 2; - else if ((c & 0xF8) == 0xF0) + } else if ((c & 0xF8) == 0xF0) { i += 3; - else + } else { return ""; //invalid utf8 + } } - if (q <= start + leng || leng == std::string::npos) + if (q <= start + leng || leng == std::string::npos) { max = i; - if (min == std::string::npos || max == std::string::npos) + } + if (min == std::string::npos || max == std::string::npos) { return ""; + } return str.substr(min, max); } diff --git a/src/dpp/voiceregion.cpp b/src/dpp/voiceregion.cpp index 25096d0f2a..b377fa4f83 100644 --- a/src/dpp/voiceregion.cpp +++ b/src/dpp/voiceregion.cpp @@ -34,14 +34,18 @@ voiceregion::voiceregion() : flags(0) voiceregion& voiceregion::fill_from_json(nlohmann::json* j) { id = string_not_null(j, "id"); name = string_not_null(j, "id"); - if (bool_not_null(j, "optimal")) + if (bool_not_null(j, "optimal")) { flags |= v_optimal; - if (bool_not_null(j, "deprecated")) + } + if (bool_not_null(j, "deprecated")) { flags |= v_deprecated; - if (bool_not_null(j, "custom")) + } + if (bool_not_null(j, "custom")) { flags |= v_custom; - if (bool_not_null(j, "vip")) + } + if (bool_not_null(j, "vip")) { flags |= v_vip; + } return *this; } diff --git a/src/dpp/voicestate.cpp b/src/dpp/voicestate.cpp index f4accf1e8b..f58d1f2968 100644 --- a/src/dpp/voicestate.cpp +++ b/src/dpp/voicestate.cpp @@ -38,20 +38,27 @@ voicestate& voicestate::fill_from_json(nlohmann::json* j) { session_id = string_not_null(j, "session_id"); request_to_speak = ts_not_null(j, "request_to_speak_timestamp"); flags = 0; - if (bool_not_null(j, "deaf")) + if (bool_not_null(j, "deaf")) { flags |= vs_deaf; - if (bool_not_null(j, "mute")) + } + if (bool_not_null(j, "mute")) { flags |= vs_mute; - if (bool_not_null(j, "self_mute")) + } + if (bool_not_null(j, "self_mute")) { flags |= vs_self_mute; - if (bool_not_null(j, "self_deaf")) + } + if (bool_not_null(j, "self_deaf")) { flags |= vs_self_deaf; - if (bool_not_null(j, "self_stream")) + } + if (bool_not_null(j, "self_stream")) { flags |= vs_self_stream; - if (bool_not_null(j, "self_video")) + } + if (bool_not_null(j, "self_video")) { flags |= vs_self_video; - if (bool_not_null(j, "suppress")) + } + if (bool_not_null(j, "suppress")) { flags |= vs_suppress; + } return *this; } diff --git a/src/dpp/webhook.cpp b/src/dpp/webhook.cpp index cb177e7add..f8cd64325c 100644 --- a/src/dpp/webhook.cpp +++ b/src/dpp/webhook.cpp @@ -84,16 +84,21 @@ std::string webhook::build_json(bool with_id) const { } j["name"] = name; j["type"] = type; - if (channel_id) + if (channel_id) { j["channel_id"] = channel_id; - if (guild_id) + } + if (guild_id) { j["guild_id"] = guild_id; - if (!name.empty()) + } + if (!name.empty()) { j["name"] = name; - if (image_data) + } + if (image_data) { j["avatar"] = *image_data; - if (application_id) + } + if (application_id) { j["application_id"] = application_id; + } return j.dump(); } diff --git a/src/dpp/wsclient.cpp b/src/dpp/wsclient.cpp index d23f6742af..f7b90ece5f 100644 --- a/src/dpp/wsclient.cpp +++ b/src/dpp/wsclient.cpp @@ -72,22 +72,18 @@ size_t websocket_client::fill_header(unsigned char* outbuf, size_t sendlength, w size_t pos = 0; outbuf[pos++] = WS_FINBIT | opcode; - if (sendlength <= WS_MAX_PAYLOAD_LENGTH_SMALL) - { + if (sendlength <= WS_MAX_PAYLOAD_LENGTH_SMALL) { outbuf[pos++] = (unsigned int)sendlength; - } - else if (sendlength <= WS_MAX_PAYLOAD_LENGTH_LARGE) - { + } else if (sendlength <= WS_MAX_PAYLOAD_LENGTH_LARGE) { outbuf[pos++] = WS_PAYLOAD_LENGTH_MAGIC_LARGE; outbuf[pos++] = (sendlength >> 8) & 0xff; outbuf[pos++] = sendlength & 0xff; - } - else - { + } else { outbuf[pos++] = WS_PAYLOAD_LENGTH_MAGIC_HUGE; const uint64_t len = sendlength; - for (int i = sizeof(uint64_t)-1; i >= 0; i--) + for (int i = sizeof(uint64_t)-1; i >= 0; i--) { outbuf[pos++] = ((len >> i*8) & 0xff); + } } /* Masking - We don't care about masking, but discord insists on it. We send a mask of 0x00000000 because @@ -174,14 +170,12 @@ bool websocket_client::parseheader(std::string &data) return false; } else { unsigned char opcode = data[0]; - switch (opcode & ~WS_FINBIT) - { + switch (opcode & ~WS_FINBIT) { case OP_CONTINUATION: case OP_TEXT: case OP_BINARY: case OP_PING: - case OP_PONG: - { + case OP_PONG: { unsigned char len1 = data[1]; unsigned int payloadstartoffset = 2; @@ -240,8 +234,7 @@ bool websocket_client::parseheader(std::string &data) } break; - case OP_CLOSE: - { + case OP_CLOSE: { uint16_t error = data[2] & 0xff; error <<= 8; error |= (data[3] & 0xff); @@ -250,8 +243,7 @@ bool websocket_client::parseheader(std::string &data) } break; - default: - { + default: { this->error(0); return false; } diff --git a/src/unittest/coro.cpp b/src/unittest/coro.cpp index 922e715c03..677e19d32f 100644 --- a/src/unittest/coro.cpp +++ b/src/unittest/coro.cpp @@ -84,10 +84,11 @@ struct job_awaitable { } catch (const test_exception<42> &) { /* intended exception to catch. we should have 2, one for the job_data test, one for the top-level job test */ int exceptions = ++exceptions_caught; - if (exceptions == 2) + if (exceptions == 2) { set_status(CORO_JOB_OFFLINE, ts_success); - else if (exceptions > 2) + } else if (exceptions > 2) { set_status(CORO_JOB_OFFLINE, ts_failed, "resume() threw more than expected"); + } } catch (const std::exception &) { /* anything else should not be caught */ set_status(CORO_JOB_OFFLINE, ts_failed, "resume() threw an exception it shouldn't have"); @@ -102,21 +103,25 @@ struct job_awaitable { }; dpp::job job_offline_test() { - if (int ret = co_await job_awaitable{42}; ret != 42) + if (int ret = co_await job_awaitable{42}; ret != 42) { set_status(CORO_JOB_OFFLINE, ts_failed, "failed simple awaitable"); + } std::array jobs; for (int i = 0; i < 10; ++i) { jobs[i] = [](int i) -> dpp::job { static std::atomic passed = false; - if (int ret = co_await job_awaitable{i}; ret != i) + if (int ret = co_await job_awaitable{i}; ret != i) { set_status(CORO_JOB_OFFLINE, ts_failed, "failed in-loop awaitable"); + } job_data[i] = i; for (int j = 0; j < 10; ++j) { - if (job_data[j] != j) + if (job_data[j] != j) { co_return; + } } - if (passed.exchange(true) == true) // another thread came here and already passed this check, skip the exception + if (passed.exchange(true) == true) { // another thread came here and already passed this check, skip the exception co_return; + } throw test_exception<42>(); // should be caught by simple_awaitable (since this is after resume) }(i); } @@ -139,21 +144,24 @@ dpp::task task_offline_test() { using clock = time::system_clock; static auto &test = CORO_TASK_OFFLINE; - if (int ret = co_await simple_awaitable{test, 42}; ret != 42) + if (int ret = co_await simple_awaitable{test, 42}; ret != 42) { set_status(test, ts_failed, "failed simple awaitable"); + } std::array, 10> tasks; auto start = clock::now(); for (int i = 0; i < 10; ++i) { tasks[i] = [](int i) -> dpp::task { - if (int ret = co_await simple_awaitable{test, i}; ret != i) + if (int ret = co_await simple_awaitable{test, i}; ret != i) { set_status(test, ts_failed, "failed in-loop awaitable"); + } job_data[i] = i; co_return i; }(i); } for (int i = 0; i < 10; ++i) { - if (co_await tasks[i] != i) + if (co_await tasks[i] != i) { set_status(test, ts_failed, "failed to co_await expected value"); + } } auto diff = clock::now() - start; if (diff > time::seconds(10)) { // task is async so 10 parallel tasks should all take 5 seconds + some overhead @@ -165,8 +173,9 @@ dpp::task task_offline_test() { dpp::task task2; try { task1 = []() -> dpp::task { // throws after co_await - if (int ret = co_await simple_awaitable{test, 69}; ret != 69) + if (int ret = co_await simple_awaitable{test, 69}; ret != 69) { set_status(test, ts_failed, "nested failed simple awaitable"); + } dpp::task task2 = []() -> dpp::task { co_await std_coroutine::suspend_never{}; // needed to make sure the task is initialized properly throw test_exception<1>{}; @@ -189,8 +198,9 @@ dpp::task task_offline_test() { } catch (const test_exception<1> &) { success = true; } - if (!success) + if (!success) { set_status(CORO_TASK_OFFLINE, ts_failed, "task failed to throw expected test_exception<1>"); + } success = false; // test throw without suspending try { @@ -199,8 +209,9 @@ dpp::task task_offline_test() { catch (const test_exception<2> &) { success = true; } - if (!success) + if (!success) { set_status(CORO_TASK_OFFLINE, ts_failed, "task failed to throw expected test_exception<2>"); + } // test cancel (throws dpp::task_cancelled_exception) auto task3 = []() -> dpp::task { auto make_task = []() -> dpp::task { @@ -215,8 +226,9 @@ dpp::task task_offline_test() { auto start_time = clock::now(); int exceptions = 0; std::generate(tasks.begin(), tasks.end(), make_task); - for (auto &task : tasks) + for (auto &task : tasks) { task.cancel(); + } for (auto &task : tasks) { try { co_await task; @@ -225,9 +237,9 @@ dpp::task task_offline_test() { ++exceptions; } } - if (exceptions < 3 || !(clock::now() - start_time < std::chrono::seconds(10))) + if (exceptions < 3 || !(clock::now() - start_time < std::chrono::seconds(10))) { set_status(CORO_TASK_OFFLINE, ts_failed, "failed to cancel 3 nested tasks in time"); - // } + } // test cancel and propagate { std::generate(tasks.begin(), tasks.end(), make_task); @@ -257,19 +269,24 @@ dpp::coroutine coroutine_offline_test() { int num = 0; auto factory = [&data](int &i) -> dpp::coroutine { - if (int ret = co_await simple_awaitable{test, 42}; ret != 42) + if (int ret = co_await simple_awaitable{test, 42}; ret != 42) { set_status(test, ts_failed, "failed simple awaitable"); + } data[i] = i; co_return i++; }; - if (int ret = co_await factory(num); ret != 0) + if (int ret = co_await factory(num); ret != 0) { set_status(test, ts_failed, "coroutine 1 to set expected values"); - if (int ret = co_await factory(num); ret != 1) + } + if (int ret = co_await factory(num); ret != 1) { set_status(test, ts_failed, "coroutine 2 to set expected values"); - if (int ret = co_await factory(num); ret != 2) + } + if (int ret = co_await factory(num); ret != 2) { set_status(test, ts_failed, "coroutine 3 to set expected values"); - if (data != std::to_array({0, 1, 2})) + } + if (data != std::to_array({0, 1, 2})) { set_status(test, ts_failed, "unexpected test data"); + } // verify that exceptions work as expected (dpp::coroutine throws uncaught exceptions to co_await-er) co_await []() -> dpp::coroutine { @@ -303,10 +320,11 @@ dpp::coroutine coroutine_offline_test() { } catch (const test_exception<2> &) { success2 = true; } - if (success1 && success2) + if (success1 && success2) { throw test_exception<0>{}; - else + } else { set_status(test, ts_failed, "failed to throw at co_await"); + } }(); // test_exception<0> escapes } @@ -319,8 +337,9 @@ dpp::job async_test() { std::generate(arr.begin(), arr.end(), [] { return dpp::async{&sync_awaitable_fun}; }); for (auto &async : arr) { - if (int ret = co_await async; ret != 42) + if (int ret = co_await async; ret != 42) { set_status(test, ts_failed, "failed to sync resume with expected value"); + } } bool success = false; try { @@ -328,13 +347,15 @@ dpp::job async_test() { } catch (const test_exception<0> &) { success = true; } - if (!success) + if (!success) { set_status(test, ts_failed, "failed to propagate exception"); + } auto now = clock::now(); std::generate(arr.begin(), arr.end(), [] { return dpp::async{&async_awaitable_wait5}; }); for (auto &async : arr) { - if (int ret = co_await async; ret != 69) + if (int ret = co_await async; ret != 69) { set_status(test, ts_failed, "failed to async resume with expected value"); + } } auto diff = clock::now() - now; if (diff > time::seconds(10)) { @@ -428,14 +449,17 @@ void event_handler_test(dpp::cluster *bot) { std::string content = "coro " + std::to_string(i); dpp::confirmation_callback_t result = co_await bot->co_message_create(dpp::message{channel, content}); - if (result.is_error()) + if (result.is_error()) { co_return {}; + } dpp::message msg = std::get(std::move(result).value); - if (msg.content != content) + if (msg.content != content) { co_return {}; + } user_member pair = co_await get_member_task; - if (!pair.first.has_value()) + if (!pair.first.has_value()) { co_return {}; + } const std::string& member_nick = pair.second.has_value() ? pair.second->nickname : ""; const std::string& user_nick = pair.first->username; result = co_await bot->co_message_edit(msg.set_content("coro " + (member_nick.empty() ? user_nick : member_nick) + " " + std::to_string(i))); @@ -451,9 +475,9 @@ void event_handler_test(dpp::cluster *bot) { if (msg_ids[i] == dpp::snowflake{}) { set_status(CORO_MUMBO_JUMBO, ts_failed, "failed to post message"); reacts[i] = dpp::async{[](auto &&cc) { cc(dpp::confirmation_callback_t{}); }}; - } - else + } else { reacts[i] = bot->co_message_add_reaction(msg_ids[i], event.msg.channel_id, "✅"); + } } catch (const std::exception &e) { set_status(CORO_MUMBO_JUMBO, ts_failed, "message task threw " + std::string{e.what()}); reacts[i] = dpp::async{[](auto &&cc) { cc(dpp::confirmation_callback_t{}); }}; diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index f3adb662f9..0789f2e2cb 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -34,8 +34,9 @@ int main(int argc, char *argv[]) if (offline) { std::cout << "Running offline unit tests only.\n"; } else { - if (argc > 1 && std::find_if(argv + 1, argv + argc, [](const char *a){ return (std::strcmp(a, "full") == 0); }) != argv + argc) + if (argc > 1 && std::find_if(argv + 1, argv + argc, [](const char *a){ return (std::strcmp(a, "full") == 0); }) != argv + argc) { extended = true; + } std::cout << "Running offline and " << (extended ? "extended" : "limited") << " online unit tests. Guild ID: " << TEST_GUILD_ID << " Text Channel ID: " << TEST_TEXT_CHANNEL_ID << " VC ID: " << TEST_VC_ID << " User ID: " << TEST_USER_ID << " Event ID: " << TEST_EVENT_ID << "\n"; } @@ -1056,8 +1057,9 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b } events_tested_mask |= flag; for (uint32_t i = 1; i < EVENT_END; i <<= 1) { - if ((events_to_test_mask & i) && (events_tested_mask & i) != i) + if ((events_to_test_mask & i) && (events_tested_mask & i) != i) { return; + } } set_events_tested(); } @@ -1066,8 +1068,9 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b { events_tested_mask |= ~events_to_test_mask; for (uint32_t i = 1; i < EVENT_END; i <<= 1) { - if ((events_tested_mask & i) != i) + if ((events_tested_mask & i) != i) { return; + } } set_events_tested(); } @@ -1508,7 +1511,9 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b invite.max_uses = 100; set_test(INVITE_CREATE_EVENT, false); bot.channel_invite_create(channel, invite, [&bot, invite](const dpp::confirmation_callback_t &event) { - if (event.is_error()) return; + if (event.is_error()) { + return; + } auto created = event.get(); if (!created.code.empty() && created.channel_id == TEST_TEXT_CHANNEL_ID && created.guild_id == TEST_GUILD_ID && created.inviter.id == bot.me.id) { @@ -1519,13 +1524,13 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b if (!event.is_error()) { auto retrieved = event.get(); if (retrieved.code == created.code && retrieved.guild_id == created.guild_id && retrieved.channel_id == created.channel_id && retrieved.inviter.id == created.inviter.id) { - if (retrieved.destination_guild.flags & dpp::g_community) + if (retrieved.destination_guild.flags & dpp::g_community) { set_test(INVITE_GET, retrieved.expires_at == 0); - else + } else { set_test(INVITE_GET, true); + } - } - else { + } else { set_test(INVITE_GET, false); } } else { diff --git a/src/unittest/unittest.cpp b/src/unittest/unittest.cpp index 37408448a9..bd7c682f49 100644 --- a/src/unittest/unittest.cpp +++ b/src/unittest/unittest.cpp @@ -41,10 +41,12 @@ void set_status(test_t &test, test_status_t newstatus, std::string_view message) static std::mutex m; std::scoped_lock lock{m}; - if (is_skipped(test) || newstatus == test.status) + if (is_skipped(test) || newstatus == test.status) { return; - if (test.status != ts_failed) // disallow changing the state of a failed test, but we still log + } + if (test.status != ts_failed) { // disallow changing the state of a failed test, but we still log test.status = newstatus; + } switch (newstatus) { case ts_started: std::cout << "[" << std::fixed << std::setprecision(3) << get_time() << "]: " << "[\u001b[33mTESTING\u001b[0m] " << test.description << (message.empty() ? "" : " - " + std::string{message} ) << "\n"; @@ -198,7 +200,8 @@ void wait_for_tests() { ticks++; } for (auto t : tests) { - if (t->status == ts_started) + if (t->status == ts_started) { std::cout << "[" << std::fixed << std::setprecision(3) << get_time() << "]: " << "[\u001b[31mTIMEOUT\u001b[0m] " << t->description << "\n"; + } } }