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

Prise en charge des emojis personnalisés

parent 5570329c
......@@ -5,6 +5,7 @@ use serenity::prelude::SerenityError;
use serde::Deserialize;
use serenity::model::channel::ReactionType;
use tracing::info;
use itertools::Itertools;
fn log_and_convert(error: SerenityError) -> ClientError {
tracing::warn!("Error from Discord: {:#?}", error);
......@@ -103,13 +104,22 @@ pub async fn delete_all_reactions(web_params: web::Path<(u64, u64)>) -> Result<H
#[post("/messages/{channel_id}/{message_id}/react/{reaction}")]
pub async fn add_reaction(web_params: web::Path<(u64, u64, String)>) -> Result<HttpResponse, ClientError> {
let (channel_id, message_id, reaction) = web_params.into_inner();
let reaction = reaction.replace(":", "");
let client = DiscordClient::get();
let guild = match client.cache.guild_channel(channel_id).await {
None => return Err(ClientError::NotFound),
Some(guild_channel) => match guild_channel.guild(&client.cache).await {
None => return Err(ClientError::NotFound),
Some(guild) => guild,
}
};
info!("Getting message ID {} in {} from cache", message_id, channel_id);
let _ = match client.cache.message(channel_id, message_id).await {
Some(message) => message,
None => {
info!("Getting message ID {} in {} from Discord", message_id, channel_id);
client.http.get_message(channel_id, message_id)
.await
......@@ -117,8 +127,32 @@ pub async fn add_reaction(web_params: web::Path<(u64, u64, String)>) -> Result<H
}
};
// TODO: Prendre en charge les emojis personnalisée
let reaction = ReactionType::Unicode(reaction);
/*
On a plusieurs cas de figure qui se présentent à nous:
- Emoji unicode
- Nom d'un emoji personnalisé
- Emoji unicode mais sous forme textuelle, style :upside_down:
Pour le premier cas, simple: caractère unicode et hasta la vista baby.
Attention aux emojis qui sont sur plusieurs bytes !
Pour le second cas, on peut essayer de trouver quelque chose et continuer avec ça.
Pour le troisième cas... Pas possible !
*/
let emoji = guild.emojis.values()
.filter(|emoji| emoji.name == reaction)
.take(1)
.collect_vec()
.get(0)
.map(|e| *e);
let reaction = match emoji {
Some(emoji) => ReactionType::Custom { animated: emoji.animated, id: emoji.id, name: Some(emoji.name.clone()) },
// Unicode emoji or... hope for the best !
None => ReactionType::Unicode(reaction)
};
info!("Reaction: {:?}", reaction);
info!("Creating reaction");
......
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