aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2016-11-19 08:48:58 -0800
committerAustin Hellyer <[email protected]>2016-11-19 08:48:58 -0800
commit7f24c706b36e8815c1d4f47de5257466cc281571 (patch)
tree064653c56fc42d7fdac963120c4206b4151caa76 /src/client
parentDon't send embed on message edits if empty (diff)
downloadserenity-7f24c706b36e8815c1d4f47de5257466cc281571.tar.xz
serenity-7f24c706b36e8815c1d4f47de5257466cc281571.zip
Fix cond. compile across multiple feature targets
Fixes conditional compilation across multiple combinations of feature targets, where it was assumed a second feature would be enabled by something that requires a feature to be enabled. This also fixes an EOF compilation error on no-feature builds.
Diffstat (limited to 'src/client')
-rw-r--r--src/client/context.rs22
-rw-r--r--src/client/dispatch.rs67
-rw-r--r--src/client/event_store.rs14
-rw-r--r--src/client/mod.rs168
4 files changed, 180 insertions, 91 deletions
diff --git a/src/client/context.rs b/src/client/context.rs
index 9103e3a..cd3468f 100644
--- a/src/client/context.rs
+++ b/src/client/context.rs
@@ -757,7 +757,7 @@ impl Context {
let guild_id = guild_id.into();
let role_id = role_id.into();
- let map = feature_state! {{
+ feature_state! {{
let state = STATE.lock().unwrap();
let role = if let Some(role) = {
@@ -768,12 +768,14 @@ impl Context {
return Err(Error::Client(ClientError::RecordNotFound));
};
- f(EditRole::new(role)).0.build()
+ let map = f(EditRole::new(role)).0.build();
+
+ return http::edit_role(guild_id.0, role_id.0, map);
} else {
- f(EditRole::default()).0.build()
- }};
+ let map = f(EditRole::default()).0.build();
- http::edit_role(guild_id.0, role_id.0, map)
+ return http::edit_role(guild_id.0, role_id.0, map);
+ }}
}
/// Edit a message given its Id and the Id of the channel it belongs to.
@@ -1233,11 +1235,15 @@ impl Context {
/// [`OnlineStatus`]: ../model/enum.OnlineStatus.html
/// [`Playing`]: ../model/enum.GameType.html#variant.Playing
pub fn set_game_name(&self, game: Option<&str>) {
+ let game = game.map(|x| Game {
+ kind: GameType::Playing,
+ name: x.to_owned(),
+ url: None,
+ });
+
self.connection.lock()
.unwrap()
- .set_presence(game.map(|x| Game::playing(x.to_owned())),
- OnlineStatus::Online,
- false);
+ .set_presence(game, OnlineStatus::Online, false);
}
pub fn set_presence(&self,
diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs
index 9aff46f..8798723 100644
--- a/src/client/dispatch.rs
+++ b/src/client/dispatch.rs
@@ -122,19 +122,20 @@ fn handle_event(event: Event,
},
Event::CallDelete(event) => {
if let Some(ref handler) = handler!(on_call_delete, event_store) {
- let call = STATE
- .lock()
- .unwrap()
- .calls
- .remove(&event.channel_id);
- update!(update_with_call_delete, event);
-
let context = context(None, conn, login_type);
let handler = handler.clone();
- thread::spawn(move || {
- (handler)(context, call);
- });
+ feature_state! {{
+ let call = update!(update_with_call_delete, event);
+
+ thread::spawn(move || {
+ (handler)(context, event.channel_id, call);
+ });
+ } else {
+ thread::spawn(move || {
+ (handler)(context, event.channel_id);
+ });
+ }}
} else {
update!(update_with_call_delete, event);
}
@@ -249,18 +250,25 @@ fn handle_event(event: Event,
},
Event::ChannelUpdate(event) => {
if let Some(ref handler) = handler!(on_channel_update, event_store) {
- let before = STATE.lock()
- .unwrap()
- .find_channel(event.channel.id());
- update!(update_with_channel_update, event);
let context = context(Some(event.channel.id()),
conn,
login_type);
let handler = handler.clone();
- thread::spawn(move || {
- (handler)(context, before, event.channel);
- });
+ feature_state! {{
+ let before = STATE.lock()
+ .unwrap()
+ .find_channel(event.channel.id());
+ update!(update_with_channel_update, event);
+
+ thread::spawn(move || {
+ (handler)(context, before, event.channel);
+ });
+ } else {
+ thread::spawn(move || {
+ (handler)(context, event.channel);
+ });
+ }}
} else {
update!(update_with_channel_update, event);
}
@@ -516,18 +524,25 @@ fn handle_event(event: Event,
},
Event::GuildUpdate(event) => {
if let Some(ref handler) = handler!(on_guild_update, event_store) {
- let before = STATE.lock()
- .unwrap()
- .guilds
- .get(&event.guild.id)
- .cloned();
- update!(update_with_guild_update, event);
let context = context(None, conn, login_type);
let handler = handler.clone();
- thread::spawn(move || {
- (handler)(context, before, event.guild);
- });
+ feature_state! {{
+ let before = STATE.lock()
+ .unwrap()
+ .guilds
+ .get(&event.guild.id)
+ .cloned();
+ update!(update_with_guild_update, event);
+
+ thread::spawn(move || {
+ (handler)(context, before, event.guild);
+ });
+ } else {
+ thread::spawn(move || {
+ (handler)(context, event.guild);
+ });
+ }}
} else {
update!(update_with_guild_update, event);
}
diff --git a/src/client/event_store.rs b/src/client/event_store.rs
index 1693a73..9558ba1 100644
--- a/src/client/event_store.rs
+++ b/src/client/event_store.rs
@@ -26,7 +26,10 @@ use ::model::*;
#[derive(Default)]
pub struct EventStore {
pub on_call_create: Option<Arc<Fn(Context, Call) + Send + Sync + 'static>>,
- pub on_call_delete: Option<Arc<Fn(Context, Option<Call>) + Send + Sync + 'static>>,
+ #[cfg(feature = "state")]
+ pub on_call_delete: Option<Arc<Fn(Context, ChannelId, Option<Call>) + Send + Sync + 'static>>,
+ #[cfg(not(feature = "state"))]
+ pub on_call_delete: Option<Arc<Fn(Context, ChannelId) + Send + Sync + 'static>>,
#[cfg(feature = "state")]
pub on_call_update: Option<Arc<Fn(Context, Option<Call>, Option<Call>) + Send + Sync + 'static>>,
#[cfg(not(feature = "state"))]
@@ -37,7 +40,10 @@ pub struct EventStore {
pub on_channel_pins_update: Option<Arc<Fn(Context, ChannelPinsUpdateEvent) + Send + Sync + 'static>>,
pub on_channel_recipient_addition: Option<Arc<Fn(Context, ChannelId, User) + Send + Sync + 'static>>,
pub on_channel_recipient_removal: Option<Arc<Fn(Context, ChannelId, User) + Send + Sync + 'static>>,
+ #[cfg(feature = "state")]
pub on_channel_update: Option<Arc<Fn(Context, Option<Channel>, Channel) + Send + Sync + 'static>>,
+ #[cfg(not(feature = "state"))]
+ pub on_channel_update: Option<Arc<Fn(Context, Channel) + Send + Sync + 'static>>,
pub on_friend_suggestion_create: Option<Arc<Fn(Context, User, Vec<SuggestionReason>) + Send + Sync + 'static>>,
pub on_friend_suggestion_delete: Option<Arc<Fn(Context, UserId) + Send + Sync + 'static>>,
pub on_guild_ban_addition: Option<Arc<Fn(Context, GuildId, User) + Send + Sync + 'static>>,
@@ -70,7 +76,10 @@ pub struct EventStore {
pub on_guild_role_update: Option<Arc<Fn(Context, GuildId, Role) + Send + Sync + 'static>>,
pub on_guild_sync: Option<Arc<Fn(Context, GuildSyncEvent) + Send + Sync + 'static>>,
pub on_guild_unavailable: Option<Arc<Fn(Context, GuildId) + Send + Sync + 'static>>,
+ #[cfg(feature = "state")]
pub on_guild_update: Option<Arc<Fn(Context, Option<LiveGuild>, Guild) + Send + Sync + 'static>>,
+ #[cfg(not(feature = "state"))]
+ pub on_guild_update: Option<Arc<Fn(Context, Guild) + Send + Sync + 'static>>,
pub on_message: Option<Arc<Fn(Context, Message) + Send + Sync + 'static>>,
pub on_message_ack: Option<Arc<Fn(Context, ChannelId, Option<MessageId>) + Send + Sync + 'static>>,
pub on_message_delete: Option<Arc<Fn(Context, ChannelId, MessageId) + Send + Sync + 'static>>,
@@ -91,7 +100,10 @@ pub struct EventStore {
pub on_resume: Option<Arc<Fn(Context, ResumedEvent) + Send + Sync + 'static>>,
pub on_typing_start: Option<Arc<Fn(Context, TypingStartEvent) + Send + Sync + 'static>>,
pub on_unknown: Option<Arc<Fn(Context, String, BTreeMap<String, Value>) + Send + Sync + 'static>>,
+ #[cfg(feature = "state")]
pub on_user_guild_settings_update: Option<Arc<Fn(Context, Option<UserGuildSettings>, UserGuildSettings) + Send + Sync + 'static>>,
+ #[cfg(not(feature = "state"))]
+ pub on_user_guild_settings_update: Option<Arc<Fn(Context, UserGuildSettings) + Send + Sync + 'static>>,
#[cfg(feature = "state")]
pub on_user_update: Option<Arc<Fn(Context, CurrentUser, CurrentUser) + Send + Sync + 'static>>,
#[cfg(not(feature = "state"))]
diff --git a/src/client/mod.rs b/src/client/mod.rs
index 44e951b..3dac20d 100644
--- a/src/client/mod.rs
+++ b/src/client/mod.rs
@@ -460,16 +460,6 @@ impl Client {
.on_call_create = Some(Arc::new(handler));
}
- /// Attaches a handler for when a [`CallDelete`] is received.
- ///
- /// [`CallDelete`]: ../model/enum.Event.html#variant.CallDelete
- pub fn on_call_delete<F>(&mut self, handler: F)
- where F: Fn(Context, Option<Call>) + Send + Sync + 'static {
- self.event_store.lock()
- .unwrap()
- .on_call_delete = Some(Arc::new(handler));
- }
-
/// Attaches a handler for when a [`ChannelCreate`] is received.
///
/// [`ChannelCreate`]: ../model/enum.Event.html#variant.ChannelCreate
@@ -510,16 +500,6 @@ impl Client {
.on_channel_pins_update = Some(Arc::new(handler));
}
- /// Attaches a handler for when a [`ChannelUpdate`] is received.
- ///
- /// [`ChannelUpdate`]: ../model/enum.Event.html#variant.ChannelUpdate
- pub fn on_channel_update<F>(&mut self, handler: F)
- where F: Fn(Context, Option<Channel>, Channel) + Send + Sync + 'static {
- self.event_store.lock()
- .unwrap()
- .on_channel_update = Some(Arc::new(handler));
- }
-
/// Attaches a handler for when a [`FriendSuggestionCreate`] is received.
///
/// [`FriendSuggestionCreate`]: ../model/enum.Event.html#variant.FriendSuggestionCreate
@@ -602,20 +582,6 @@ impl Client {
.on_guild_role_create = Some(Arc::new(handler));
}
- /// Attaches a handler for when a [`GuildRoleUpdate`] is received.
- ///
- /// The optional `Role` is the role prior to updating. This can be `None` if
- /// it did not exist in the [`State`] before the update.
- ///
- /// [`GuildRoleUpdate`]: ../model/enum.Event.html#variant.GuildRoleUpdate
- /// [`State`]: ../ext/state/struct.State.html
- pub fn on_guild_role_update<F>(&mut self, handler: F)
- where F: Fn(Context, GuildId, Option<Role>, Role) + Send + Sync + 'static {
- self.event_store.lock()
- .unwrap()
- .on_guild_role_update = Some(Arc::new(handler));
- }
-
/// Attaches a handler for when a [`GuildRoleSync`] is received.
///
/// [`GuildRoleSync`]: ../model/enum.Event.html#variant.GuildRoleSync
@@ -636,16 +602,6 @@ impl Client {
.on_guild_unavailable = Some(Arc::new(handler));
}
- /// Attaches a handler for when a [`GuildUpdate`] is received.
- ///
- /// [`GuildUpdate`]: ../model/enum.Event.html#variant.GuildUpdate
- pub fn on_guild_update<F>(&mut self, handler: F)
- where F: Fn(Context, Option<LiveGuild>, Guild) + Send + Sync + 'static {
- self.event_store.lock()
- .unwrap()
- .on_guild_update = Some(Arc::new(handler));
- }
-
/// Attaches a handler for when a [`GuildBan`] is received.
///
/// [`GuildBan`]: ../model/enum.Event.html#variant.GuildBan
@@ -869,16 +825,6 @@ impl Client {
.on_unknown = Some(Arc::new(handler));
}
- /// Attaches a handler for when a [`UserGuildSettingsUpdate`] is received.
- ///
- /// [`UserGuildSettingsUpdate`]: ../model/enum.Event.html#variant.UserGuildSettingsUpdate
- pub fn on_user_guild_settings_update<F>(&mut self, handler: F)
- where F: Fn(Context, Option<UserGuildSettings>, UserGuildSettings) + Send + Sync + 'static {
- self.event_store.lock()
- .unwrap()
- .on_user_guild_settings_update = Some(Arc::new(handler));
- }
-
/// Attaches a handler for when a [`VoiceServerUpdate`] is received.
///
/// [`VoiceServerUpdate`]: ../model/enum.Event.html#variant.VoiceServerUpdate
@@ -998,6 +944,19 @@ impl Client {
#[cfg(feature = "state")]
impl Client {
+ /// Attaches a handler for when a [`CallDelete`] is received.
+ ///
+ /// The `ChannelId` is the Id of the channel hosting the call. Returns the
+ /// call from the state - optionally - if the call was in it.
+ ///
+ /// [`CallDelete`]: ../model/enum.Event.html#variant.CallDelete
+ pub fn on_call_delete<F>(&mut self, handler: F)
+ where F: Fn(Context, ChannelId, Option<Call>) + Send + Sync + 'static {
+ self.event_store.lock()
+ .unwrap()
+ .on_call_delete = Some(Arc::new(handler));
+ }
+
/// Attaches a handler for when a [`CallUpdate`] is received.
///
/// [`CallUpdate`]: ../model/enum.Event.html#variant.CallUpdate
@@ -1008,6 +967,18 @@ impl Client {
.on_call_update = Some(Arc::new(handler));
}
+ /// Attaches a handler for when a [`ChannelUpdate`] is received.
+ ///
+ /// Optionally provides the version of the channel before the update.
+ ///
+ /// [`ChannelUpdate`]: ../model/enum.Event.html#variant.ChannelUpdate
+ pub fn on_channel_update<F>(&mut self, handler: F)
+ where F: Fn(Context, Option<Channel>, Channel) + Send + Sync + 'static {
+ self.event_store.lock()
+ .unwrap()
+ .on_channel_update = Some(Arc::new(handler));
+ }
+
/// Attaches a handler for when a [`GuildDelete`] is received.
///
/// Returns a partial guild as well as - optionally - the full guild, with
@@ -1061,6 +1032,40 @@ impl Client {
.on_guild_role_delete = Some(Arc::new(handler));
}
+ /// Attaches a handler for when a [`GuildRoleUpdate`] is received.
+ ///
+ /// The optional `Role` is the role prior to updating. This can be `None` if
+ /// it did not exist in the [`State`] before the update.
+ ///
+ /// [`GuildRoleUpdate`]: ../model/enum.Event.html#variant.GuildRoleUpdate
+ /// [`State`]: ../ext/state/struct.State.html
+ pub fn on_guild_role_update<F>(&mut self, handler: F)
+ where F: Fn(Context, GuildId, Option<Role>, Role) + Send + Sync + 'static {
+ self.event_store.lock()
+ .unwrap()
+ .on_guild_role_update = Some(Arc::new(handler));
+ }
+
+ /// Attaches a handler for when a [`UserGuildSettingsUpdate`] is received.
+ ///
+ /// [`UserGuildSettingsUpdate`]: ../model/enum.Event.html#variant.UserGuildSettingsUpdate
+ pub fn on_user_guild_settings_update<F>(&mut self, handler: F)
+ where F: Fn(Context, Option<UserGuildSettings>, UserGuildSettings) + Send + Sync + 'static {
+ self.event_store.lock()
+ .unwrap()
+ .on_user_guild_settings_update = Some(Arc::new(handler));
+ }
+
+ /// Attaches a handler for when a [`GuildUpdate`] is received.
+ ///
+ /// [`GuildUpdate`]: ../model/enum.Event.html#variant.GuildUpdate
+ pub fn on_guild_update<F>(&mut self, handler: F)
+ where F: Fn(Context, Option<LiveGuild>, Guild) + Send + Sync + 'static {
+ self.event_store.lock()
+ .unwrap()
+ .on_guild_update = Some(Arc::new(handler));
+ }
+
/// Attaches a handler for when a [`UserNoteUpdate`] is received.
///
/// Optionally returns the old note for the [`User`], if one existed.
@@ -1101,6 +1106,16 @@ impl Client {
#[cfg(not(feature = "state"))]
impl Client {
+ /// Attaches a handler for when a [`CallDelete`] is received.
+ ///
+ /// [`CallDelete`]: ../model/enum.Event.html#variant.CallDelete
+ pub fn on_call_delete<F>(&mut self, handler: F)
+ where F: Fn(Context, ChannelId) + Send + Sync + 'static {
+ self.event_store.lock()
+ .unwrap()
+ .on_call_delete = Some(Arc::new(handler));
+ }
+
/// Attaches a handler for when a [`CallUpdate`] is received.
///
/// [`CallUpdate`]: ../model/enum.Event.html#variant.CallUpdate
@@ -1111,13 +1126,23 @@ impl Client {
.on_call_update = Some(Arc::new(handler));
}
+ /// Attaches a handler for when a [`ChannelUpdate`] is received.
+ ///
+ /// [`ChannelUpdate`]: ../model/enum.Event.html#variant.ChannelUpdate
+ pub fn on_channel_update<F>(&mut self, handler: F)
+ where F: Fn(Context, Channel) + Send + Sync + 'static {
+ self.event_store.lock()
+ .unwrap()
+ .on_channel_update = Some(Arc::new(handler));
+ }
+
/// Attaches a handler for when a [`GuildDelete`] is received.
///
/// [`GuildDelete`]: ../model/enum.Event.html#variant.GuildDelete
/// [`Role`]: ../model/struct.Role.html
/// [`State`]: ../ext/state/struct.State.html
pub fn on_guild_delete<F>(&mut self, handler: F)
- where F: Fn(Context, Guild, Option<LiveGuild>) + Send + Sync + 'static {
+ where F: Fn(Context, Guild) + Send + Sync + 'static {
self.event_store.lock()
.unwrap()
.on_guild_delete = Some(Arc::new(handler));
@@ -1156,6 +1181,37 @@ impl Client {
.on_guild_role_delete = Some(Arc::new(handler));
}
+ /// Attaches a handler for when a [`GuildRoleUpdate`] is received.
+ ///
+ /// [`GuildRoleUpdate`]: ../model/enum.Event.html#variant.GuildRoleUpdate
+ /// [`State`]: ../ext/state/struct.State.html
+ pub fn on_guild_role_update<F>(&mut self, handler: F)
+ where F: Fn(Context, GuildId, Role) + Send + Sync + 'static {
+ self.event_store.lock()
+ .unwrap()
+ .on_guild_role_update = Some(Arc::new(handler));
+ }
+
+ /// Attaches a handler for when a [`UserGuildSettingsUpdate`] is received.
+ ///
+ /// [`UserGuildSettingsUpdate`]: ../model/enum.Event.html#variant.UserGuildSettingsUpdate
+ pub fn on_user_guild_settings_update<F>(&mut self, handler: F)
+ where F: Fn(Context, UserGuildSettings) + Send + Sync + 'static {
+ self.event_store.lock()
+ .unwrap()
+ .on_user_guild_settings_update = Some(Arc::new(handler));
+ }
+
+ /// Attaches a handler for when a [`GuildUpdate`] is received.
+ ///
+ /// [`GuildUpdate`]: ../model/enum.Event.html#variant.GuildUpdate
+ pub fn on_guild_update<F>(&mut self, handler: F)
+ where F: Fn(Context, Guild) + Send + Sync + 'static {
+ self.event_store.lock()
+ .unwrap()
+ .on_guild_update = Some(Arc::new(handler));
+ }
+
/// Attaches a handler for when a [`UserNoteUpdate`] is received.
///
/// Optionally returns the old note for the [`User`], if one existed.
@@ -1173,7 +1229,7 @@ impl Client {
///
/// [`UserSettingsUpdate`]: ../model/enum.Event.html#variant.UserSettingsUpdate
pub fn on_user_settings_update<F>(&mut self, handler: F)
- where F: Fn(Context, UserSettingsEvent) + Send + Sync + 'static {
+ where F: Fn(Context, UserSettingsUpdateEvent) + Send + Sync + 'static {
self.event_store.lock()
.unwrap()
.on_user_settings_update = Some(Arc::new(handler));