feat: handler update concour

This commit is contained in:
Max batleforc 2024-06-21 11:46:18 +02:00
parent 17bb4e03bf
commit cf83ccbd6e
No known key found for this signature in database
GPG Key ID: 25D243AB4B6AC9E7
3 changed files with 119 additions and 2 deletions

View File

@ -1,5 +1,5 @@
use crate::botv2::{ use crate::botv2::{
cmd::concour::list::list, cmd::concour::{list::list, update::update},
domain::concour::{ domain::concour::{
check_if_allowed::check_if_concour_allowed, get_channel_concour::get_channel_concour, check_if_allowed::check_if_concour_allowed, get_channel_concour::get_channel_concour,
}, },
@ -17,7 +17,7 @@ use tracing::instrument;
slash_command, slash_command,
prefix_command, prefix_command,
category = "concour", category = "concour",
subcommands("get", "list"), subcommands("get", "list", "update"),
guild_only = true guild_only = true
)] )]
pub async fn concour( pub async fn concour(

View File

@ -1,3 +1,4 @@
pub mod create; pub mod create;
pub mod list; pub mod list;
pub mod main; pub mod main;
pub mod update;

View File

@ -0,0 +1,116 @@
use crate::botv2::{
domain::concour::{
check_if_allowed::check_if_allowed,
update_concour::{update_concour, UpdateConcourError},
},
init::{Context, Error},
};
use poise::{
serenity_prelude::{model::colour, CreateEmbed, CreateEmbedFooter, Mentionable, Role, RoleId},
CreateReply,
};
use tracing::instrument;
/// Update concour (only for admin)
#[instrument(skip(ctx), level = "info", fields(channel = ctx.channel_id().get(), guild = ?ctx.guild_id().unwrap().get()))]
#[poise::command(
slash_command,
prefix_command,
category = "server_config",
guild_only = true
)]
pub async fn update(
ctx: Context<'_>,
#[description = "Titre du concour"] title: Option<String>,
#[description = "Description du concour"] description: Option<String>,
#[description = "Banniére du concour"] banner: Option<String>,
#[description = "Role récompense"] role_recompense: Option<Role>,
) -> Result<(), Error> {
let guild = match ctx.guild_id() {
Some(guild) => guild,
None => return Ok(()),
};
let entity_name = ctx.data().entity_name.clone();
let footer = CreateEmbedFooter::new(entity_name.clone());
match check_if_allowed(guild.get(), ctx.author().id.get(), ctx.http()).await {
Ok(ok) => {
if !ok {
let embed = CreateEmbed::new()
.title("You are not an admin")
.color(colour::Color::RED)
.footer(footer);
if let Err(why) = ctx
.send(CreateReply::default().embed(embed).ephemeral(true))
.await
{
tracing::error!("Error sending message: {:?}", why);
}
return Ok(());
}
}
Err(_) => {
let embed = CreateEmbed::new()
.title("You are not an admin")
.color(colour::Color::RED)
.footer(footer);
if let Err(why) = ctx
.send(CreateReply::default().embed(embed).ephemeral(true))
.await
{
tracing::error!("Error sending message: {:?}", why);
}
return Ok(());
}
};
let role = match role_recompense {
Some(role) => Some(role.id.get()),
None => None,
};
let concour = match update_concour(
guild.get(),
ctx.channel_id().get(),
title,
description,
role,
banner,
)
.await
{
Ok(concour) => {
if concour.is_none() {
CreateEmbed::new()
.title("No concour updated")
.color(colour::Color::RED)
} else {
let concour = concour.unwrap();
CreateEmbed::new()
.title(concour.title)
.description(concour.description)
.field("Start date", concour.start_date.to_string(), false)
.field("Periode", concour.periode.to_string(), false)
.field(
"Role récompense",
RoleId::new(concour.role_recompense).mention().to_string(),
false,
)
}
}
Err(err) => match err {
UpdateConcourError::DoesntExist => CreateEmbed::new()
.title("Concour dont exist")
.color(colour::Color::RED),
_ => CreateEmbed::new()
.title("Error while creating concour")
.field("Please contact your administrator", "", false)
.field("Your concour is possibly not initied correctly", "", false)
.color(colour::Color::RED),
},
};
let mut builder = CreateReply::default().ephemeral(true);
builder = builder.embed(concour.footer(footer));
if let Err(why) = ctx.send(builder).await {
tracing::error!("Error sending message: {:?}", why);
}
Ok(())
}