Skip to content

Commit

Permalink
0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Geobert Quach committed Aug 7, 2020
1 parent b4d04cd commit 7b4cbe1
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 23 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.3.0
- Update to `caith` 0.3.0
- Added alias `r` for `roll`
- Added `reroll` command with `rr` alias

# 0.2.0
- Update to `caith` 0.2.0
- Better error feedback
Expand Down
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
[package]
name = "disle"
version = "0.2.0"
version = "0.3.0"
authors = ["Geobert Quach <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "0.2.22", features = ["macros"]}
caith = "0.2.0"
tokio = { version = "0.2.22", features = ["macros", "sync"]}
caith = "0.3.0"
once_cell = "1.4.0"

[dependencies.serenity]
git = "https://github.com/acdenisSK/serenity"
Expand Down
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,36 @@ You need to host the bot yourself.

# Usage

See the underlying crate `caith`'s [Readme for the syntax](https://github.com/Geobert/caith/blob/master/README.md)
```
/roll xdy [OPTIONS][TARGET][FAILURE][REASON]
(or "/r" for short)
rolls x dices of y sides
/reroll (or /rr)
reroll the last roll of the user
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
Target:
t# : Target value to be a success
Failure:
f# : Value under which it is count as failuer
Reason:
! : Any text after `!` will be a comment
```

See the underlying crate `caith`'s [Readme for the full syntax](https://github.com/Geobert/caith/blob/master/README.md)

94 changes: 78 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use std::{collections::HashSet, env};
use std::{
collections::{HashMap, HashSet},
env,
sync::Mutex,
};

use caith::RollResult;
use once_cell::sync::Lazy;
use serenity::{
client::Context,
framework::{
Expand All @@ -15,9 +21,12 @@ use serenity::{
};

#[group]
#[commands(roll)]
#[commands(roll, reroll)]
struct Roll;

static REROLL_TABLE: Lazy<Mutex<HashMap<String, String>>> =
Lazy::new(|| Mutex::new(HashMap::new()));

#[hook]
async fn dispatch_error(ctx: &Context, msg: &Message, error: DispatchError) {
if let DispatchError::Ratelimited(seconds) = error {
Expand All @@ -33,10 +42,15 @@ async fn dispatch_error(ctx: &Context, msg: &Message, error: DispatchError) {

fn get_help_msg() -> String {
r#"```
/roll xdy [OPTIONS]
/roll xdy [OPTIONS][TARGET][FAILURE][REASON]
(or "/r" for short)
rolls x dices of y sides
/reroll (or /rr)
reroll the last roll of the user
Options:
+ - / * : modifiers
e# : Explode value
Expand All @@ -47,35 +61,83 @@ fn get_help_msg() -> String {
d# : Dropping the lowest (lowercase "d")
r# : Reroll if <= value
ir# : Indefinite reroll if <= value
Target:
t# : Target value to be a success
Failure:
f# : Value under which it is count as failuer
Reason:
! : Any text after `!` will be a comment
```"#
.to_string()
}

async fn process_roll(
input: &str,
ctx: &Context,
msg: &Message,
) -> Result<(String, RollResult), String> {
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());
{
let mut reroll_table = REROLL_TABLE.lock().unwrap();
reroll_table.insert(msg.author.to_string(), input.to_owned());
}
Ok((name, res))
}
Err(err) => match err {
caith::RollError::ParseError(_) => Err(format!("Error:\n```\n{}\n```", err)),
caith::RollError::ParamError(err) => Err(format!("Error: {}", err)),
},
}
}

#[command]
#[help_available]
#[aliases("r")]
#[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.starts_with("help") {
get_help_msg()
} else {
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),
match process_roll(input, ctx, msg).await {
Ok((name, res)) => format!("{} roll: {}", name, res),
Err(msg) => msg,
}
};

if let Err(e) = msg.channel_id.say(&ctx.http, msg_to_send).await {
eprintln!("Error sending message: {:?}", e);
}
Ok(())
}

#[command]
#[aliases("rr")]
#[description("Reroll last roll")]
async fn reroll(ctx: &Context, msg: &Message, args: Args) -> CommandResult {
let input = args.rest();
let msg_to_send = if input.starts_with("help") {
get_help_msg()
} else {
let input = {
let reroll_table = REROLL_TABLE.lock().unwrap();
reroll_table.get(&msg.author.to_string()).cloned()
};
match input {
Some(input) => match process_roll(&input, ctx, msg).await {
Ok((name, res)) => format!("{} reroll `{}`: {}", name, input, res),
Err(msg) => msg,
},
None => "No previous roll".to_owned(),
}
};

Expand Down

0 comments on commit 7b4cbe1

Please sign in to comment.