feat: handler create list main

This commit is contained in:
Max batleforc 2024-06-21 11:38:39 +02:00
parent 683a9d94a4
commit 17bb4e03bf
No known key found for this signature in database
GPG Key ID: 25D243AB4B6AC9E7
9 changed files with 252 additions and 54 deletions

View File

@ -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(())
}

View File

@ -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(())
}

View File

@ -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<bool>) -> 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(),

View File

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

View File

@ -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());
});
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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))
}

View File

@ -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,