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

Ajout de la suppression de toutes les réactions et de la création de ces dernières

parent 5a1f21fd
......@@ -3,6 +3,8 @@ use crate::rpc_server::DiscordClient;
use crate::rpc_server::utilities::ClientError;
use serenity::prelude::SerenityError;
use serde::Deserialize;
use serenity::model::channel::ReactionType;
use tracing::info;
fn log_and_convert(error: SerenityError) -> ClientError {
tracing::warn!("Error from Discord: {:#?}", error);
......@@ -17,6 +19,7 @@ pub struct SendMessageRequestBody {
#[get("/channels/{channel_id}/{message_id}")]
#[tracing::instrument]
pub async fn get_message(path_params: web::Path<(u64, u64)>) -> Result<HttpResponse, ClientError> {
// TODO : Récupérer le message sans devoir passer par un channel
let (channel_id, message_id) = path_params.into_inner();
let client = DiscordClient::get();
......@@ -51,6 +54,7 @@ pub async fn send_message(path_params: web::Path<u64>, body: web::Json<SendMessa
#[delete("/channels/{channel_id}/{message_id}")]
pub async fn delete_message(path_params: web::Path<(u64, u64)>) -> Result<HttpResponse, ClientError> {
// TODO : Permettre de supprimer l'ancien message sans devoir passer par un channel
let (channel_id, message_id) = path_params.into_inner();
let client = DiscordClient::get();
......@@ -78,12 +82,48 @@ pub async fn update_message(path_params: web::Path<(u64, u64)>, update_body: web
.map(|_| HttpResponse::NoContent().finish())
}
#[delete("/messages/{message_id}/remove-all-reactions")]
pub async fn delete_all_reactions() -> HttpResponse {
todo!("Implémenter la méthode")
#[delete("/messages/{channel_id}/{message_id}/remove-all-reactions")]
pub async fn delete_all_reactions(web_params: web::Path<(u64, u64)>) -> Result<HttpResponse, ClientError> {
let (channel_id, message_id) = web_params.into_inner();
let client = DiscordClient::get();
let message = match client.cache.message(channel_id, message_id).await {
Some(message) => message,
None => {
client.http.get_message(channel_id, message_id).await
.map_err(log_and_convert)?
}
};
message.delete_reactions(&client.http).await
.map_err(log_and_convert)
.map(|_| HttpResponse::NoContent().finish())
}
#[post("/messages/{message_id}/react")]
pub async fn add_reaction() -> HttpResponse {
todo!("Implémenter la méthode")
#[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 client = DiscordClient::get();
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
.map_err(log_and_convert)?
}
};
// TODO: Prendre en charge les emojis personnalisée
let reaction = ReactionType::Unicode(reaction);
info!("Reaction: {:?}", reaction);
info!("Creating reaction");
client.http.create_reaction(channel_id, message_id, &reaction)
.await
.map(|_| HttpResponse::NoContent().finish())
.map_err(log_and_convert)
}
\ 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