diff options
| author | Lakelezz <[email protected]> | 2018-07-15 16:48:06 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2018-07-15 16:48:06 +0200 |
| commit | 29480e5eeccc12afc0e9020373647786736aabc7 (patch) | |
| tree | 347ade11861894d95de87274e5c534d6795b282b /examples/05_command_framework/src | |
| parent | typos (diff) | |
| download | serenity-29480e5eeccc12afc0e9020373647786736aabc7.tar.xz serenity-29480e5eeccc12afc0e9020373647786736aabc7.zip | |
Add checks for groups (#349)
Diffstat (limited to 'examples/05_command_framework/src')
| -rw-r--r-- | examples/05_command_framework/src/main.rs | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/examples/05_command_framework/src/main.rs b/examples/05_command_framework/src/main.rs index ca16f23..afec234 100644 --- a/examples/05_command_framework/src/main.rs +++ b/examples/05_command_framework/src/main.rs @@ -186,13 +186,21 @@ fn main() { .command("latency", |c| c .cmd(latency)) .command("ping", |c| c - .check(owner_check) + .check(owner_check) // User needs to pass this test to run command .cmd(ping)) .command("role", |c| c .cmd(about_role) // Limits the usage of this command to roles named: .allowed_roles(vec!["mods", "ultimate neko"])) - .command("some long command", |c| c.cmd(some_long_command)), + .command("some long command", |c| c.cmd(some_long_command)) + .group("Owner", |g| g + // This check applies to every command on this group. + // User needs to pass the test for the command to execute. + .check(admin_check) + .command("am i admin", |c| c + .cmd(am_i_admin)) + .guild_only(true) + ), ); if let Err(why) = client.start() { @@ -231,6 +239,21 @@ fn owner_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) msg.author.id == 7 } +// A function which acts as a "check", to determine whether to call a command. +// +// This check analyses whether a guild member permissions has +// administrator-permissions. +fn admin_check(_: &mut Context, msg: &Message, _: &mut Args, _: &CommandOptions) -> bool { + if let Some(member) = msg.member() { + + if let Ok(permissions) = member.permissions() { + return permissions.administrator(); + } + } + + false +} + command!(some_long_command(_ctx, msg, args) { if let Err(why) = msg.channel_id.say(&format!("Arguments: {:?}", args)) { println!("Error sending message: {:?}", why); @@ -333,6 +356,12 @@ command!(ping(_ctx, msg, _args) { } }); +command!(am_i_admin(_ctx, msg, _args) { + if let Err(why) = msg.channel_id.say("Yes you are.") { + println!("Error sending message: {:?}", why); + } +}); + command!(dog(_ctx, msg, _args) { if let Err(why) = msg.channel_id.say(":dog:") { println!("Error sending message: {:?}", why); |