diff options
| author | Lakelezz <[email protected]> | 2017-09-06 18:34:48 +0200 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-09-06 09:34:48 -0700 |
| commit | 01867549709ef73ee09ed442e1d5ea938fd7f74d (patch) | |
| tree | b44c762114b38b2cf80affd98819c53e631fa44e /examples/05_command_framework | |
| parent | Update `Guild::ban` to use `BanOptions` (diff) | |
| download | serenity-01867549709ef73ee09ed442e1d5ea938fd7f74d.tar.xz serenity-01867549709ef73ee09ed442e1d5ea938fd7f74d.zip | |
Add more info on methods in framework example
Add `delimiters()`, `role_by_name()` and `allowed_roles()` to example 05.
Diffstat (limited to 'examples/05_command_framework')
| -rw-r--r-- | examples/05_command_framework/src/main.rs | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/examples/05_command_framework/src/main.rs b/examples/05_command_framework/src/main.rs index d987628..a48aae1 100644 --- a/examples/05_command_framework/src/main.rs +++ b/examples/05_command_framework/src/main.rs @@ -38,8 +38,9 @@ impl EventHandler for Handler { fn main() { // Configure the client with your Discord bot token in the environment. - let token = env::var("DISCORD_TOKEN") - .expect("Expected a token in the environment"); + let token = env::var("DISCORD_TOKEN").expect( + "Expected a token in the environment", + ); let mut client = Client::new(&token, Handler); { @@ -65,7 +66,17 @@ fn main() { .configure(|c| c .allow_whitespace(true) .on_mention(true) - .prefix("~")) + .prefix("~") + // You can set multiple delimiters via delimiters() + // or just one via delimiter(",") + // If you set multiple delimiters, the order you list them + // decides their priority (from first to last). + // + // In this case, if "," would be first, a message would never + // be delimited at ", ", forcing you to trim your arguments if you + // want to avoid whitespaces at the start of each. + .delimiters(vec![", ", ","])) + // Set a function to be called prior to each command execution. This // provides the context of the command, the message that was received, // and the full name of the command that will be called. @@ -133,7 +144,12 @@ fn main() { .command("ping", |c| c .check(owner_check) .exec_str("Pong!")) - .command("some long command", |c| c.exec(some_long_command))); + .command("role", |c| c + .exec(about_role) + // Limits the usage of this command to roles named: + .allowed_roles(vec!["mods", "ultimate neko"])) + .command("some long command", |c| c.exec(some_long_command)), + ); if let Err(why) = client.start() { println!("Client error: {:?}", why); @@ -177,6 +193,27 @@ command!(some_long_command(_ctx, msg, args) { } }); +command!(about_role(_ctx, msg, args) { + let potential_role_name = args.full(); + + if let Some(guild) = msg.guild() { + // `role_by_name()` allows us to attempt attaining a reference to a role + // via its name. + if let Some(role) = guild.read().unwrap().role_by_name(&potential_role_name) { + if let Err(why) = msg.channel_id.say(&format!("Role-ID: {}", role.id)) { + println!("Error sending message: {:?}", why); + } + + return Ok(()); + } + } + + if let Err(why) = msg.channel_id.say( + &format!("Could not find role named: {:?}", potential_role_name)) { + println!("Error sending message: {:?}", why); + } +}); + // Using the `command!` macro, commands can be created with a certain type of // "dynamic" type checking. This is a method of requiring that the arguments // given match the required type, and maps those arguments to the specified |