Skip to content

Commit

Permalink
style: make code style of if () and some other stuff i spotted while …
Browse files Browse the repository at this point in the history
…going through the code match code style, if () must always have braces even if its on the same line
  • Loading branch information
braindigitalis committed Sep 27, 2023
1 parent 90f63df commit 42e3329
Show file tree
Hide file tree
Showing 25 changed files with 277 additions and 185 deletions.
3 changes: 2 additions & 1 deletion docpages/example_code/coro_simple_commands2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion docpages/example_code/join_voice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 2 additions & 1 deletion docpages/example_code/oggopus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/dpp/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,21 +581,24 @@ 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);
}

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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/dpp/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
40 changes: 19 additions & 21 deletions src/dpp/discordvoiceclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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; });
Expand All @@ -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;
Expand All @@ -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<uint32_t>();
snowflake u_id = snowflake_not_null(&j["d"], "user_id");
ssrc_map[u_ssrc] = u_id;
Expand Down Expand Up @@ -683,8 +678,9 @@ float discord_voice_client::get_secs_remaining() {
std::lock_guard<std::mutex> 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;
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -890,9 +886,9 @@ void discord_voice_client::write_ready()
sleep_time -= std::chrono::duration_cast<std::chrono::nanoseconds>(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();
Expand Down Expand Up @@ -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<std::mutex> 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() {
Expand All @@ -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());
}
Expand Down
12 changes: 8 additions & 4 deletions src/dpp/emoji.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/dpp/etf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
3 changes: 1 addition & 2 deletions src/dpp/events/voice_state_update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 21 additions & 11 deletions src/dpp/guild.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;

Expand All @@ -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;

Expand All @@ -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.
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 10 additions & 5 deletions src/dpp/integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down
12 changes: 8 additions & 4 deletions src/dpp/invite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Loading

0 comments on commit 42e3329

Please sign in to comment.