mirror of
https://github.com/arthur-pbty/shadowbot.git
synced 2026-06-16 23:57:56 +02:00
netoyage et optimisation des commande pour que elle soit trier correctement
This commit is contained in:
+149
-104
@@ -35,124 +35,169 @@ pub async fn handle_punish(ctx: &Context, msg: &Message, args: &[&str]) {
|
||||
let bot_id = ctx.cache.current_user().id.get() as i64;
|
||||
let guild_id_raw = guild_id.get() as i64;
|
||||
|
||||
if args.is_empty() {
|
||||
let rules = db::list_punish_rules(&pool, bot_id, guild_id_raw)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
|
||||
let description = if rules.is_empty() {
|
||||
"Aucune regle.".to_string()
|
||||
} else {
|
||||
rules
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(idx, rule)| describe_rule(idx + 1, rule))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
};
|
||||
|
||||
if !args.is_empty() {
|
||||
send_embed(
|
||||
ctx,
|
||||
msg,
|
||||
CreateEmbed::new()
|
||||
.title("Punish")
|
||||
.description(description)
|
||||
.color(0x5865F2),
|
||||
.description("Usage: +punish")
|
||||
.color(0xED4245),
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
|
||||
if args[0].eq_ignore_ascii_case("setup") {
|
||||
let _ = db::setup_default_punish_rules(&pool, bot_id, guild_id_raw).await;
|
||||
send_embed(
|
||||
ctx,
|
||||
msg,
|
||||
CreateEmbed::new()
|
||||
.title("Punish")
|
||||
.description("Regles par defaut restaurees.")
|
||||
.color(0x57F287),
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
let rules = db::list_punish_rules(&pool, bot_id, guild_id_raw)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
|
||||
if args[0].eq_ignore_ascii_case("add") {
|
||||
if args.len() < 4 {
|
||||
return;
|
||||
}
|
||||
|
||||
let Ok(threshold) = args[1].parse::<i32>() else {
|
||||
return;
|
||||
};
|
||||
let Some(window_seconds) = parse_duration_to_seconds(args[2]) else {
|
||||
return;
|
||||
};
|
||||
let Some(sanction) = parse_sanction(args[3]) else {
|
||||
return;
|
||||
};
|
||||
let sanction_seconds = args.get(4).and_then(|raw| parse_duration_to_seconds(raw));
|
||||
|
||||
let _ = db::upsert_punish_rule(
|
||||
&pool,
|
||||
bot_id,
|
||||
guild_id_raw,
|
||||
threshold.clamp(1, 200),
|
||||
window_seconds,
|
||||
sanction,
|
||||
sanction_seconds,
|
||||
)
|
||||
.await;
|
||||
|
||||
send_embed(
|
||||
ctx,
|
||||
msg,
|
||||
CreateEmbed::new()
|
||||
.title("Punish")
|
||||
.description("Regle ajoutee ou mise a jour.")
|
||||
.color(0x57F287),
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
|
||||
if args[0].eq_ignore_ascii_case("del") {
|
||||
let Some(raw_index) = args.get(1) else {
|
||||
return;
|
||||
};
|
||||
let Ok(index) = raw_index.parse::<usize>() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let rules = db::list_punish_rules(&pool, bot_id, guild_id_raw)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
if index == 0 || index > rules.len() {
|
||||
return;
|
||||
}
|
||||
|
||||
let rule = &rules[index - 1];
|
||||
let _ = db::delete_punish_rule_by_id(&pool, bot_id, guild_id_raw, rule.id).await;
|
||||
|
||||
send_embed(
|
||||
ctx,
|
||||
msg,
|
||||
CreateEmbed::new()
|
||||
.title("Punish")
|
||||
.description(format!("Regle {} supprimee.", index))
|
||||
.color(0x57F287),
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
let description = if rules.is_empty() {
|
||||
"Aucune regle.".to_string()
|
||||
} else {
|
||||
rules
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(idx, rule)| describe_rule(idx + 1, rule))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
};
|
||||
|
||||
send_embed(
|
||||
ctx,
|
||||
msg,
|
||||
CreateEmbed::new()
|
||||
.title("Punish")
|
||||
.description("Usage: +punish | +punish add <nombre> <duree> <sanction> [duree] | +punish del <numero> | +punish setup")
|
||||
.color(0xED4245),
|
||||
.description(description)
|
||||
.color(0x5865F2),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
pub async fn handle_punishsetup(ctx: &Context, msg: &Message, _args: &[&str]) {
|
||||
let Some(guild_id) = msg.guild_id else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(pool) = pool(ctx).await else {
|
||||
return;
|
||||
};
|
||||
let bot_id = ctx.cache.current_user().id.get() as i64;
|
||||
let guild_id_raw = guild_id.get() as i64;
|
||||
|
||||
let _ = db::setup_default_punish_rules(&pool, bot_id, guild_id_raw).await;
|
||||
send_embed(
|
||||
ctx,
|
||||
msg,
|
||||
CreateEmbed::new()
|
||||
.title("Punish")
|
||||
.description("Regles par defaut restaurees.")
|
||||
.color(0x57F287),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
pub async fn handle_punishadd(ctx: &Context, msg: &Message, args: &[&str]) {
|
||||
let Some(guild_id) = msg.guild_id else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(pool) = pool(ctx).await else {
|
||||
return;
|
||||
};
|
||||
let bot_id = ctx.cache.current_user().id.get() as i64;
|
||||
let guild_id_raw = guild_id.get() as i64;
|
||||
|
||||
if args.len() < 3 {
|
||||
send_embed(
|
||||
ctx,
|
||||
msg,
|
||||
CreateEmbed::new()
|
||||
.title("Punish")
|
||||
.description("Usage: +punishadd <nombre> <duree> <sanction> [duree]")
|
||||
.color(0xED4245),
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
|
||||
let Ok(threshold) = args[0].parse::<i32>() else {
|
||||
return;
|
||||
};
|
||||
let Some(window_seconds) = parse_duration_to_seconds(args[1]) else {
|
||||
return;
|
||||
};
|
||||
let Some(sanction) = parse_sanction(args[2]) else {
|
||||
return;
|
||||
};
|
||||
let sanction_seconds = args.get(3).and_then(|raw| parse_duration_to_seconds(raw));
|
||||
|
||||
let _ = db::upsert_punish_rule(
|
||||
&pool,
|
||||
bot_id,
|
||||
guild_id_raw,
|
||||
threshold.clamp(1, 200),
|
||||
window_seconds,
|
||||
sanction,
|
||||
sanction_seconds,
|
||||
)
|
||||
.await;
|
||||
|
||||
send_embed(
|
||||
ctx,
|
||||
msg,
|
||||
CreateEmbed::new()
|
||||
.title("Punish")
|
||||
.description("Regle ajoutee ou mise a jour.")
|
||||
.color(0x57F287),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
pub async fn handle_punishdel(ctx: &Context, msg: &Message, args: &[&str]) {
|
||||
let Some(guild_id) = msg.guild_id else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(pool) = pool(ctx).await else {
|
||||
return;
|
||||
};
|
||||
let bot_id = ctx.cache.current_user().id.get() as i64;
|
||||
let guild_id_raw = guild_id.get() as i64;
|
||||
|
||||
let Some(raw_index) = args.first() else {
|
||||
send_embed(
|
||||
ctx,
|
||||
msg,
|
||||
CreateEmbed::new()
|
||||
.title("Punish")
|
||||
.description("Usage: +punishdel <numero>")
|
||||
.color(0xED4245),
|
||||
)
|
||||
.await;
|
||||
return;
|
||||
};
|
||||
let Ok(index) = raw_index.parse::<usize>() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let rules = db::list_punish_rules(&pool, bot_id, guild_id_raw)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
if index == 0 || index > rules.len() {
|
||||
return;
|
||||
}
|
||||
|
||||
let rule = &rules[index - 1];
|
||||
let _ = db::delete_punish_rule_by_id(&pool, bot_id, guild_id_raw, rule.id).await;
|
||||
|
||||
send_embed(
|
||||
ctx,
|
||||
msg,
|
||||
CreateEmbed::new()
|
||||
.title("Punish")
|
||||
.description(format!("Regle {} supprimee.", index))
|
||||
.color(0x57F287),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -165,9 +210,9 @@ impl crate::commands::command_contract::CommandSpec for PunishCommand {
|
||||
crate::commands::command_contract::CommandMetadata {
|
||||
name: "punish",
|
||||
category: "mod",
|
||||
params: "[add <nombre> <duree> <sanction> [duree] | del <numero> | setup]",
|
||||
description: "Affiche et gere les sanctions automatiques appliquees selon les strikes.",
|
||||
examples: &["+punish", "+punish add 8 1h mute 30m", "+punish setup"],
|
||||
params: "aucun",
|
||||
description: "Affiche les sanctions automatiques appliquees selon les strikes.",
|
||||
examples: &["+punish", "+help punish"],
|
||||
default_aliases: &["pn"],
|
||||
allow_in_dm: false,
|
||||
default_permission: 7,
|
||||
|
||||
Reference in New Issue
Block a user