diff options
| author | acdenisSK <[email protected]> | 2017-07-07 16:21:34 +0200 |
|---|---|---|
| committer | acdenisSK <[email protected]> | 2017-07-07 16:21:34 +0200 |
| commit | cdedf36330aa6da9e59d296164090f54b651b874 (patch) | |
| tree | be2d18f9882debcdf549200462d27930382efc0f /src | |
| parent | Add an `is_new` to the arguments of the `guild_create` handler (diff) | |
| download | serenity-cdedf36330aa6da9e59d296164090f54b651b874.tar.xz serenity-cdedf36330aa6da9e59d296164090f54b651b874.zip | |
Apply the new api change for dms in bots
Diffstat (limited to 'src')
| -rw-r--r-- | src/cache/mod.rs | 54 | ||||
| -rw-r--r-- | src/client/dispatch.rs | 24 | ||||
| -rw-r--r-- | src/client/event_handler.rs | 6 |
3 files changed, 42 insertions, 42 deletions
diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 797fc3c..8ce3d34 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -714,29 +714,24 @@ impl Cache { } } - pub(crate) fn update_with_channel_delete(&mut self, event: &ChannelDeleteEvent) -> Option<Channel> { - match event.channel { - Channel::Group(ref group) => { - self.groups.remove(&group.read().unwrap().channel_id).map(Channel::Group) - }, - Channel::Private(ref channel) => { - self.private_channels.remove(&channel.read().unwrap().id).map(Channel::Private) - }, - Channel::Guild(ref channel) => { - let (channel_id, guild_id) = { - let channel = channel.read().unwrap(); + pub(crate) fn update_with_channel_delete(&mut self, event: &ChannelDeleteEvent) { + // We ignore these two due to the fact that the delete event for dms/groups will _not_ fire anymore. + let channel = match event.channel { + Channel::Guild(ref channel) => channel, + Channel::Private(_) | Channel::Group(_) => panic!("Unexpected group/dm channel in the delete event!"), + }; - (channel.id, channel.guild_id) - }; + let (channel_id, guild_id) = { + let channel = channel.read().unwrap(); - self.channels.remove(&channel_id); + (channel.id, channel.guild_id) + }; - self.guilds - .get_mut(&guild_id) - .and_then(|guild| guild.write().unwrap().channels.remove(&channel_id)) - .map(Channel::Guild) - }, - } + self.channels.remove(&channel_id); + + self.guilds + .get_mut(&guild_id) + .and_then(|guild| guild.write().unwrap().channels.remove(&channel_id)); } #[allow(dead_code)] @@ -1048,19 +1043,8 @@ impl Cache { } } - // The private channels sent in the READY contains both the actual - // private channels and the groups. - for (channel_id, channel) in ready.private_channels { - match channel { - Channel::Group(group) => { - self.groups.insert(channel_id, group); - }, - Channel::Private(channel) => { - self.private_channels.insert(channel_id, channel); - }, - Channel::Guild(guild) => warn!("Got a guild in DMs: {:?}", guild), - } - } + // `ready.private_channels` will always be empty, and possibly be removed in the future. + // So don't handle it at all. for (user_id, presence) in &mut ready.presences { if let Some(ref user) = presence.user { @@ -1126,11 +1110,11 @@ impl Default for Cache { fn default() -> Cache { Cache { channels: HashMap::default(), - groups: HashMap::default(), + groups: HashMap::with_capacity(128), guilds: HashMap::default(), notes: HashMap::default(), presences: HashMap::default(), - private_channels: HashMap::default(), + private_channels: HashMap::with_capacity(128), shard_count: 1, unavailable_guilds: HashSet::default(), user: CurrentUser { diff --git a/src/client/dispatch.rs b/src/client/dispatch.rs index 7ff37d5..c1df0d2 100644 --- a/src/client/dispatch.rs +++ b/src/client/dispatch.rs @@ -6,7 +6,7 @@ use super::Context; use typemap::ShareMap; use ::gateway::Shard; use ::model::event::Event; -use ::model::{Message, Reaction, GuildId}; +use ::model::{Message, Reaction, GuildId, Channel}; use chrono::{Utc, Timelike}; #[cfg(feature="framework")] @@ -202,16 +202,32 @@ fn handle_event<H: EventHandler + Send + Sync + 'static>(event: Event, let context = context(conn, data); + // This different channel_create dispacthing is only due to the fact that + // each time the bot receives a dm, this event is also fired. + // So in short, only exists to reduce unnecessary clutter. let h = event_handler.clone(); - thread::spawn(move || h.on_channel_create(context, event.channel)); + match event.channel { + Channel::Private(channel) => { + thread::spawn(move || h.on_private_channel_create(context, channel)); + }, + Channel::Group(_) => {}, + Channel::Guild(channel) => { + thread::spawn(move || h.on_channel_create(context, channel)); + }, + } }, Event::ChannelDelete(event) => { update!(update_with_channel_delete, event); let context = context(conn, data); - let h = event_handler.clone(); - thread::spawn(move || h.on_channel_delete(context, event.channel)); + match event.channel { + Channel::Private(_) | Channel::Group(_) => {} + Channel::Guild(channel) => { + let h = event_handler.clone(); + thread::spawn(move || h.on_channel_delete(context, channel)); + }, + } }, Event::ChannelPinsUpdate(event) => { let context = context(conn, data); diff --git a/src/client/event_handler.rs b/src/client/event_handler.rs index d8baecb..09482b8 100644 --- a/src/client/event_handler.rs +++ b/src/client/event_handler.rs @@ -5,14 +5,14 @@ use super::context::Context; use ::model::event::*; use ::model::*; -#[cfg(feature="cache")] use std::sync::RwLock; pub trait EventHandler { #[cfg(feature="cache")] fn on_cached(&self, _: Context, _: Vec<GuildId>) {} - fn on_channel_create(&self, _: Context, _: Channel) {} - fn on_channel_delete(&self, _: Context, _: Channel) {} + fn on_channel_create(&self, _: Context, _: Arc<RwLock<GuildChannel>>) {} + fn on_private_channel_create(&self, _: Context, _: Arc<RwLock<PrivateChannel>>) {} + fn on_channel_delete(&self, _: Context, _: Arc<RwLock<GuildChannel>>) {} fn on_channel_pins_update(&self, _: Context, _: ChannelPinsUpdateEvent) {} fn on_channel_recipient_addition(&self, _: Context, _: ChannelId, _: User) {} fn on_channel_recipient_removal(&self, _: Context, _: ChannelId, _: User) {} |