BotDiscord/src/botv2/cmd/concour/concour.rs

120 lines
4.3 KiB
Rust

use crate::botv2::{
domain::concour::{
check_if_allowed::check_if_concour_allowed, get_channel_concour::get_channel_concour,
},
init::{Context, Error},
};
use poise::{
serenity_prelude::{model::colour, CreateEmbed, CreateEmbedFooter, Mentionable, RoleId},
CreateReply,
};
use tracing::instrument;
/// Show concour in the current channel (alias of get)
#[instrument(skip(ctx), level = "info", fields(channel = ctx.channel_id().get(), guild = ?ctx.guild_id().unwrap().get()))]
#[poise::command(
slash_command,
prefix_command,
category = "concour",
subcommands("get"),
guild_only = true
)]
pub async fn concour(
ctx: Context<'_>,
#[description = "Reponse cacher ?"] ephemeral: Option<bool>,
) -> Result<(), Error> {
concour_get(ctx, ephemeral).await
}
/// Show concour in the current channel
#[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 get(
ctx: Context<'_>,
#[description = "Reponse cacher ?"] ephemeral: Option<bool>,
) -> Result<(), Error> {
concour_get(ctx, ephemeral).await
}
#[instrument(skip(ctx), level = "info")]
async fn concour_get(ctx: Context<'_>, ephemeral: Option<bool>) -> 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_concour_allowed(guild.get()).await {
Ok((ok, _)) => {
if !ok {
let embed = CreateEmbed::new()
.title("The concour feature is disabled on this server, please contact your administrator")
.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("Error while fetching server config")
.field("Please contact your administrator", "", false)
.field("Your config is possibly not initied correctly", "", false)
.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 get_channel_concour(guild.get(), ctx.channel_id().get()).await {
Ok(concour) => {
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(
"Role récompense",
RoleId::new(concour.role_recompense).mention().to_string(),
false,
)
.color(colour::Color::DARK_GREEN)
} else {
CreateEmbed::new()
.title("No concour found in this channel")
.color(colour::Color::DARK_GREEN)
}
}
Err(_) => CreateEmbed::new()
.title("Error while fetching 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().embed(concour.footer(footer));
if ephemeral.unwrap_or(true) {
builder = builder.ephemeral(true);
}
if let Err(why) = ctx.send(builder).await {
tracing::error!("Error sending message: {:?}", why);
}
Ok(())
}