177 lines
6.6 KiB
Rust
177 lines
6.6 KiB
Rust
use crate::botv2::{
|
|
domain::concour::{
|
|
check_if_allowed::check_if_allowed,
|
|
start_concour::{start_concour, start_concour_step2, StartConcourError},
|
|
},
|
|
init::{Context, Error},
|
|
};
|
|
use poise::{
|
|
serenity_prelude::{model::colour, CreateEmbed, CreateEmbedFooter, Mentionable, RoleId},
|
|
CreateReply,
|
|
};
|
|
use tracing::instrument;
|
|
|
|
/// Start 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 start(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 mut cron_schedule = ctx.data().scheduler.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, text, success) = match start_concour(
|
|
guild.get(),
|
|
ctx.channel_id().get(),
|
|
&mut cron_schedule,
|
|
ctx.http(),
|
|
)
|
|
.await
|
|
{
|
|
Ok(concour) => {
|
|
if concour.is_none() {
|
|
(
|
|
CreateEmbed::new()
|
|
.title("No concour created")
|
|
.color(colour::Color::RED),
|
|
"".to_string(),
|
|
false,
|
|
)
|
|
} else {
|
|
let concour = concour.unwrap();
|
|
let keyword_index = concour.index_keyword as usize;
|
|
let keyword = concour.keywords.get(keyword_index).unwrap();
|
|
// TODO : condition pour returner ou non le texte ? ou transformer les annonce obligatoirement en texte ? ou permettre de choisir entre embed ou texte ?
|
|
let ping_concour = match concour.ping_concour {
|
|
Some(role) => RoleId::new(role).mention().to_string(),
|
|
None => "".to_string(),
|
|
};
|
|
let role_recompense = if concour.role_recompense != 0 {
|
|
RoleId::new(concour.role_recompense).mention().to_string()
|
|
} else {
|
|
"(Pas encore définis)".to_string()
|
|
};
|
|
let text = format!("
|
|
Bonsoir !
|
|
|
|
👹 ❱ Le thème de ce soir est : {}
|
|
|
|
📜 ❱ Les règles : Pas de loli, ni de shoota, ni de irl, ni de zoo.
|
|
|
|
Celui ou celle qui a le plus de votes gagne, comme récompense elle aura le rôle {} pour une durée de 48h.
|
|
Le concours ce termine dans deux jours.
|
|
Ceux qui votent pour leur propre photo, cela ne sera pas pris en compte, une photo par personne.
|
|
|
|
À vos photos !
|
|
|
|
{}
|
|
", keyword,role_recompense,ping_concour);
|
|
let output = CreateEmbed::new()
|
|
.title(format!(
|
|
"Concour: {} Jour : {}",
|
|
concour.title,
|
|
concour.index_keyword + 1
|
|
))
|
|
.description(concour.description)
|
|
.field("Word of the day ", keyword.to_string(), false)
|
|
.field("Good luck !", "", false)
|
|
.field(
|
|
"Please see when the concour end in the concour get command",
|
|
"",
|
|
false,
|
|
)
|
|
.color(colour::Color::DARK_GREEN);
|
|
(output, text, true)
|
|
}
|
|
}
|
|
Err(err) => (
|
|
match err {
|
|
StartConcourError::AlreadyOnGoing => CreateEmbed::new()
|
|
.title("Concour already OnGoing")
|
|
.color(colour::Color::RED),
|
|
StartConcourError::DoesntExist => CreateEmbed::new()
|
|
.title("Concour doesn't exist")
|
|
.color(colour::Color::RED),
|
|
StartConcourError::KeyWordListEmpty => CreateEmbed::new()
|
|
.title("Keyword list empty")
|
|
.color(colour::Color::RED),
|
|
StartConcourError::FinishedKeyWordList => CreateEmbed::new()
|
|
.title("Finished keyword list, add new one")
|
|
.color(colour::Color::RED),
|
|
StartConcourError::RoleRecompenseEmpty => CreateEmbed::new()
|
|
.title("Role recompense not defined")
|
|
.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),
|
|
},
|
|
"".to_string(),
|
|
false,
|
|
),
|
|
};
|
|
let mut builder = CreateReply::default().ephemeral(!success);
|
|
if !text.is_empty() {
|
|
builder = builder.content(text);
|
|
} else {
|
|
builder = builder.embed(concour.footer(footer));
|
|
}
|
|
match ctx.send(builder).await {
|
|
Ok(handler) => {
|
|
if success {
|
|
let message_id = handler.message().await.unwrap().id.get();
|
|
tracing::info!(msg_id = message_id, "Concour started");
|
|
match start_concour_step2(guild.get(), ctx.channel_id().get(), message_id).await {
|
|
Ok(_) => {}
|
|
Err(err) => {
|
|
tracing::error!("Error starting concour step 2: {:?}", err);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Err(why) => {
|
|
tracing::error!("Error sending message: {:?}", why);
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|