-
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.
chore(examples): Show how to extend cadency with custom commands
- Loading branch information
Showing
3 changed files
with
128 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ members = [ | |
"cadency_codegen", | ||
"cadency_commands", | ||
"cadency_yt_playlist", | ||
"cadency" | ||
"cadency", | ||
"examples/*" | ||
] |
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,37 @@ | ||
[package] | ||
name = "custom_commands" | ||
version = "0.3.0" | ||
edition = "2021" | ||
description = "An example how to integrate custom commands into cadency_rs" | ||
license = "MIT" | ||
repository = "https://github.com/jontze/cadency-rs" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[[example]] | ||
name = "custom_commands" | ||
|
||
[dependencies] | ||
env_logger = "0.10.0" | ||
log = "0.4.17" | ||
|
||
[dependencies.serenity] | ||
version = "0.11.5" | ||
default-features = false | ||
features = ["client", "gateway", "rustls_backend", "model", "voice", "cache"] | ||
|
||
[dependencies.cadency_core] | ||
path = "../../cadency_core" | ||
version = "0.3.0" | ||
|
||
[dependencies.cadency_codegen] | ||
path = "../../cadency_codegen" | ||
version = "0.3.0" | ||
|
||
[dependencies.cadency_commands] | ||
path = "../../cadency_commands" | ||
version = "0.3.0" | ||
|
||
[dependencies.tokio] | ||
version = "1.25.0" | ||
features = ["macros", "rt-multi-thread"] |
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,89 @@ | ||
#[macro_use] | ||
extern crate log; | ||
#[macro_use] | ||
extern crate cadency_codegen; | ||
|
||
use cadency_commands::Fib; | ||
use cadency_core::{ | ||
setup_commands, utils, Cadency, CadencyCommand, CadencyCommandOption, CadencyError, | ||
}; | ||
use serenity::{ | ||
async_trait, | ||
client::Context, | ||
model::application::{ | ||
command::CommandOptionType, | ||
interaction::application_command::{ApplicationCommandInteraction, CommandDataOptionValue}, | ||
}, | ||
}; | ||
|
||
// This is your custom command with the name "hello" | ||
#[derive(CommandBaseline)] | ||
struct Hello { | ||
// Description of the command in the discord UI | ||
description: String, | ||
// The allowed list of command arguments | ||
options: Vec<CadencyCommandOption>, | ||
} | ||
|
||
impl std::default::Default for Hello { | ||
fn default() -> Self { | ||
Self { | ||
description: "Say Hello to a user".to_string(), | ||
options: vec![CadencyCommandOption { | ||
name: "user", | ||
description: "The number in the fibonacci sequence", | ||
kind: CommandOptionType::User, | ||
required: true, | ||
}], | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl CadencyCommand for Hello { | ||
// The following code will get executed by the cadency command handler if the command is called | ||
#[command] | ||
async fn execute<'a>( | ||
&self, | ||
ctx: &Context, | ||
command: &'a mut ApplicationCommandInteraction, | ||
) -> Result<(), CadencyError> { | ||
let user_arg = utils::get_option_value_at_position(command.data.options.as_ref(), 0) | ||
.and_then(|option_value| { | ||
if let CommandDataOptionValue::User(user, _) = option_value { | ||
Some(user) | ||
} else { | ||
error!("Command argument is not a user"); | ||
None | ||
} | ||
}) | ||
.expect("A user as command argument"); | ||
utils::create_response(ctx, command, &format!("**Hello {user_arg}!**",)).await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
// Setup info log level | ||
let env = env_logger::Env::default().filter_or("RUST_LOG", "cadency=info"); | ||
env_logger::init_from_env(env); | ||
|
||
// Setup an array of all commands for the discord bot | ||
// The "Fib" command is imported from the cadency commands library. | ||
// The "Hello" command is your own custom command. | ||
let commands = setup_commands![Fib::default(), Hello::default()]; | ||
|
||
// Init cadency with a valid discord bot token | ||
let mut cadency = Cadency::new("<your_discord_bot_token>".to_string()) | ||
.await | ||
.expect("To init cadency") | ||
// Add the commands array to cadency | ||
.with_commands(commands) | ||
.await; | ||
|
||
// Start cadency - this will submit and register the commands to discord | ||
if let Err(why) = cadency.start().await { | ||
error!("Client error: {:?}", why); | ||
} | ||
} |