Skip to content

Commit

Permalink
Merge pull request #136 from jontze/feat/roll-dice
Browse files Browse the repository at this point in the history
Add command to roll a dice of n sides
  • Loading branch information
jontze authored Dec 19, 2023
2 parents f0de19b + 123ab4c commit 759e7af
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
5 changes: 3 additions & 2 deletions cadency/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate log;
extern crate cadency_core;

use cadency_commands::{
Fib, Inspire, Now, Pause, Ping, Play, Resume, Skip, Slap, Stop, TrackLoop, Tracks, Urban,
Fib, Inspire, Now, Pause, Ping, Play, Resume, Roll, Skip, Slap, Stop, TrackLoop, Tracks, Urban,
};
use cadency_core::Cadency;
use settings::CadencySettings;
Expand Down Expand Up @@ -34,7 +34,8 @@ async fn main() {
Stop::default(),
Tracks::default(),
Urban::default(),
TrackLoop::default()
TrackLoop::default(),
Roll::default()
];
let cadency = Cadency::builder()
.token(std::env::var("DISCORD_TOKEN").expect("Discord token to be present"))
Expand Down
1 change: 1 addition & 0 deletions cadency_commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ songbird = { workspace = true }
tokio = { workspace = true }
reqwest = { workspace = true }
serde = { workspace = true }
rand = "0.8.5"

[dependencies.cadency_core]
path = "../cadency_core"
Expand Down
2 changes: 2 additions & 0 deletions cadency_commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ mod urban;
pub use urban::Urban;
mod track_loop;
pub use track_loop::TrackLoop;
mod roll;
pub use roll::Roll;

#[cfg(test)]
mod test {
Expand Down
36 changes: 36 additions & 0 deletions cadency_commands/src/roll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use cadency_core::{
response::{Response, ResponseBuilder},
CadencyCommand, CadencyError,
};
use rand::Rng;
use serenity::{async_trait, client::Context, model::application::CommandInteraction};

#[derive(CommandBaseline, Default)]
#[description = "Roll a dice of n sides"]
#[argument(
name = "sides",
description = "The number of sides on the dice",
kind = "Integer"
)]
pub struct Roll {}

impl Roll {
fn roll_dice(&self, sides: &i64) -> i64 {
let mut rng = rand::thread_rng();
rng.gen_range(1..=*sides) as i64
}
}

#[async_trait]
impl CadencyCommand for Roll {
async fn execute<'a>(
&self,
_ctx: &Context,
command: &'a mut CommandInteraction,
response_builder: &'a mut ResponseBuilder,
) -> Result<Response, CadencyError> {
let roll = self.roll_dice(&self.arg_sides(command));
let roll_msg = format!("**:dice_cube: You rolled a `{roll}`**");
Ok(response_builder.message(Some(roll_msg)).build()?)
}
}

0 comments on commit 759e7af

Please sign in to comment.