Skip to content

Commit

Permalink
Fix the error handling of mute deletions
Browse files Browse the repository at this point in the history
When the bot starts up, it checks the database to see if a user needs to
be unmuted. However, with the introduction of the delete GC, this
caused the bot to error since it's no longer in the guild.

Fix this by checking the delete table before doing anything. If
the guild id isn't in the delete table, proceed with the mute deletion
otherwise skip.

However, if there's a rogue database value, the bot would fire an error
when trying to unwrap the cached guild. Fix this by adding a simple error
handler to let the dev know that there was a guild deletion error.

From there, the bad entry can be manually removed.

Resolves #14

Signed-off-by: kingbri <bdashore3@gmail.com>
  • Loading branch information
bdashore3 committed Oct 7, 2020
1 parent 3d8b896 commit b941cc8
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/commands/mutes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,16 @@ async fn create_mute_timer(ctx: Context, time: u64, user_id: UserId, guild_id: G
async fn unmute_by_time(ctx: &Context, user_id: &UserId, guild_id: &GuildId) -> CommandResult {
let pool = ctx.data.read().await
.get::<ConnectionPool>().cloned().unwrap();
let guild = ctx.cache.guild(guild_id).await.unwrap();

let guild = match ctx.cache.guild(guild_id).await {
Some(guild) => guild,
None => {
eprintln!("There was an error in finding guild {} from the cache!", guild_id.0);

return Ok(())
}
};

let mut member = guild.member(ctx, user_id).await?;

let mute_info = handle_mute_role(ctx, &guild, None).await?;
Expand Down Expand Up @@ -384,7 +393,8 @@ async fn new_mute_role(ctx: &Context, guild: &Guild, channel_id: ChannelId) -> R
}
}

sqlx::query!("UPDATE guild_info SET muted_role_id = $1, mute_channel_id = $2 WHERE guild_id = $3", mute_role.id.0 as i64, channel_id.0 as i64, guild.id.0 as i64)
sqlx::query!("UPDATE guild_info SET muted_role_id = $1, mute_channel_id = $2 WHERE guild_id = $3",
mute_role.id.0 as i64, channel_id.0 as i64, guild.id.0 as i64)
.execute(&pool).await?;

let mute_info = MuteInfo {
Expand Down Expand Up @@ -414,7 +424,13 @@ pub async fn load_mute_timers(ctx: &Context) -> CommandResult {
let user_id = UserId::from(i.user_id as u64);

if mute_time_diff <= 0 {
unmute_by_time(&ctx, &user_id, &guild_id).await?;
let check = sqlx::query!("SELECT EXISTS(SELECT 1 FROM delete_time_store WHERE guild_id = $1)",
i.guild_id)
.fetch_one(&pool).await?;

if !check.exists.unwrap() {
unmute_by_time(&ctx, &user_id, &guild_id).await?;
}
} else {
let ctx_clone = ctx.clone();

Expand Down

0 comments on commit b941cc8

Please sign in to comment.