From be28e5d50d71f86d5058747ddfa4ffaac115d91f Mon Sep 17 00:00:00 2001 From: Geobert Quach Date: Thu, 6 Aug 2020 14:46:34 +0100 Subject: [PATCH] update to caith 0.2.0 and use the new stuff --- CHANGELOG.md | 7 ++++++ Cargo.lock | 6 ++--- Cargo.toml | 4 +-- src/main.rs | 70 ++++++++++++++++++++++++++++++---------------------- 4 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b266cf8 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,7 @@ +# 0.2.0 +- Update to `caith` 0.2.0 + - Better error feedback + - Accept uppercase `D` + +# 0.1.0 +- Initial release \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 39dadee..82ed970 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -130,9 +130,9 @@ checksum = "0e4cec68f03f32e44924783795810fa50a7035d8c8ebe78580ad7e6c703fba38" [[package]] name = "caith" -version = "0.1.4" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e655ba2f87a4ba62b42e07a248effb55a41aa02e565b43b488a9836d78393b0b" +checksum = "9a7ee0dcefded04b82cfa57bf47db445fa6cb39160c1f168cd4deeb16f305697" dependencies = [ "once_cell", "pest", @@ -194,7 +194,7 @@ dependencies = [ [[package]] name = "disle" -version = "0.1.0" +version = "0.2.0" dependencies = [ "caith", "serenity", diff --git a/Cargo.toml b/Cargo.toml index 2d1bff4..ea89531 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "disle" -version = "0.1.0" +version = "0.2.0" authors = ["Geobert Quach "] edition = "2018" @@ -8,7 +8,7 @@ edition = "2018" [dependencies] tokio = { version = "0.2.22", features = ["macros"]} -caith = "0.1.4" +caith = "0.2.0" [dependencies.serenity] git = "https://github.com/acdenisSK/serenity" diff --git a/src/main.rs b/src/main.rs index 4bf4ed3..f6c99bc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::{collections::HashSet, env}; + use serenity::{ client::Context, framework::{ @@ -11,7 +13,6 @@ use serenity::{ model::channel::Message, Client, }; -use std::{collections::HashSet, env}; #[group] #[commands(roll)] @@ -30,41 +31,52 @@ async fn dispatch_error(ctx: &Context, msg: &Message, error: DispatchError) { } } +fn get_help_msg() -> String { + r#"``` + /roll xdy [OPTIONS] + + rolls x dices of y sides + + Options: + + - / * : modifiers + e# : Explode value + ie# : Indefinite explode value + K# : Keeping # highest (upperacse "K") + k# : Keeping # lowest (lowercase "k") + D# : Dropping the highest (uppercase "D") + d# : Dropping the lowest (lowercase "d") + r# : Reroll if <= value + ir# : Indefinite reroll if <= value + t# : Target value to be a success + f# : Value under which it is count as failuer + ! : Any text after `!` will be a comment + ```"# + .to_string() +} + #[command] #[help_available] #[min_args(1)] #[description("Roll dice(s)")] async fn roll(ctx: &Context, msg: &Message, args: Args) -> CommandResult { let input = args.rest(); - let msg_to_send = if input.trim().starts_with("help") { - r#"``` -/roll xdy [OPTIONS] - -rolls x dices of y sides - -Options: -+ - / * : modifiers -e# : Explode value -ie# : Indefinite explode value -K# : Keeping # highest (upperacse "K") -k# : Keeping # lowest (lowercase "k") -D# : Dropping the highest (uppercase "D") -d# : Dropping the lowest (lowercase "d") -r# : Reroll if <= value -ir# : Indefinite reroll if <= value -t# : Target value to be a success -f# : Value under which it is count as failuer -! : Any text after `!` will be a comment -```"# - .to_string() + let msg_to_send = if input.starts_with("help") { + get_help_msg() } else { - let res = caith::roll(input)?; - let name = msg - .author - .nick_in(&ctx.http, msg.guild_id.unwrap()) - .await - .unwrap_or_else(|| msg.author.name.to_owned()); - format!("{} roll: {}", name, res) + match caith::roll(input) { + Ok(res) => { + let name = msg + .author + .nick_in(&ctx.http, msg.guild_id.unwrap()) + .await + .unwrap_or_else(|| msg.author.name.to_owned()); + format!("{} roll: {}", name, res) + } + Err(err) => match err { + caith::RollError::ParseError(_) => format!("Error:\n```\n{}\n```", err), + caith::RollError::ParamError(err) => format!("Error: {}", err), + }, + } }; if let Err(e) = msg.channel_id.say(&ctx.http, msg_to_send).await {