-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from jontze/feat/roll-dice
Add command to roll a dice of n sides
- Loading branch information
Showing
4 changed files
with
42 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()?) | ||
} | ||
} |