diff options
Diffstat (limited to 'src/handler.rs')
-rw-r--r-- | src/handler.rs | 56 |
1 files changed, 43 insertions, 13 deletions
diff --git a/src/handler.rs b/src/handler.rs index ef73f36..00cf0eb 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -1,12 +1,13 @@ use poise::serenity_prelude as serenity; -use crate::{Data, Error}; use crate::models::ReactionRole; +use crate::{Data, Error}; -pub async fn event_handler(ctx: &serenity::Context, - event: &poise::Event<'_>, - data: &Data, - ) -> Result<(), Error> { +pub async fn event_handler( + ctx: &serenity::Context, + event: &poise::Event<'_>, + data: &Data, +) -> Result<(), Error> { { let pool = data.pg.lock().unwrap().clone(); match event { @@ -15,26 +16,55 @@ pub async fn event_handler(ctx: &serenity::Context, if add_reaction.user_id.unwrap() == current_user.id { return Ok(()); } - let rrole = sqlx::query_as!(ReactionRole, "SELECT * FROM reaction_roles WHERE message_id=$1 AND reaction=$2", add_reaction.message_id.0.to_string(), add_reaction.emoji.to_string()).fetch_one(&pool).await?; - let member = ctx.http.get_member(rrole.guild_id.parse::<u64>()?, add_reaction.user_id.unwrap().0).await?; + let rrole = sqlx::query_as!( + ReactionRole, + "SELECT * FROM reaction_roles WHERE message_id=$1 AND reaction=$2", + add_reaction.message_id.0.to_string(), + add_reaction.emoji.to_string() + ) + .fetch_one(&pool) + .await?; + let member = ctx + .http + .get_member( + rrole.guild_id.parse::<u64>()?, + add_reaction.user_id.unwrap().0, + ) + .await?; let member_roles = member.roles; let role_id = serenity::RoleId(rrole.role_id.parse::<u64>()?); if member_roles.contains(&role_id) { - ctx.http.remove_member_role(member.guild_id.0, member.user.id.0, role_id.0, Some("Reaction Role Menu")).await?; + ctx.http + .remove_member_role( + member.guild_id.0, + member.user.id.0, + role_id.0, + Some("Reaction Role Menu"), + ) + .await?; } else { - ctx.http.add_member_role(rrole.guild_id.parse::<u64>()?, add_reaction.user_id.unwrap().0, rrole.role_id.parse::<u64>()?, Some("Reaction Role")).await?; + ctx.http + .add_member_role( + rrole.guild_id.parse::<u64>()?, + add_reaction.user_id.unwrap().0, + rrole.role_id.parse::<u64>()?, + Some("Reaction Role"), + ) + .await?; } - let dm_chan = add_reaction.user_id.unwrap().create_dm_channel(&ctx.http).await?; + let dm_chan = add_reaction + .user_id + .unwrap() + .create_dm_channel(&ctx.http) + .await?; dm_chan.say(ctx, format!("Toggled the role!")).await?; add_reaction.delete(&ctx.http).await?; - - }, + } _ => (), } } - Ok(()) } |