From 29480e5eeccc12afc0e9020373647786736aabc7 Mon Sep 17 00:00:00 2001 From: Lakelezz <12222135+Lakelezz@users.noreply.github.com> Date: Sun, 15 Jul 2018 16:48:06 +0200 Subject: Add checks for groups (#349) --- examples/05_command_framework/src/main.rs | 33 +++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'examples/05_command_framework/src') 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); -- cgit v1.2.3