From 60c33db56bb3754bb0d2196d5f48fee63adf7730 Mon Sep 17 00:00:00 2001 From: acdenisSK Date: Mon, 10 Jul 2017 22:11:07 +0200 Subject: Return an error if the reason the user provided exceeded the limit --- src/error.rs | 5 +++++ src/model/guild/guild_id.rs | 8 +++++++- src/model/guild/member.rs | 8 +++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/error.rs b/src/error.rs index a9484d5..390d295 100644 --- a/src/error.rs +++ b/src/error.rs @@ -61,6 +61,10 @@ pub enum Error { Model(ModelError), /// An error occurred while parsing an integer. Num(ParseIntError), + /// Input exceeded a limit. + /// + /// *This only exists for the `GuildId::ban` and `Member::ban` functions. For their cases, it's the "reason".* + ExceededLimit, /// Some other error. This is only used for "Expected value " errors, /// when a more detailed error can not be easily provided via the /// [`Error::Decode`] variant. @@ -186,6 +190,7 @@ impl StdError for Error { fn description(&self) -> &str { match *self { Error::Decode(msg, _) | Error::Other(msg) => msg, + Error::ExceededLimit => "Input exceeded a limit", Error::Format(ref inner) => inner.description(), Error::Io(ref inner) => inner.description(), Error::Json(ref inner) => inner.description(), diff --git a/src/model/guild/guild_id.rs b/src/model/guild/guild_id.rs index c2cfc3d..c707413 100644 --- a/src/model/guild/guild_id.rs +++ b/src/model/guild/guild_id.rs @@ -53,7 +53,13 @@ impl GuildId { return Err(Error::Model(ModelError::DeleteMessageDaysAmount(dmd))); } - http::ban_user(self.0, user.into().0, dmd, &*ban_options.reason()) + let reason = ban_options.reason(); + + if reason.len() > 512 { + return Err(Error::ExceededLimit); + } + + http::ban_user(self.0, user.into().0, dmd, &*reason) } /// Gets a list of the guild's bans. diff --git a/src/model/guild/member.rs b/src/model/guild/member.rs index b128887..053360d 100644 --- a/src/model/guild/member.rs +++ b/src/model/guild/member.rs @@ -146,7 +146,13 @@ impl Member { /// [Ban Members]: permissions/constant.BAN_MEMBERS.html #[cfg(feature="cache")] pub fn ban(&self, ban_options: BO) -> Result<()> { - http::ban_user(self.guild_id.0, self.user.read().unwrap().id.0, ban_options.dmd(), &*ban_options.reason()) + let reason = ban_options.reason(); + + if reason.len() > 512 { + return Err(Error::ExceededLimit); + } + + http::ban_user(self.guild_id.0, self.user.read().unwrap().id.0, ban_options.dmd(), &*reason) } /// Determines the member's colour. -- cgit v1.2.3