From 67dd9a1f2613e7781fca3c309b6b3b93804dab18 Mon Sep 17 00:00:00 2001 From: Skye Date: Thu, 28 Dec 2023 18:12:36 +0900 Subject: opt-commands: add uwurandom (#112) * opt-commands: add uwurandom * Apply suggestions from code review Co-authored-by: seth * opt-commands: set random default value for uwurandom --------- Co-authored-by: seth --- src/commands/optional/mod.rs | 7 ++++++- src/commands/optional/uwurandom.rs | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/commands/optional/uwurandom.rs (limited to 'src') diff --git a/src/commands/optional/mod.rs b/src/commands/optional/mod.rs index 2be1fef..8ef7757 100644 --- a/src/commands/optional/mod.rs +++ b/src/commands/optional/mod.rs @@ -5,7 +5,12 @@ use poise::Command; mod copypasta; mod teawiespam; +mod uwurandom; pub fn to_commands() -> Vec> { - vec![copypasta::copypasta(), teawiespam::teawiespam()] + vec![ + copypasta::copypasta(), + teawiespam::teawiespam(), + uwurandom::uwurandom(), + ] } diff --git a/src/commands/optional/uwurandom.rs b/src/commands/optional/uwurandom.rs new file mode 100644 index 0000000..c8405a0 --- /dev/null +++ b/src/commands/optional/uwurandom.rs @@ -0,0 +1,41 @@ +use crate::Context; + +use color_eyre::eyre::Result; +use log::debug; +use rand::Rng; + +/// Generate some amount of uwurandom +#[poise::command(slash_command)] +pub async fn uwurandom( + ctx: Context<'_>, + #[description = "The amount of uwurandom to generate"] + #[min = 1] + #[max = 2000] + length: Option, +) -> Result<()> { + let gid = ctx.guild_id().unwrap_or_default(); + let settings = ctx.data().storage.get_guild_settings(&gid).await?; + + if !settings.optional_commands_enabled { + debug!("Not running uwurandom in {gid} since it's disabled"); + ctx.say("I'm not allowed to do that here").await?; + return Ok(()); + } + + let length = length.unwrap_or(rand::thread_rng().gen_range(1..50)); + + let mut result = String::with_capacity(length as usize); + // ThreadRng is not Send(obviously), and rustc is slightly too paranoid about rng spilling to await point + // So calm it by constraining it to a block + { + let mut rng = rand::thread_rng(); + let mut state_machine = uwurandom_rs::StateMachine::new(&mut rng); + for _ in 0..length { + let generated; + (state_machine, generated) = state_machine.generate(&mut rng); + result.push(generated); + } + } + ctx.say(result).await?; + Ok(()) +} -- cgit v1.2.3