minor code cleanup

This commit is contained in:
Lilith Schier 2026-08-21 13:20:52 +02:00
parent 0843c77473
commit 7cce361c73
3 changed files with 31 additions and 34 deletions

View file

@ -137,8 +137,7 @@ pub async fn roll(ctx: &Context, command: &CommandInteraction) -> anyhow::Result
}
})
.unwrap_or(1)
.min(MAX_ROLL_REPEATS as i64)
.max(1) as usize;
.clamp(1, MAX_ROLL_REPEATS as i64) as usize;
let private = options
.iter()
.find(|option| option.name == "private")
@ -170,7 +169,8 @@ pub async fn roll(ctx: &Context, command: &CommandInteraction) -> anyhow::Result
None
}
})
.and_then(|s| s.parse::<u64>().ok());
.map(|s| s.parse::<u64>().context("Seed could not be parsed. Make sure it is a 64 bit integer between 0 and 2^64-1."))
.transpose()?;
let expression = parsing::parse(&formula).context("Dice formula could not be parsed.")?;
@ -193,13 +193,13 @@ pub async fn roll(ctx: &Context, command: &CommandInteraction) -> anyhow::Result
let surrounding_par_count = pre_paragraphs.len() + post_paragraphs.len();
let paragraphs = if repeat <= 40 - surrounding_par_count {
let roll_paragraphs = if repeat <= 40 - surrounding_par_count {
let discord_md = Arc::new(std::sync::RwLock::new(DiscordMd {
buffers: Vec::with_capacity(repeat),
dice_written: 0,
}));
for _ in 0..repeat {
let mut witness = DiscordMd::create_witness(&discord_md);
let mut witness = DiscordMd::create_witness(discord_md.clone());
let result = expression.evaluate(&mut rng, &mut witness, i64::MAX)?;
witness.witness_total_result(result)?;
witness.end();
@ -208,7 +208,7 @@ pub async fn roll(ctx: &Context, command: &CommandInteraction) -> anyhow::Result
discord_md.buffers
} else {
let mut par = String::new();
let mut par = String::with_capacity(8 * repeat);
for i in 0..repeat {
if i > 0 {
write!(par, ", ")?;
@ -222,9 +222,11 @@ pub async fn roll(ctx: &Context, command: &CommandInteraction) -> anyhow::Result
vec![par]
};
pre_paragraphs.extend(paragraphs);
pre_paragraphs.extend(post_paragraphs);
let paragraphs = pre_paragraphs;
let paragraphs = {
pre_paragraphs.extend(roll_paragraphs);
pre_paragraphs.extend(post_paragraphs);
pre_paragraphs
};
if paragraphs.iter().map(|s| s.chars().count()).sum::<usize>() > 4000
|| paragraphs.len() > 40