47 lines
1.4 KiB
Rust
Raw Normal View History

use crate::{
types::dietype,
util::{junk, random, validate},
};
use rand::Rng;
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()
{
println!("got to the roll command! input: {}", input);
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();
let result = rng.rng.gen_range(1..die_num).to_string();
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),
)
}