diff options
| author | Nathan Perry <avaglir@gmail.com> | 2018-06-01 06:44:41 -0400 |
|---|---|---|
| committer | Nathan Perry <avaglir@gmail.com> | 2018-06-01 06:44:41 -0400 |
| commit | 21ce509ef21eb8f21c68173005f4f8fe537f7443 (patch) | |
| tree | 6e9f4ae02ed632d6d75031c9dd3d8270a049f7b4 | |
| parent | 413384896f66aeb2737f5f60433c2467d867fb36 (diff) | |
add a roll command, relax semver requirements on dependencies
| -rw-r--r-- | Cargo.lock | 12 | ||||
| -rw-r--r-- | Cargo.toml | 14 | ||||
| -rw-r--r-- | src/commands/mod.rs | 5 | ||||
| -rw-r--r-- | src/commands/roll.rs | 60 |
4 files changed, 78 insertions, 13 deletions
@@ -996,19 +996,19 @@ dependencies = [ [[package]] name = "rand" -version = "0.5.0-pre.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", - "rand_core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "rand_core" -version = "0.1.0" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -1423,7 +1423,7 @@ dependencies = [ "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "mime_guess 1.8.4 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.5.0-pre.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.8.5 (registry+https://github.com/rust-lang/crates.io-index)", "serenity 0.5.2 (git+https://github.com/mammothbane/serenity)", @@ -1876,8 +1876,8 @@ dependencies = [ "checksum r2d2 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f9078ca6a8a5568ed142083bb2f7dc9295b69d16f867ddcc9849e51b17d8db46" "checksum rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "15a732abf9d20f0ad8eeb6f909bf6868722d9a06e1e50802b6a70351f40b4eb1" "checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" -"checksum rand 0.5.0-pre.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7d7a7728c20bfd9fcc6e713e748e787c3d00e5ffd139b3ad1b5be92c5dfbaad5" -"checksum rand_core 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0224284424a4b818387b58d59336c288f99b48f69681aa60cc681fe038bbca5d" +"checksum rand 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a89abf8d34faf9783692392dca7bcdc6e82fa84eca86ccb6301ec87f3497185" +"checksum rand_core 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1b7a5f27547c49e5ccf8a586db3f3782fd93cf849780b21853b9d981db203302" "checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" @@ -10,16 +10,16 @@ default = ["diesel"] lazy_static = "1.0" failure = "0.1" log = "0.4" -typemap = "0.3.3" -url = "1.6.0" -dotenv = "0.11.0" -dotenv_codegen = "0.11.0" -chrono = "0.4.1" +typemap = "0.3" +url = "1.6" +dotenv = "0.11" +dotenv_codegen = "0.11" +chrono = "0.4" fern = { version = "0.5", features = ["colored"] } diesel = { version = "1.1", features = ["postgres", "chrono", "r2d2"], optional = true } ctrlc = { version = "3.0", features = ["termination"] } -rand = "0.5.0-pre.0" -either = "1.5.0" +rand = "0.5" +either = "1.5" reqwest = "0.8" sha1 = { version = "0.6.0", features = ["std"] } mime_guess = "1.8.4" diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 80581d5..4d7b4ae 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -8,6 +8,7 @@ use std::time::Duration; mod playback; mod sound; +mod roll; pub use self::sound::*; pub use self::playback::*; @@ -53,6 +54,10 @@ pub fn register_commands(f: StandardFramework) -> StandardFramework { .desc("set playback volume") .guild_only(true) .exec(volume)) + .command("roll", |c| c + .desc("simulate rolling dice") + .guild_only(true) + .exec(roll::roll)) .unrecognised_command(|ctx, msg, unrec| { let url = match msg.content.split_whitespace().skip(1).next() { Some(x) if x.starts_with("http") => x, diff --git a/src/commands/roll.rs b/src/commands/roll.rs new file mode 100644 index 0000000..fb5c71a --- /dev/null +++ b/src/commands/roll.rs @@ -0,0 +1,60 @@ +use serenity::prelude::*; +use serenity::framework::standard::Args; +use serenity::model::channel::Message; +use regex::Regex; +use rand::prelude::*; + +use Result; + +use super::send; + +lazy_static! { + static ref ROLL_REGEX: Regex = Regex::new(r"([0-9]+)?(?:d([0-9]+)(?:\s+\+\s+([0-9]+))?)") + .expect("error parsing roll regex"); +} + +pub fn roll(_ctx: &mut Context, msg: &Message, args: Args) -> Result<()> { + let captures = match ROLL_REGEX.captures(args.full()) { + Some(captures) => captures, + None => return send(msg.channel_id, "conway is a goldfish", msg.tts), + }; + + let dice_count = match captures.get(1) { + Some(x) => { + match x.as_str().parse::<usize>() { + Ok(x) => x, + Err(e) => { + send(msg.channel_id, "conway is a goldfish", msg.tts)?; + return Err(e.into()); + }, + } + }, + None => 1, + }; + + if dice_count > 1000000 { + send(msg.channel_id, "no.", msg.tts)?; + return Ok(()); + } + + let faces = match captures.get(2).unwrap().as_str().parse::<usize>() { + Ok(faces) => faces, + Err(e) => { + send(msg.channel_id, "conway is a goldfish", msg.tts)?; + return Err(e.into()) + }, + }; + + let adjust = match captures.get(3).map(|adjust| adjust.as_str().parse::<usize>()).transpose() { + Ok(adjust) => adjust.unwrap_or(0), + Err(e) => { + send(msg.channel_id, "conway is a goldfish", msg.tts)?; + return Err(e.into()) + }, + }; + + let mut rng = thread_rng(); + let total = (0..dice_count).map(|_| rng.gen_range(0, faces)).sum::<usize>() + adjust + 1; + + send(msg.channel_id, &format!("{}", total), msg.tts) +} |
