From e44838f4339b90817b5eba5df16230b02487f0cc Mon Sep 17 00:00:00 2001 From: Illia Date: Sat, 10 Dec 2016 22:25:55 +0200 Subject: More config for CreateCommand, add various methods Adds multiple configurations to the command builder, and adds methods to various structs. Context::get_current_user is a shortcut to retrieve the current user from the cache. Message::get_member retrieves the member object of the message, if sent in a guild. Message::is_private checks if the message was sent in a Group or PrivateChannel. User::member retrieves the user's member object in a guild by Id; Adds 6 configurations to the command builder: - dm_only: whether the command can only be used in direct messages; - guild_only: whether the command can only be used in guilds; - help_available: whether the command should be displayed in the help list; - max_args: specify the maximum number of arguments a command must be given; - min_args: specify the minimum number of arguments a command must be given; - required_permissions: the permissions a member must have to be able to use the command; --- src/client/context.rs | 11 ++++++++- src/client/dispatch.rs | 2 +- src/client/event_store.rs | 54 ++++++++++++++++++++++----------------------- src/client/gateway/prep.rs | 4 ++-- src/client/gateway/shard.rs | 2 +- src/client/mod.rs | 12 +++++----- 6 files changed, 47 insertions(+), 38 deletions(-) (limited to 'src/client') diff --git a/src/client/context.rs b/src/client/context.rs index eb7246b..e646ec6 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -22,7 +22,7 @@ use ::internal::prelude::*; use ::model::*; use ::utils; -#[cfg(feature = "cache")] +#[cfg(feature="cache")] use super::CACHE; /// The context is a general utility struct provided on event dispatches, which @@ -1077,6 +1077,15 @@ impl Context { Ok(channels) } + /// Gets information about the current user. + /// + /// Note this is shorthand for retrieving the current user through the + /// cache, and will perform a clone. + #[cfg(all(feature = "cache", feature = "methods"))] + pub fn get_current_user(&self) -> CurrentUser { + CACHE.read().unwrap().user.clone() + } + /// Gets an [`Guild`]'s emoji by Id. /// /// Requires the [Manage Emojis] permission. diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index 7b0439d..5f61ea9 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -11,7 +11,7 @@ use ::model::{ChannelId, Message}; #[cfg(feature="framework")] use ::ext::framework::Framework; -#[cfg(feature = "cache")] +#[cfg(feature="cache")] use super::CACHE; macro_rules! handler { diff --git a/src/client/event_store.rs b/src/client/event_store.rs index e2ddf94..7cc7e81 100644 --- a/src/client/event_store.rs +++ b/src/client/event_store.rs @@ -14,7 +14,7 @@ use ::model::event::{ }; use ::model::*; -#[cfg(not(feature = "cache"))] +#[cfg(not(feature="cache"))] use ::model::event::{ CallUpdateEvent, GuildMemberUpdateEvent, @@ -43,13 +43,13 @@ use ::model::event::{ #[derive(Default)] pub struct EventStore { pub on_call_create: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_call_delete: Option) + Send + Sync + 'static>>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_call_delete: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_call_update: Option, Option) + Send + Sync + 'static>>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_call_update: Option>, pub on_channel_create: Option>, pub on_channel_delete: Option>, @@ -57,45 +57,45 @@ pub struct EventStore { pub on_channel_pins_update: Option>, pub on_channel_recipient_addition: Option>, pub on_channel_recipient_removal: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_channel_update: Option, Channel) + Send + Sync + 'static>>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_channel_update: Option>, pub on_friend_suggestion_create: Option) + Send + Sync + 'static>>, pub on_friend_suggestion_delete: Option>, pub on_guild_ban_addition: Option>, pub on_guild_ban_removal: Option>, pub on_guild_create: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_guild_delete: Option) + Send + Sync + 'static>>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_guild_delete: Option>, pub on_guild_emojis_update: Option) + Send + Sync + 'static>>, pub on_guild_integrations_update: Option>, pub on_guild_member_addition: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_guild_member_removal: Option) + Send + Sync + 'static>>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_guild_member_removal: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_guild_member_update: Option, Member) + Send + Sync + 'static>>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_guild_member_update: Option>, pub on_guild_members_chunk: Option) + Send + Sync + 'static>>, pub on_guild_role_create: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_guild_role_delete: Option) + Send + Sync + 'static>>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_guild_role_delete: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_guild_role_update: Option, Role) + Send + Sync + 'static>>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_guild_role_update: Option>, pub on_guild_sync: Option>, pub on_guild_unavailable: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_guild_update: Option, PartialGuild) + Send + Sync + 'static>>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_guild_update: Option>, pub on_message: Option>, pub on_message_ack: Option) + Send + Sync + 'static>>, @@ -105,9 +105,9 @@ pub struct EventStore { pub on_reaction_remove: Option>, pub on_reaction_remove_all: Option>, pub on_message_update: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_note_update: Option, String) + Send + Sync + 'static>>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_note_update: Option>, pub on_presence_replace: Option) + Send + Sync + 'static>>, pub on_presence_update: Option>, @@ -117,17 +117,17 @@ pub struct EventStore { pub on_resume: Option>, pub on_typing_start: Option>, pub on_unknown: Option) + Send + Sync + 'static>>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_user_guild_settings_update: Option, UserGuildSettings) + Send + Sync + 'static>>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_user_guild_settings_update: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_user_update: Option>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_user_update: Option>, - #[cfg(feature = "cache")] + #[cfg(feature="cache")] pub on_user_settings_update: Option>, - #[cfg(not(feature = "cache"))] + #[cfg(not(feature="cache"))] pub on_user_settings_update: Option>, pub on_voice_server_update: Option>, pub on_voice_state_update: Option, VoiceState) + Send + Sync + 'static>>, diff --git a/src/client/gateway/prep.rs b/src/client/gateway/prep.rs index 3844a8d..4602ea6 100644 --- a/src/client/gateway/prep.rs +++ b/src/client/gateway/prep.rs @@ -80,12 +80,12 @@ pub fn identify(token: &str, shard_info: Option<[u8; 2]>) -> Value { .build() } -#[cfg(not(feature = "debug"))] +#[cfg(not(feature="debug"))] pub fn identify_compression(object: ObjectBuilder) -> ObjectBuilder { object.insert("compression", true) } -#[cfg(feature = "debug")] +#[cfg(feature="debug")] pub fn identify_compression(object: ObjectBuilder) -> ObjectBuilder { object.insert("compression", false) } diff --git a/src/client/gateway/shard.rs b/src/client/gateway/shard.rs index 232d2c3..c026f0c 100644 --- a/src/client/gateway/shard.rs +++ b/src/client/gateway/shard.rs @@ -69,7 +69,7 @@ pub struct Shard { ws_url: String, /// The voice connections that this Shard is responsible for. The Shard will /// update the voice connections' states. - #[cfg(feature = "voice")] + #[cfg(feature="voice")] pub manager: VoiceManager, } diff --git a/src/client/mod.rs b/src/client/mod.rs index 843bbfe..c9a9c65 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -61,20 +61,20 @@ use ::model::event::{ }; use ::model::*; -#[cfg(feature = "framework")] +#[cfg(feature="framework")] use ::ext::framework::Framework; -#[cfg(feature = "cache")] +#[cfg(feature="cache")] use ::ext::cache::Cache; -#[cfg(not(feature = "cache"))] +#[cfg(not(feature="cache"))] use ::model::event::{ CallUpdateEvent, GuildMemberUpdateEvent, UserSettingsUpdateEvent, }; -#[cfg(feature = "cache")] +#[cfg(feature="cache")] lazy_static! { /// A mutable and lazily-initialized static binding. It can be accessed /// across any function and in any context. @@ -841,7 +841,7 @@ impl Client { } } -#[cfg(feature = "cache")] +#[cfg(feature="cache")] impl Client { /// Attaches a handler for when a [`CallDelete`] is received. /// @@ -1003,7 +1003,7 @@ impl Client { } } -#[cfg(not(feature = "cache"))] +#[cfg(not(feature="cache"))] impl Client { /// Attaches a handler for when a [`CallDelete`] is received. /// -- cgit v1.2.3