diff options
| author | Zeyla Hellyer <[email protected]> | 2017-06-26 21:50:16 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-06-26 21:50:16 -0700 |
| commit | af1061b5e82ed1bf4e71ff3146cb98bc6cbb678c (patch) | |
| tree | 6a33be2c9cea182a04b82273244c097bbfff33c9 /src/model | |
| parent | Fix ModelError doctest (diff) | |
| download | serenity-af1061b5e82ed1bf4e71ff3146cb98bc6cbb678c.tar.xz serenity-af1061b5e82ed1bf4e71ff3146cb98bc6cbb678c.zip | |
Prevent Direct Messaging other bot users
The API no longer allows bot users to Direct Message other bot users, so
pre-emptively check that the recipient is not a bot. If it is, return a
`ModelError::MessagingBot`.
Diffstat (limited to 'src/model')
| -rw-r--r-- | src/model/error.rs | 3 | ||||
| -rw-r--r-- | src/model/user.rs | 16 |
2 files changed, 19 insertions, 0 deletions
diff --git a/src/model/error.rs b/src/model/error.rs index a54b1ed..4126ac3 100644 --- a/src/model/error.rs +++ b/src/model/error.rs @@ -101,6 +101,9 @@ pub enum Error { /// /// [`Message`]: ../model/struct.Message.html MessageTooLong(u64), + /// Indicates that the current user is attempting to Direct Message another + /// bot user, which is disallowed by the API. + MessagingBot, } impl Display for Error { diff --git a/src/model/user.rs b/src/model/user.rs index aaf334b..364b978 100644 --- a/src/model/user.rs +++ b/src/model/user.rs @@ -471,6 +471,12 @@ impl User { /// }); /// ``` /// + /// # Examples + /// + /// Returns a [`ModelError::MessagingBot`] if the user being direct messaged + /// is a bot user. + /// + /// [`ModelError::MessagingBot`]: enum.ModelError.html#variant.MessagingBot /// [`PrivateChannel`]: struct.PrivateChannel.html /// [`User::dm`]: struct.User.html#method.dm // A tale with Clippy: @@ -491,6 +497,10 @@ impl User { #[cfg(feature="builder")] pub fn direct_message<F>(&self, f: F) -> Result<Message> where F: FnOnce(CreateMessage) -> CreateMessage { + if self.bot { + return Err(Error::Model(ModelError::MessagingBot)); + } + let private_channel_id = feature_cache! {{ let finding = { let cache = CACHE.read().unwrap(); @@ -545,6 +555,12 @@ impl User { /// let _ = message.author.dm("Hello!"); /// ``` /// + /// # Examples + /// + /// Returns a [`ModelError::MessagingBot`] if the user being direct messaged + /// is a bot user. + /// + /// [`ModelError::MessagingBot`]: enum.ModelError.html#variant.MessagingBot /// [direct_message]: #method.direct_message #[cfg(feature="builder")] #[inline] |