Files
caitsith/src/commands/roll.rs
T

45 lines
1.4 KiB
Rust
Raw Normal View History

2025-04-19 20:22:08 -04:00
use crate::{
types::dietype,
util::{junk, random, validate},
};
use serenity::builder::{CreateCommand, CreateCommandOption};
use serenity::model::application::{CommandOptionType, ResolvedOption, ResolvedValue};
pub async fn run(options: &[ResolvedOption<'_>]) -> String {
if let Some(ResolvedOption {
value: ResolvedValue::String(input),
..
}) = options.first()
{
let split = input.split("d").nth(1).unwrap();
let die_num = match validate::parse_str_into_num::<i32>(split.trim()) {
Some(d) => d,
None => return junk::get_random_insult(),
};
let _die_type = match dietype::DieType::from_sides(die_num) {
Some(d) => d.to_sides(),
None => return junk::get_random_insult(),
};
let mut rng = random::RandomGen::new();
2025-04-19 21:43:10 -04:00
let result = rng.range_random_from_one(die_num).to_string();
2025-04-19 20:22:08 -04:00
return format!("{result}");
} else {
junk::get_random_insult()
}
}
pub fn register() -> CreateCommand {
CreateCommand::new("roll")
.description("Roll dice")
.add_option(
CreateCommandOption::new(
CommandOptionType::String,
"input",
"The number sides for the dice roll, typically starts with \'d\' then a number",
)
.required(true),
)
}