Skip to content

Commit 397c760

Browse files
GnomedDevmkrasnitski
authored andcommitted
Clean up Message::reply (#2897)
- Gets rid of duplicate documentation - Removes unnecessary monomorphisation of the `Cow<'_, str>` argument - Removes the useless `reply_mention` method - Respects the default_allowed_mentions.
1 parent 9a54f57 commit 397c760

File tree

1 file changed

+18
-60
lines changed

1 file changed

+18
-60
lines changed

src/model/channel/message.rs

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! Models relating to Discord channels.
22
33
use std::borrow::Cow;
4-
#[cfg(feature = "model")]
5-
use std::fmt::Display;
64

75
use nonmax::NonMaxU64;
86

@@ -449,83 +447,43 @@ impl Message {
449447

450448
/// Uses Discord's inline reply to a user without pinging them.
451449
///
452-
/// User mentions are generally around 20 or 21 characters long.
453-
///
454-
/// **Note**: Requires the [Send Messages] permission.
455-
///
456-
/// **Note**: Message contents must be under 2000 unicode code points.
450+
/// Refer to the documentation for [`CreateMessage`] for information regarding content
451+
/// restrictions and requirements.
457452
///
458453
/// # Errors
459454
///
460-
/// Returns a [`ModelError::TooLarge`] if the content of the message is over the above
461-
/// limit, containing the number of unicode code points over the limit.
462-
///
463-
/// [Send Messages]: Permissions::SEND_MESSAGES
455+
/// See the documentation of [`CreateMessage::execute`] for possible errors.
464456
pub async fn reply(&self, http: &Http, content: impl Into<Cow<'_, str>>) -> Result<Message> {
465-
self._reply(http, content, Some(false)).await
457+
self._reply(http, content.into(), false).await
466458
}
467459

468460
/// Uses Discord's inline reply to a user with a ping.
469461
///
470-
/// **Note**: Requires the [Send Messages] permission.
471-
///
472-
/// **Note**: Message contents must be under 2000 unicode code points.
462+
/// Refer to the documentation for [`CreateMessage`] for information regarding content
463+
/// restrictions and requirements.
473464
///
474465
/// # Errors
475466
///
476-
/// Returns [`Error::Http`] if the current user lacks permission or if invalid data is given.
477-
///
478-
/// Returns a [`ModelError::TooLarge`] if the content of the message is over the above
479-
/// limit, containing the number of unicode code points over the limit.
480-
///
481-
/// [Send Messages]: Permissions::SEND_MESSAGES
467+
/// See the documentation of [`CreateMessage::execute`] for possible errors.
482468
pub async fn reply_ping(
483469
&self,
484470
http: &Http,
485471
content: impl Into<Cow<'_, str>>,
486472
) -> Result<Message> {
487-
self._reply(http, content, Some(true)).await
473+
self._reply(http, content.into(), true).await
488474
}
489475

490-
/// Replies to the user, mentioning them prior to the content in the form of: `@<USER_ID>
491-
/// YOUR_CONTENT`.
492-
///
493-
/// User mentions are generally around 20 or 21 characters long.
494-
///
495-
/// **Note**: Requires the [Send Messages] permission.
496-
///
497-
/// **Note**: Message contents must be under 2000 unicode code points.
498-
///
499-
/// # Errors
500-
///
501-
/// Returns [`Error::Http`] if the current user lacks permission or if invalid data is given.
502-
///
503-
/// Returns a [`ModelError::TooLarge`] if the content of the message is over the above
504-
/// limit, containing the number of unicode code points over the limit.
505-
///
506-
/// [Send Messages]: Permissions::SEND_MESSAGES
507-
pub async fn reply_mention(&self, http: &Http, content: impl Display) -> Result<Message> {
508-
self._reply(http, format!("{} {content}", self.author.mention()), None).await
509-
}
476+
async fn _reply(&self, http: &Http, content: Cow<'_, str>, ping_user: bool) -> Result<Message> {
477+
let default_allowed_mentions = http.default_allowed_mentions.clone();
478+
let allowed_mentions = default_allowed_mentions.unwrap_or_else(|| {
479+
CreateAllowedMentions::new().everyone(true).all_users(true).all_roles(true)
480+
});
481+
482+
let builder = CreateMessage::new()
483+
.content(content)
484+
.reference_message(self)
485+
.allowed_mentions(allowed_mentions.replied_user(ping_user));
510486

511-
/// `inlined` decides whether this reply is inlined and whether it pings.
512-
async fn _reply(
513-
&self,
514-
http: &Http,
515-
content: impl Into<Cow<'_, str>>,
516-
inlined: Option<bool>,
517-
) -> Result<Message> {
518-
let mut builder = CreateMessage::new().content(content);
519-
if let Some(ping_user) = inlined {
520-
let allowed_mentions = CreateAllowedMentions::new()
521-
.replied_user(ping_user)
522-
// By providing allowed_mentions, Discord disabled _all_ pings by default so we
523-
// need to re-enable them
524-
.everyone(true)
525-
.all_users(true)
526-
.all_roles(true);
527-
builder = builder.reference_message(self).allowed_mentions(allowed_mentions);
528-
}
529487
self.channel_id.send_message(http, builder).await
530488
}
531489

0 commit comments

Comments
 (0)