diff options
author | Cara Salter <cara@devcara.com> | 2022-01-12 15:45:20 -0500 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-01-12 15:45:20 -0500 |
commit | 67e4081aa69f489ad88ee064402d68a5969069c2 (patch) | |
tree | c69b79679d35df650b684fa487b1725b3c211250 /src/handler.rs | |
parent | 6bc33b93eb5d55fb855803b0bf8cf7658a60e2b1 (diff) | |
download | glitch-ng-67e4081aa69f489ad88ee064402d68a5969069c2.tar.gz glitch-ng-67e4081aa69f489ad88ee064402d68a5969069c2.zip |
rroles: Implement Reaction Roles
now requires postgresql.
Diffstat (limited to 'src/handler.rs')
-rw-r--r-- | src/handler.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/handler.rs b/src/handler.rs new file mode 100644 index 0000000..933bd72 --- /dev/null +++ b/src/handler.rs @@ -0,0 +1,36 @@ +use poise::serenity_prelude as serenity; + +use crate::{Data, Error}; +use crate::models::ReactionRole; + +pub async fn event_handler(ctx: &serenity::Context, + event: &poise::Event<'_>, + data: &Data, + ) -> Result<(), Error> { + { + let pool = data.pg.lock().unwrap().clone(); + match event { + poise::Event::ReactionAdd { add_reaction } => { + 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?; + } 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?; + } + + 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(()) +} |