summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSkye <[email protected]>2023-12-28 18:12:36 +0900
committerGitHub <[email protected]>2023-12-28 09:12:36 +0000
commit67dd9a1f2613e7781fca3c309b6b3b93804dab18 (patch)
tree0ea138463227a032084d4274cbea5eaf52e97a82
parenta1913ad35602c54483c502f72815e4bc2c2f38c6 (diff)
opt-commands: add uwurandom (#112)
* opt-commands: add uwurandom * Apply suggestions from code review Co-authored-by: seth <[email protected]> * opt-commands: set random default value for uwurandom --------- Co-authored-by: seth <[email protected]>
-rw-r--r--Cargo.lock39
-rw-r--r--Cargo.toml1
-rw-r--r--src/commands/optional/mod.rs7
-rw-r--r--src/commands/optional/uwurandom.rs41
4 files changed, 87 insertions, 1 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 75a29ba..97628fc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -263,6 +263,15 @@ dependencies = [
]
[[package]]
+name = "convert_case"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec182b0ca2f35d8fc196cf3404988fd8b8c739a4d270ff118a398feb0cbec1ca"
+dependencies = [
+ "unicode-segmentation",
+]
+
+[[package]]
name = "core-foundation"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1712,6 +1721,7 @@ dependencies = [
"serde_json",
"tokio",
"url",
+ "uwurandom-rs",
]
[[package]]
@@ -2000,6 +2010,12 @@ dependencies = [
]
[[package]]
+name = "unicode-segmentation"
+version = "1.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36"
+
+[[package]]
name = "unicode-width"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2036,6 +2052,29 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
+name = "uwurandom-proc-macros"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "04ffa647cfeda30cf8659b18f9b7d96199d3439f6336ac52621249aca7b5375e"
+dependencies = [
+ "convert_case",
+ "quote",
+ "serde",
+ "serde_json",
+ "syn 1.0.109",
+]
+
+[[package]]
+name = "uwurandom-rs"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d1d88a4d544a6204e9e83517522d7e6dc2007241272e7dfd647b307b41104b4b"
+dependencies = [
+ "rand_core 0.6.4",
+ "uwurandom-proc-macros",
+]
+
+[[package]]
name = "valuable"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index df20b66..cde19f4 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -33,3 +33,4 @@ tokio = { version = "1.35.1", features = [
"signal",
] }
url = { version = "2.5.0", features = ["serde"] }
+uwurandom-rs = "1.1.0"
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<Command<Data, Report>> {
- 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<u16>,
+) -> 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(())
+}