Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
byte-sized-emi committed Jan 3, 2024
1 parent dd90a70 commit ce9e3cd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 43 deletions.
24 changes: 17 additions & 7 deletions src/bot/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ pub async fn is_admin(ctx: Context<'_>) -> bool {
false
}

/// Returns None when there was an error,
/// Some(true) when the user is a bot admin and
/// Some(false) when the user is not a bot admin
pub async fn is_bot_admin(ctx: Context<'_>) -> Option<bool> {
/// Returns false in case of an error
pub async fn is_bot_admin(ctx: Context<'_>) -> bool {
let author_id = ctx.author().id.0;
let main_guild_id = env::MAIN_GUILD_ID.get().unwrap();

Expand All @@ -68,20 +66,32 @@ pub async fn is_bot_admin(ctx: Context<'_>) -> Option<bool> {
error = err.to_string(),
member_id = author_id,
"Could not get main guild member"
)).ok()?;
)).ok();

let main_guild_member = if let Some(main_guild_member) = main_guild_member {
main_guild_member
} else {
return false;
};


let main_guild_roles = GuildId(*main_guild_id).roles(ctx.http()).await
.map_err(|err| error!(error = err.to_string(), "Could not get main guild roles"))
.ok()?;
.ok();

let main_guild_roles = if let Some(main_guild_roles) = main_guild_roles {
main_guild_roles
} else {
return false;
};

let is_admin = main_guild_member.roles.iter().any(|role_id| {
main_guild_roles.get(role_id).map_or(false, |role| {
role.has_permission(Permissions::ADMINISTRATOR)
})
});

Some(is_admin)
is_admin
}

/// Checks if the author is the owner of the guild where the message was sent
Expand Down
40 changes: 16 additions & 24 deletions src/bot/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,13 @@ pub async fn ping(ctx: Context<'_>) -> Result<(), Error> {
#[poise::command(prefix_command)]
pub async fn logger_pipe(ctx: Context<'_>) -> Result<(), Error> {
// Check permissions
if !checks::is_owner(ctx).await && !checks::is_admin(ctx).await {
match checks::is_bot_admin(ctx).await {
None => {
ctx.say("Internal error, contact bot admins").await?;
return Ok(());
}
Some(false) => {
ctx.say("Missing permissions, requires admin permissions")
.await?;
return Ok(());
}
Some(true) => {} // user is bot admin
}
if !checks::is_owner(ctx).await
&& !checks::is_admin(ctx).await
&& !checks::is_bot_admin(ctx).await
{
ctx.say("Missing permissions, requires admin permissions")
.await?;
return Ok(());
}

let guild_id = if let Some(guild_id) = ctx.guild_id() {
Expand All @@ -57,7 +51,7 @@ pub async fn logger_pipe(ctx: Context<'_>) -> Result<(), Error> {
let db_guild = if let Some(db_guild) = mysql_lib::get_guild(db, guild_id).await {
db_guild
} else {
ctx.say("Needs to be executed in a already setup guild")
ctx.say("Needs to be executed in an already setup guild")
.await?;
return Ok(());
};
Expand All @@ -69,13 +63,12 @@ pub async fn logger_pipe(ctx: Context<'_>) -> Result<(), Error> {
// => deactivate logger pipe
info!(?guild_id, "Deactivating logger pipe");

ctx.say("Deactivating logger pipe in current channel")
.await?;

mysql_lib::update_logger_pipe_channel(db, guild_id, None).await;

if checks::sent_in_main_guild(ctx).await {
logging::remove_main_logging_channel();
} else {
logging::remove_per_server_logging(guild_id);
}
logging::remove_per_server_logging(guild_id);
} else {
// Logger pipe either not setup, or not the current channel
// => set current channel as logger pipe
Expand All @@ -84,13 +77,12 @@ pub async fn logger_pipe(ctx: Context<'_>) -> Result<(), Error> {
"Setting logger pipe"
);

ctx.say("Setting logger pipe to the current channel")
.await?;

mysql_lib::update_logger_pipe_channel(db, guild_id, Some(ctx.channel_id())).await;

if checks::sent_in_main_guild(ctx).await {
logging::add_main_logging_channel(ctx.channel_id());
} else {
logging::add_per_server_logging(guild_id, ctx.channel_id());
}
logging::add_per_server_logging(guild_id, ctx.channel_id());
}

Ok(())
Expand Down
14 changes: 2 additions & 12 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ pub async fn setup_discord_logging(framework: Arc<bot::Framework>, db: Pool<MySq
let http = &framework.client().cache_and_http.http;

// Setup main logging guild/channel
let main_guild = http
let main_guild_channels = http
.get_channels(*env::MAIN_GUILD_ID.get().unwrap())
.await
.expect("Could not get main guild");

let main_logging_channel = main_guild[0].id;
let main_logging_channel = main_guild_channels[0].id;

modify_discord_layer(|discord_layer| {
discord_layer.main_log_channel = NonZeroU64::new(main_logging_channel.0);
Expand Down Expand Up @@ -107,16 +107,6 @@ pub fn remove_per_server_logging(guild_id: GuildId) {
});
}

/// Panics if called before [`install_framework`]
pub fn add_main_logging_channel(log_channel_id: ChannelId) {
modify_discord_layer(|layer| layer.main_log_channel = NonZeroU64::new(log_channel_id.0));
}

/// Panics if called before [`install_framework`]
pub fn remove_main_logging_channel() {
modify_discord_layer(|layer| layer.main_log_channel = None);
}

fn modify_discord_layer(f: impl FnOnce(&mut DiscordTracingLayer)) {
let result = DISCORD_LAYER_CHANGE_HANDLE.get().unwrap().modify(f);

Expand Down

0 comments on commit ce9e3cd

Please sign in to comment.