diff --git a/src/botv2/cmd/concour/main.rs b/src/botv2/cmd/concour/main.rs index a98366c..2559553 100644 --- a/src/botv2/cmd/concour/main.rs +++ b/src/botv2/cmd/concour/main.rs @@ -1,5 +1,5 @@ use crate::botv2::{ - cmd::concour::{create::create, keyword::keyword, list::list, update::update}, + cmd::concour::{create::create, keyword::keyword, list::list, period::period, update::update}, domain::concour::{ check_if_allowed::check_if_concour_allowed, get_channel_concour::get_channel_concour, }, @@ -17,7 +17,7 @@ use tracing::instrument; slash_command, prefix_command, category = "concour", - subcommands("get", "list", "update", "create", "keyword"), + subcommands("get", "list", "update", "create", "keyword", "period"), guild_only = true )] pub async fn concour( diff --git a/src/botv2/cmd/concour/mod.rs b/src/botv2/cmd/concour/mod.rs index c162024..2909a5f 100644 --- a/src/botv2/cmd/concour/mod.rs +++ b/src/botv2/cmd/concour/mod.rs @@ -2,4 +2,5 @@ pub mod create; pub mod keyword; pub mod list; pub mod main; +pub mod period; pub mod update; diff --git a/src/botv2/cmd/concour/period.rs b/src/botv2/cmd/concour/period.rs new file mode 100644 index 0000000..d5137a5 --- /dev/null +++ b/src/botv2/cmd/concour/period.rs @@ -0,0 +1,101 @@ +use crate::botv2::{ + domain::concour::{ + check_if_allowed::check_if_allowed, + set_periode::{set_periode, SetPeriodeConcourError}, + }, + init::{Context, Error}, +}; +use poise::{ + serenity_prelude::{model::colour, CreateEmbed, CreateEmbedFooter, Mentionable, RoleId}, + CreateReply, +}; +use tracing::instrument; + +/// Update the step duration of a 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 period( + ctx: Context<'_>, + #[description = "Dureer d'une étape du concours"] step: u64, +) -> 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 duration = time::Duration::new(step.try_into().unwrap(), 0); + let concour = match set_periode(guild.get(), ctx.channel_id().get(), duration).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 { + SetPeriodeConcourError::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(()) +}