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

Prise en charge des emojis personnalisés par nom

parent 871e305e
use serenity::client::Context;
use serenity::model::channel::Reaction;
use serenity::model::channel::{Reaction, ReactionType};
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<()> {
let (reaction_role, mut member) = fetch_role_member(ctx, &reaction).await?;
let (reaction_role, mut member) = fetch_role_member(ctx, reaction).await?;
if member.user.id == ctx.cache.current_user_id().await {
tracing::info!("S'assigner un rôle en réagissant à notre propre message n'est pas valide");
return Ok(())
}
if member.roles.contains(&reaction_role.roles_id) {
return Ok(());
......@@ -21,6 +26,11 @@ pub async fn reaction_add(ctx: &Context, reaction: &Reaction) -> eyre::Result<()
pub async fn reaction_remove(ctx: &Context, reaction: &Reaction) -> eyre::Result<()> {
let (reaction_role, mut member) = fetch_role_member(ctx, &reaction).await?;
if member.user.id == ctx.cache.current_user_id().await {
tracing::info!("Se désassigner un rôle en dé-réagissant à notre propre message n'est pas valide");
return Ok(())
}
if !member.roles.contains(&reaction_role.roles_id) {
return Ok(());
}
......@@ -31,7 +41,7 @@ pub async fn reaction_remove(ctx: &Context, reaction: &Reaction) -> eyre::Result
Ok(())
}
async fn fetch_role_member(ctx: &Context, reaction: &&Reaction) -> eyre::Result<(ReactionAssignment, Member)> {
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?
......@@ -41,7 +51,13 @@ async fn fetch_role_member(ctx: &Context, reaction: &&Reaction) -> eyre::Result<
.context("Aucun message trouvé qui correspond à un menu de réaction")?;
// Récupérer la réaction en base afin de déterminer le rôle à assigner
let reaction_role = RoleReactionRepository::fetch_role_from_reaction(reaction_message.id, &reaction.emoji.to_string())
let str_reaction = match &reaction.emoji {
ReactionType::Custom { animated: _, id: _, name: Some(name) } => format!(":{}:", name),
ReactionType::Unicode(emoji) => emoji.clone(),
_ => reaction.emoji.to_string()
};
let reaction_role = RoleReactionRepository::fetch_role_from_reaction(reaction_message.id, &str_reaction)
.await?
.context("Aucun rôle correspondant à la réaction donnée")?;
......
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