diff options
| author | Zeyla Hellyer <[email protected]> | 2018-04-27 08:05:56 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2018-05-27 19:26:44 -0700 |
| commit | bbff98c4c58f5169fa232cef2e714ba6b1490dfd (patch) | |
| tree | e528ffeb883352c8584beac37b9636ba0e2612d6 /src/client | |
| parent | Remove erroneous `migrations` directory (diff) | |
| download | serenity-bbff98c4c58f5169fa232cef2e714ba6b1490dfd.tar.xz serenity-bbff98c4c58f5169fa232cef2e714ba6b1490dfd.zip | |
Add Rich Presence parsing support
Adds support for parsing Rich Presences.
This can not be used for setting activities with bots.
Upgrade path:
Basically change your import and usage from
`serenity::model::gateway::Game` to
`serenity::model::gateway::Activity`.
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/bridge/gateway/shard_messenger.rs | 26 | ||||
| -rw-r--r-- | src/client/bridge/gateway/shard_runner.rs | 16 | ||||
| -rw-r--r-- | src/client/bridge/gateway/shard_runner_message.rs | 10 | ||||
| -rw-r--r-- | src/client/context.rs | 62 |
4 files changed, 55 insertions, 59 deletions
diff --git a/src/client/bridge/gateway/shard_messenger.rs b/src/client/bridge/gateway/shard_messenger.rs index 2331d4a..efec9fc 100644 --- a/src/client/bridge/gateway/shard_messenger.rs +++ b/src/client/bridge/gateway/shard_messenger.rs @@ -7,11 +7,11 @@ use websocket::message::OwnedMessage; /// A lightweight wrapper around an mpsc sender. /// /// This is used to cleanly communicate with a shard's respective -/// [`ShardRunner`]. This can be used for actions such as setting the game via -/// [`set_game`] or shutting down via [`shutdown`]. +/// [`ShardRunner`]. This can be used for actions such as setting the activity +/// via [`set_activity`] or shutting down via [`shutdown`]. /// /// [`ShardRunner`]: struct.ShardRunner.html -/// [`set_game`]: #method.set_game +/// [`set_activity`]: #method.set_activity /// [`shutdown`]: #method.shutdown #[derive(Clone, Debug)] pub struct ShardMessenger { @@ -125,13 +125,13 @@ impl ShardMessenger { }); } - /// Sets the user's current game, if any. + /// Sets the user's current activity, if any. /// /// Other presence settings are maintained. /// /// # Examples /// - /// Setting the current game to playing `"Heroes of the Storm"`: + /// Setting the current activity to playing `"Heroes of the Storm"`: /// /// ```rust,no_run /// # extern crate parking_lot; @@ -147,9 +147,9 @@ impl ShardMessenger { /// # /// # let mut shard = Shard::new(mutex.clone(), mutex, [0, 1]).unwrap(); /// # - /// use serenity::model::gateway::Game; + /// use serenity::model::gateway::Activity; /// - /// shard.set_game(Some(Game::playing("Heroes of the Storm"))); + /// shard.set_activity(Some(Activity::playing("Heroes of the Storm"))); /// # Ok(()) /// # } /// # @@ -157,8 +157,8 @@ impl ShardMessenger { /// # try_main().unwrap(); /// # } /// ``` - pub fn set_game(&self, game: Option<Game>) { - let _ = self.send(ShardRunnerMessage::SetGame(game)); + pub fn set_activity(&self, activity: Option<Activity>) { + let _ = self.send(ShardRunnerMessage::SetActivity(activity)); } /// Sets the user's full presence information. @@ -185,9 +185,9 @@ impl ShardMessenger { /// # /// # let mut shard = Shard::new(mutex.clone(), mutex, [0, 1]).unwrap(); /// # - /// use serenity::model::{Game, OnlineStatus}; + /// use serenity::model::{Activity, OnlineStatus}; /// - /// shard.set_presence(Some(Game::playing("Heroes of the Storm")), OnlineStatus::Online); + /// shard.set_presence(Some(Activity::playing("Heroes of the Storm")), OnlineStatus::Online); /// # Ok(()) /// # } /// # @@ -195,12 +195,12 @@ impl ShardMessenger { /// # try_main().unwrap(); /// # } /// ``` - pub fn set_presence(&self, game: Option<Game>, mut status: OnlineStatus) { + pub fn set_presence(&self, activity: Option<Activity>, mut status: OnlineStatus) { if status == OnlineStatus::Offline { status = OnlineStatus::Invisible; } - let _ = self.send(ShardRunnerMessage::SetPresence(status, game)); + let _ = self.send(ShardRunnerMessage::SetPresence(status, activity)); } /// Sets the user's current online status. diff --git a/src/client/bridge/gateway/shard_runner.rs b/src/client/bridge/gateway/shard_runner.rs index ea1fad6..0f244bd 100644 --- a/src/client/bridge/gateway/shard_runner.rs +++ b/src/client/bridge/gateway/shard_runner.rs @@ -258,26 +258,26 @@ impl<H: EventHandler + Send + Sync + 'static> ShardRunner<H> { ShardRunnerMessage::Message(msg) => { self.shard.client.send_message(&msg).is_ok() }, - ShardRunnerMessage::SetGame(game) => { - // To avoid a clone of `game`, we do a little bit of + ShardRunnerMessage::SetActivity(activity) => { + // To avoid a clone of `activity`, we do a little bit of // trickery here: // // First, we obtain a reference to the current presence of // the shard, and create a new presence tuple of the new - // game we received over the channel as well as the online - // status that the shard already had. + // activity we received over the channel as well as the + // online status that the shard already had. // // We then (attempt to) send the websocket message with the // status update, expressively returning: // // - whether the message successfully sent - // - the original game we received over the channel - self.shard.set_game(game); + // - the original activity we received over the channel + self.shard.set_activity(activity); self.shard.update_presence().is_ok() }, - ShardRunnerMessage::SetPresence(status, game) => { - self.shard.set_presence(status, game); + ShardRunnerMessage::SetPresence(status, activity) => { + self.shard.set_presence(status, activity); self.shard.update_presence().is_ok() }, diff --git a/src/client/bridge/gateway/shard_runner_message.rs b/src/client/bridge/gateway/shard_runner_message.rs index 29024c9..2e262d8 100644 --- a/src/client/bridge/gateway/shard_runner_message.rs +++ b/src/client/bridge/gateway/shard_runner_message.rs @@ -1,7 +1,7 @@ use model::{ - gateway::Game, + gateway::Activity, + id::GuildId, user::OnlineStatus, - id::GuildId }; use websocket::message::OwnedMessage; @@ -38,11 +38,11 @@ pub enum ShardRunnerMessage { Close(u16, Option<String>), /// Indicates that the client is to send a custom WebSocket message. Message(OwnedMessage), - /// Indicates that the client is to update the shard's presence's game. - SetGame(Option<Game>), + /// Indicates that the client is to update the shard's presence's activity. + SetActivity(Option<Activity>), /// Indicates that the client is to update the shard's presence in its /// entirity. - SetPresence(OnlineStatus, Option<Game>), + SetPresence(OnlineStatus, Option<Activity>), /// Indicates that the client is to update the shard's presence's status. SetStatus(OnlineStatus), } diff --git a/src/client/context.rs b/src/client/context.rs index 9bb6f68..72ca372 100644 --- a/src/client/context.rs +++ b/src/client/context.rs @@ -25,7 +25,7 @@ use utils::{self, VecMap}; /// [`Shard`] which received the event, or the low-level [`http`] module. /// /// The context contains "shortcuts", like for interacting with the shard. -/// Methods like [`set_game`] will unlock the shard and perform an update for +/// Methods like [`set_activity`] will unlock the shard and perform an update for /// you to save a bit of work. /// /// A context will only live for the event it was dispatched for. After the @@ -33,7 +33,7 @@ use utils::{self, VecMap}; /// /// [`Shard`]: ../gateway/struct.Shard.html /// [`http`]: ../http/index.html -/// [`set_game`]: #method.set_game +/// [`set_activity`]: #method.set_activity #[derive(Clone)] pub struct Context { /// A clone of [`Client::data`]. Refer to its documentation for more @@ -121,7 +121,7 @@ impl Context { /// Sets the current user as being [`Online`]. This maintains the current - /// game. + /// activity. /// /// # Examples /// @@ -153,7 +153,7 @@ impl Context { } /// Sets the current user as being [`Idle`]. This maintains the current - /// game. + /// activity. /// /// # Examples /// @@ -184,7 +184,7 @@ impl Context { } /// Sets the current user as being [`DoNotDisturb`]. This maintains the - /// current game. + /// current activity. /// /// # Examples /// @@ -215,7 +215,7 @@ impl Context { } /// Sets the current user as being [`Invisible`]. This maintains the current - /// game. + /// activity. /// /// # Examples /// @@ -246,8 +246,8 @@ impl Context { self.shard.set_status(OnlineStatus::Invisible); } - /// "Resets" the current user's presence, by setting the game to `None` and - /// the online status to [`Online`]. + /// "Resets" the current user's presence, by setting the activity to `None` + /// and the online status to [`Online`]. /// /// Use [`set_presence`] for fine-grained control over individual details. /// @@ -280,7 +280,7 @@ impl Context { self.shard.set_presence(None, OnlineStatus::Online); } - /// Sets the current game, defaulting to an online status of [`Online`]. + /// Sets the current activity, defaulting to an online status of [`Online`]. /// /// # Examples /// @@ -293,7 +293,7 @@ impl Context { /// # use serenity::prelude::*; /// # use serenity::model::channel::Message; /// # - /// use serenity::model::gateway::Game; + /// use serenity::model::gateway::Activity; /// /// struct Handler; /// @@ -305,7 +305,7 @@ impl Context { /// return; /// } /// - /// ctx.set_game(Game::playing(*unsafe { args.get_unchecked(1) })); + /// ctx.set_activity(Activity::playing(*unsafe { args.get_unchecked(1) })); /// } /// } /// @@ -320,22 +320,22 @@ impl Context { /// /// [`Online`]: ../model/user/enum.OnlineStatus.html#variant.Online #[inline] - pub fn set_game(&self, game: Game) { - self.shard.set_presence(Some(game), OnlineStatus::Online); + pub fn set_activity(&self, activity: Activity) { + self.shard.set_presence(Some(activity), OnlineStatus::Online); } - /// Sets the current game, passing in only its name. This will automatically - /// set the current user's [`OnlineStatus`] to [`Online`], and its - /// [`GameType`] as [`Playing`]. + /// Sets the current activity, passing in only its name. This will + /// automatically set the current user's [`OnlineStatus`] to [`Online`], and + /// its [`ActivityType`] as [`Playing`]. /// - /// Use [`reset_presence`] to clear the current game, or [`set_presence`] - /// for more fine-grained control. + /// Use [`reset_presence`] to clear the current activity, or + /// [`set_presence`] for more fine-grained control. /// /// **Note**: Maximum length is 128. /// /// # Examples /// - /// When an [`Event::Ready`] is received, set the game name to `"test"`: + /// When an [`Event::Ready`] is received, set the activity name to `"test"`: /// /// ```rust,no_run /// # use serenity::prelude::*; @@ -354,27 +354,23 @@ impl Context { /// ``` /// /// [`Event::Ready`]: ../model/event/enum.Event.html#variant.Ready - /// [`GameType`]: ../model/gateway/enum.GameType.html + /// [`ActivityType`]: ../model/gateway/enum.ActivityType.html /// [`Online`]: ../model/user/enum.OnlineStatus.html#variant.Online /// [`OnlineStatus`]: ../model/user/enum.OnlineStatus.html - /// [`Playing`]: ../model/gateway/enum.GameType.html#variant.Playing + /// [`Playing`]: ../model/gateway/enum.ActivityType.html#variant.Playing /// [`reset_presence`]: #method.reset_presence /// [`set_presence`]: #method.set_presence pub fn set_game_name(&self, game_name: &str) { - let game = Game { - kind: GameType::Playing, - name: game_name.to_string(), - url: None, - }; + let activity = Activity::playing(game_name); - self.shard.set_presence(Some(game), OnlineStatus::Online); + self.shard.set_presence(Some(activity), OnlineStatus::Online); } /// Sets the current user's presence, providing all fields to be passed. /// /// # Examples /// - /// Setting the current user as having no game and being [`Idle`]: + /// Setting the current user as having no activity and being [`Idle`]: /// /// ```rust,no_run /// # use serenity::prelude::*; @@ -405,13 +401,13 @@ impl Context { /// /// impl EventHandler for Handler { /// fn ready(&self, context: Context, _: Ready) { - /// use serenity::model::gateway::Game; + /// use serenity::model::gateway::Activity; /// use serenity::model::user::OnlineStatus; /// - /// let game = Game::playing("Heroes of the Storm"); + /// let activity = Activity::playing("Heroes of the Storm"); /// let status = OnlineStatus::DoNotDisturb; /// - /// context.set_presence(Some(game), status); + /// context.set_presence(Some(activity), status); /// } /// } /// @@ -423,8 +419,8 @@ impl Context { /// [`DoNotDisturb`]: ../model/user/enum.OnlineStatus.html#variant.DoNotDisturb /// [`Idle`]: ../model/user/enum.OnlineStatus.html#variant.Idle #[inline] - pub fn set_presence(&self, game: Option<Game>, status: OnlineStatus) { - self.shard.set_presence(game, status); + pub fn set_presence(&self, activity: Option<Activity>, status: OnlineStatus) { + self.shard.set_presence(activity, status); } /// Disconnects the shard from the websocket, essentially "quiting" it. |