aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorAustin Hellyer <[email protected]>2016-11-12 11:21:12 -0800
committerAustin Hellyer <[email protected]>2016-11-12 11:21:12 -0800
commite9cdc50f68ad36d78b5fcc1c0dd6463274828eca (patch)
treec6b0bd54bae9f18906a47fd85402faea5345a1cd /src/client
parentAdd delete_message_reactions + register event (diff)
downloadserenity-e9cdc50f68ad36d78b5fcc1c0dd6463274828eca.tar.xz
serenity-e9cdc50f68ad36d78b5fcc1c0dd6463274828eca.zip
Add a check for message content length
Before sending a request to Discord, ensure that a message's content on non-HTTP functions and methods meets the required length. If it exceeds the limit, then return a `Error::Client(ClientError::MessageTooLong(u64))`, containing the number of unicode code points exceeding the limit. Note that directly using the HTTP methods does not impose this limit.
Diffstat (limited to 'src/client')
-rw-r--r--src/client/context.rs38
-rw-r--r--src/client/mod.rs7
2 files changed, 44 insertions, 1 deletions
diff --git a/src/client/context.rs b/src/client/context.rs
index faa23f6..9e584b3 100644
--- a/src/client/context.rs
+++ b/src/client/context.rs
@@ -813,10 +813,15 @@ impl Context {
///
/// # Errors
///
+ /// Returns a [`ClientError::MessageTooLong`] if the content of the message
+ /// is over the above limit, containing the number of unicode code points
+ /// over the limit.
+ ///
/// Returns a [`ClientError::NoChannelId`] when there is no [`ChannelId`]
/// directly available.
///
- /// [`ChannelId`]: ../../models/struct.ChannelId.html
+ /// [`ChannelId`]: ../../model/struct.ChannelId.html
+ /// [`ClientError::MessageTooLong`]: enum.ClientError.html#variant.MessageTooLong
/// [`ClientError::NoChannelId`]: ../enum.ClientError.html#NoChannelId
/// [`Message`]: ../model/struct.Message.html
pub fn say(&self, text: &str) -> Result<Message> {
@@ -827,6 +832,20 @@ impl Context {
}
}
+ /// Sends a file along with optional message contents. The filename _must_
+ /// be specified.
+ ///
+ /// Pass an empty string to send no message contents.
+ ///
+ /// **Note**: Message contents must be under 2000 unicode code points.
+ ///
+ /// # Errors
+ ///
+ /// If the content of the message is over the above limit, then a
+ /// [`ClientError::MessageTooLong`] will be returned, containing the number
+ /// of unicode code points over the limit.
+ ///
+ /// [`ClientError::MessageTooLong`]: enum.ClientError.html#variant.MessageTooLong
pub fn send_file<C, R>(&self,
channel_id: C,
content: &str,
@@ -834,6 +853,10 @@ impl Context {
filename: &str)
-> Result<Message> where C: Into<ChannelId>,
R: Read {
+ if let Some(length_over) = Message::overflow_length(content) {
+ return Err(Error::Client(ClientError::MessageTooLong(length_over)));
+ }
+
http::send_file(channel_id.into().0, content, file, filename)
}
@@ -842,6 +865,8 @@ impl Context {
/// Note that often a nonce is not required and can be omitted in most
/// situations.
///
+ /// **Note**: Message contents must be under 2000 unicode code points.
+ ///
/// # Example
///
/// ```rust,ignore
@@ -849,9 +874,20 @@ impl Context {
/// let _ = context.send_message(message.channel_id, "Hello!", "", false);
/// ```
///
+ /// # Errors
+ ///
+ /// Returns a [`ClientError::MessageTooLong`] if the content of the message
+ /// is over the above limit, containing the number of unicode code points
+ /// over the limit.
+ ///
/// [`Channel`]: ../model/enum.Channel.html
+ /// [`ClientError::MessageTooLong`]: enum.ClientError.html#variant.MessageTooLong
pub fn send_message<C>(&self, channel_id: C, content: &str, nonce: &str, tts: bool)
-> Result<Message> where C: Into<ChannelId> {
+ if let Some(length_over) = Message::overflow_length(content) {
+ return Err(Error::Client(ClientError::MessageTooLong(length_over)));
+ }
+
let map = ObjectBuilder::new()
.insert("content", content)
.insert("nonce", nonce)
diff --git a/src/client/mod.rs b/src/client/mod.rs
index 4db7a4e..0f0fa7a 100644
--- a/src/client/mod.rs
+++ b/src/client/mod.rs
@@ -175,6 +175,13 @@ pub enum ClientError {
///
/// [`State`]: ../ext/state/struct.State.html
ItemMissing,
+ /// Indicates that a [`Message`]s content was too long and will not
+ /// successfully send, as the length is over 2000 codepoints, or 4000 bytes.
+ ///
+ /// The number of bytes larger than the limit is provided.
+ ///
+ /// [`Message`]: ../model/struct.Message.html
+ MessageTooLong(u64),
/// When attempting to use a [`Context`] helper method which requires a
/// contextual [`ChannelId`], but the current context is not appropriate for
/// the action.