Verified Commit 5570329c authored by Mathias B.'s avatar Mathias B.
Browse files

Retrait d'un rôle à un utilisateur

parent 80648a82
......@@ -2,9 +2,37 @@ use serenity::client::Context;
use serenity::model::channel::Reaction;
use crate::repositories::RoleReactionRepository;
use eyre::ContextCompat;
use datastructs::bot::ReactionAssignment;
use serenity::model::guild::Member;
pub async fn reaction_add(ctx: &Context, reaction: &Reaction) -> eyre::Result<()> {
// Récupérer le message correspondant au menu
let (reaction_role, mut member) = fetch_role_member(ctx, &reaction).await?;
if member.roles.contains(&reaction_role.roles_id) {
return Ok(());
}
tracing::info!("Assignation du rôle {} à {} sur {:?}", reaction_role.roles_id, member.user.id, reaction.guild_id);
member.add_role(ctx, reaction_role.roles_id).await?;
Ok(())
}
pub async fn reaction_remove(ctx: &Context, reaction: &Reaction) -> eyre::Result<()> {
let (reaction_role, mut member) = fetch_role_member(ctx, &reaction).await?;
if !member.roles.contains(&reaction_role.roles_id) {
return Ok(());
}
tracing::info!("Retrait du rôle {} à {} sur {:?}", reaction_role.roles_id, member.user.id, reaction.guild_id);
member.remove_role(ctx, &reaction_role.roles_id).await?;
Ok(())
}
async fn fetch_role_member(ctx: &Context, reaction: &&Reaction) -> eyre::Result<(ReactionAssignment, Member)> {
// Récupérer le message correspondant au menu
let reaction_message = RoleReactionRepository::fetch_reaction_menu(reaction.message_id)
.await?
// Si on ne trouve aucun message correspondant à un menu, on retourne (Option::with_context)
......@@ -19,23 +47,12 @@ pub async fn reaction_add(ctx: &Context, reaction: &Reaction) -> eyre::Result<()
// Chercher le rôle dans ceux de l'utilisateur. A force de déplacer les choses, la personne peut
// s'"assigner" le rôle mais en réalité ce sera un no-op.
let mut member = ctx.cache.member(
let member = ctx.cache.member(
reaction.guild_id.context("Réaction pas dans une guilde")?,
reaction.user_id.expect("Cache is unavaliable")
)
.await
.context("Aucun membre correspondant dans la guilde")?;
if member.roles.contains(&reaction_role.roles_id) {
return Ok(());
}
member.add_role(ctx, reaction_role.roles_id).await?;
Ok(())
Ok((reaction_role, member))
}
pub async fn reaction_remove(ctx: &Context, reaction: &Reaction) -> eyre::Result<()> {
todo!()
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment