From 4116e0f5fbc9758a60551612c73d897240012a08 Mon Sep 17 00:00:00 2001 From: Jontze <42588836+jontze@users.noreply.github.com> Date: Sat, 4 Feb 2023 15:22:25 +0100 Subject: [PATCH] chore(examples): Show how to extend cadency with custom commands --- Cargo.toml | 3 +- examples/custom_commands/Cargo.toml | 37 ++++++++ .../examples/custom_commands.rs | 89 +++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 examples/custom_commands/Cargo.toml create mode 100644 examples/custom_commands/examples/custom_commands.rs diff --git a/Cargo.toml b/Cargo.toml index d5249f0..fe00f9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,6 @@ members = [ "cadency_codegen", "cadency_commands", "cadency_yt_playlist", - "cadency" + "cadency", + "examples/*" ] diff --git a/examples/custom_commands/Cargo.toml b/examples/custom_commands/Cargo.toml new file mode 100644 index 0000000..67c91c4 --- /dev/null +++ b/examples/custom_commands/Cargo.toml @@ -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"] \ No newline at end of file diff --git a/examples/custom_commands/examples/custom_commands.rs b/examples/custom_commands/examples/custom_commands.rs new file mode 100644 index 0000000..f2a34cf --- /dev/null +++ b/examples/custom_commands/examples/custom_commands.rs @@ -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, +} + +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("".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); + } +}