diff --git a/src/botv2/cmd/concour/create.rs b/src/botv2/cmd/concour/create.rs new file mode 100644 index 0000000..940e897 --- /dev/null +++ b/src/botv2/cmd/concour/create.rs @@ -0,0 +1,103 @@ +use crate::botv2::{ + domain::concour::{ + check_if_allowed::check_if_allowed, + create_concour::{create_concour, CreateConcourError}, + }, + init::{Context, Error}, +}; +use poise::{ + serenity_prelude::{model::colour, CreateEmbed, CreateEmbedFooter, Mentionable, RoleId}, + CreateReply, +}; +use tracing::instrument; + +/// Create 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 create( + ctx: Context<'_>, + #[description = "Titre du concour"] title: String, + #[description = "Description du concour"] description: String, +) -> 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 concour = + match create_concour(guild.get(), ctx.channel_id().get(), title, description).await { + Ok(concour) => { + if concour.is_none() { + CreateEmbed::new() + .title("No concour created") + .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 { + CreateConcourError::AlreadyExist => CreateEmbed::new() + .title("Concour already 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(()) +} diff --git a/src/botv2/cmd/concour/list.rs b/src/botv2/cmd/concour/list.rs new file mode 100644 index 0000000..f8a34ad --- /dev/null +++ b/src/botv2/cmd/concour/list.rs @@ -0,0 +1,98 @@ +use crate::botv2::{ + domain::concour::{check_if_allowed::check_if_allowed, list_concour::list_concour}, + init::{Context, Error}, +}; +use poise::{ + serenity_prelude::{model::colour, CreateEmbed, CreateEmbedFooter, Mentionable, RoleId}, + CreateReply, +}; +use tracing::instrument; + +/// List all 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 list(ctx: Context<'_>) -> 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 concour = match list_concour(guild.get()).await { + Ok(concour) => { + if concour.is_empty() { + vec![CreateEmbed::new() + .title("No concour found") + .color(colour::Color::DARK_GREEN) + .footer(footer)] + } else { + let mut list = vec![CreateEmbed::new() + .title("Concour list") + .color(colour::Color::DARK_GREEN)]; + concour.into_iter().for_each(|c| { + list.push( + CreateEmbed::new() + .title(c.title) + .description(c.description) + .field("Start date", c.start_date.to_string(), false) + .field("Periode", c.periode.to_string(), false) + .field( + "Role récompense", + RoleId::new(c.role_recompense).mention().to_string(), + false, + ) + .footer(footer.clone()), + ); + }); + list + } + } + Err(_) => vec![CreateEmbed::new() + .title("Error while getting concour list") + .color(colour::Color::RED) + .footer(footer)], + }; + + let mut builder = CreateReply::default().ephemeral(true); + builder.embeds = concour; + if let Err(why) = ctx.send(builder).await { + tracing::error!("Error sending message: {:?}", why); + } + Ok(()) +} diff --git a/src/botv2/cmd/concour/concour.rs b/src/botv2/cmd/concour/main.rs similarity index 92% rename from src/botv2/cmd/concour/concour.rs rename to src/botv2/cmd/concour/main.rs index c7b4432..ee3a031 100644 --- a/src/botv2/cmd/concour/concour.rs +++ b/src/botv2/cmd/concour/main.rs @@ -1,4 +1,5 @@ use crate::botv2::{ + cmd::concour::list::list, domain::concour::{ check_if_allowed::check_if_concour_allowed, get_channel_concour::get_channel_concour, }, @@ -16,7 +17,7 @@ use tracing::instrument; slash_command, prefix_command, category = "concour", - subcommands("get"), + subcommands("get", "list"), guild_only = true )] pub async fn concour( @@ -86,10 +87,10 @@ async fn concour_get(ctx: Context<'_>, ephemeral: Option) -> Result<(), Er if let Some(concour) = concour { CreateEmbed::new() .title("Concour") - .field("Title", &concour.title, false) - .field("Description", &concour.description, false) - .field("Start date", &concour.start_date.to_string(), false) - .field("Periode", &concour.periode.to_string(), false) + .field("Title", concour.title, false) + .field("Description", concour.description, false) + .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(), diff --git a/src/botv2/cmd/concour/mod.rs b/src/botv2/cmd/concour/mod.rs index 20b5f63..e14cd6a 100644 --- a/src/botv2/cmd/concour/mod.rs +++ b/src/botv2/cmd/concour/mod.rs @@ -1 +1,3 @@ -pub mod concour; +pub mod create; +pub mod list; +pub mod main; diff --git a/src/botv2/domain/concour/add_keyword.rs b/src/botv2/domain/concour/add_keyword.rs index 791cdd0..eb723d3 100644 --- a/src/botv2/domain/concour/add_keyword.rs +++ b/src/botv2/domain/concour/add_keyword.rs @@ -50,7 +50,7 @@ pub async fn add_keyword_concour( Ok(res) => { let text = res.text().await.unwrap(); let split_delimiter = delimiter.unwrap_or("\n".to_string()); - text.split(&split_delimiter).into_iter().for_each(|x| { + text.split(&split_delimiter).for_each(|x| { concour.keywords.push(x.to_string()); }); } @@ -65,7 +65,7 @@ pub async fn add_keyword_concour( } KeyWordSource::Text(text, delimiter) => { let split_delimiter = delimiter.unwrap_or("\n".to_string()); - text.split(&split_delimiter).into_iter().for_each(|x| { + text.split(&split_delimiter).for_each(|x| { concour.keywords.push(x.to_string()); }); } diff --git a/src/botv2/domain/concour/end_concour.rs b/src/botv2/domain/concour/end_concour.rs index aac532b..c524e1f 100644 --- a/src/botv2/domain/concour/end_concour.rs +++ b/src/botv2/domain/concour/end_concour.rs @@ -29,21 +29,19 @@ pub async fn end_concour( info!("Concour doesn't exist"); return Err(EndConcourError::DoesntExist); } - - let mut concour = concour.unwrap(); - - // Update status to Finished - todo!("Setup logic to end the waiting period for the concour"); + // let mut concour = concour.unwrap(); - match concour.update().await { - Ok(_) => {} - Err(err) => { - tracing::error!(error = err.to_string(), "Error updating concour"); - return Err(EndConcourError::UnknownError( - "Error updating concour".to_string(), - )); - } - } - Ok(Some(concour)) + // // Update status to Finished + + // match concour.update().await { + // Ok(_) => {} + // Err(err) => { + // tracing::error!(error = err.to_string(), "Error updating concour"); + // return Err(EndConcourError::UnknownError( + // "Error updating concour".to_string(), + // )); + // } + // } + // Ok(Some(concour)) } diff --git a/src/botv2/domain/concour/start_concour.rs b/src/botv2/domain/concour/start_concour.rs index e3de29a..0118166 100644 --- a/src/botv2/domain/concour/start_concour.rs +++ b/src/botv2/domain/concour/start_concour.rs @@ -29,21 +29,19 @@ pub async fn start_concour( info!("Concour doesn't exist"); return Err(StartConcourError::DoesntExist); } - - let mut concour = concour.unwrap(); - - // Update status to started - todo!("Setup logic to start the waiting period for the concour"); + // let mut concour = concour.unwrap(); - match concour.update().await { - Ok(_) => {} - Err(err) => { - tracing::error!(error = err.to_string(), "Error updating concour"); - return Err(StartConcourError::UnknownError( - "Error updating concour".to_string(), - )); - } - } - Ok(Some(concour)) + // // Update status to started + + // match concour.update().await { + // Ok(_) => {} + // Err(err) => { + // tracing::error!(error = err.to_string(), "Error updating concour"); + // return Err(StartConcourError::UnknownError( + // "Error updating concour".to_string(), + // )); + // } + // } + // Ok(Some(concour)) } diff --git a/src/botv2/domain/concour/stop_concour.rs b/src/botv2/domain/concour/stop_concour.rs index 805946e..8add4b7 100644 --- a/src/botv2/domain/concour/stop_concour.rs +++ b/src/botv2/domain/concour/stop_concour.rs @@ -29,21 +29,19 @@ pub async fn stop_concour( info!("Concour doesn't exist"); return Err(StopConcourError::DoesntExist); } - - let mut concour = concour.unwrap(); - - // Update status to Paused - todo!("Setup logic to stop the waiting period for the concour"); + // let mut concour = concour.unwrap(); - match concour.update().await { - Ok(_) => {} - Err(err) => { - tracing::error!(error = err.to_string(), "Error updating concour"); - return Err(StopConcourError::UnknownError( - "Error updating concour".to_string(), - )); - } - } - Ok(Some(concour)) + // // Update status to Paused + + // match concour.update().await { + // Ok(_) => {} + // Err(err) => { + // tracing::error!(error = err.to_string(), "Error updating concour"); + // return Err(StopConcourError::UnknownError( + // "Error updating concour".to_string(), + // )); + // } + // } + // Ok(Some(concour)) } diff --git a/src/botv2/init.rs b/src/botv2/init.rs index f2bf7f9..f34bf62 100644 --- a/src/botv2/init.rs +++ b/src/botv2/init.rs @@ -10,7 +10,7 @@ use std::sync::Arc; use tokio::sync::oneshot; use tracing::{info, instrument}; -use super::cmd::concour::concour::concour; +use super::cmd::concour::main::concour; pub struct Data { pub config_img: ConfigFile,