1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
#[cfg(all(feature = "model", feature = "utils"))]
use std::error::Error as StdError;
use std::fmt;
#[cfg(all(feature = "model", feature = "utils"))]
use std::str::FromStr;
use super::prelude::*;
#[cfg(all(feature = "model", feature = "utils"))]
use crate::utils;
/// Allows something - such as a channel or role - to be mentioned in a message.
pub trait Mentionable {
/// Creates a [`Mention`] that will be able to notify or create a link to the item.
///
/// [`Mention`] implements [`Display`], so [`ToString::to_string()`] can be called on it, or
/// inserted directly into a [`format_args!`] type of macro.
///
/// [`Display`]: fmt::Display
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "client")] {
/// # use serenity::builder::CreateMessage;
/// # use serenity::model::guild::Member;
/// # use serenity::model::channel::GuildChannel;
/// # use serenity::model::id::ChannelId;
/// # use serenity::prelude::Context;
/// # use serenity::Error;
/// use serenity::model::mention::Mentionable;
/// async fn greet(
/// ctx: Context,
/// member: Member,
/// to_channel: GuildChannel,
/// rules_channel: ChannelId,
/// ) -> Result<(), Error> {
/// let builder = CreateMessage::new().content(format!(
/// "Hi {member}, welcome to the server! \
/// Please refer to {rules} for our code of conduct, \
/// and enjoy your stay.",
/// member = member.mention(),
/// rules = rules_channel.mention(),
/// ));
/// to_channel.id.send_message(ctx, builder).await?;
/// Ok(())
/// }
/// # }
/// ```
/// ```
/// # use serenity::model::id::{RoleId, ChannelId, UserId};
/// use serenity::model::mention::Mentionable;
/// let user = UserId::new(1);
/// let channel = ChannelId::new(2);
/// let role = RoleId::new(3);
/// assert_eq!(
/// "<@1> <#2> <@&3>",
/// format!("{} {} {}", user.mention(), channel.mention(), role.mention(),),
/// )
/// ```
fn mention(&self) -> Mention;
}
/// A struct that represents some way to insert a notification, link, or emoji into a message.
///
/// [`Display`] is the primary way of utilizing a [`Mention`], either in a [`format_args!`] type of
/// macro or with [`ToString::to_string()`]. A [`Mention`] is created using
/// [`Mentionable::mention()`], or with [`From`]/[`Into`].
///
/// [`Display`]: fmt::Display
///
/// # Examples
///
/// ```
/// # use serenity::model::id::{RoleId, ChannelId, UserId};
/// use serenity::model::mention::Mention;
/// let user = UserId::new(1);
/// let channel = ChannelId::new(2);
/// let role = RoleId::new(3);
/// assert_eq!(
/// "<@1> <#2> <@&3>",
/// format!("{} {} {}", Mention::from(user), Mention::from(channel), Mention::from(role),),
/// )
/// ```
#[derive(Clone, Copy, Debug)]
pub enum Mention {
Channel(ChannelId),
Role(RoleId),
User(UserId),
}
macro_rules! mention {
($i:ident: $($t:ty, $e:expr;)*) => {$(
impl From<$t> for Mention {
#[inline(always)]
fn from($i: $t) -> Self {
$e
}
}
)*};
}
mention!(value:
ChannelId, Mention::Channel(value);
RoleId, Mention::Role(value);
UserId, Mention::User(value);
);
impl fmt::Display for Mention {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Mention::Channel(id) => f.write_fmt(format_args!("<#{id}>")),
Mention::Role(id) => f.write_fmt(format_args!("<@&{id}>")),
Mention::User(id) => f.write_fmt(format_args!("<@{id}>")),
}
}
}
#[cfg(all(feature = "model", feature = "utils"))]
#[derive(Debug)]
pub enum MentionParseError {
InvalidMention,
}
#[cfg(all(feature = "model", feature = "utils"))]
impl fmt::Display for MentionParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid mention")
}
}
#[cfg(all(feature = "model", feature = "utils"))]
impl StdError for MentionParseError {}
#[cfg(all(feature = "model", feature = "utils"))]
impl FromStr for Mention {
type Err = MentionParseError;
fn from_str(s: &str) -> StdResult<Self, Self::Err> {
let m = if let Some(id) = utils::parse_channel_mention(s) {
id.mention()
} else if let Some(id) = utils::parse_role_mention(s) {
id.mention()
} else if let Some(id) = utils::parse_user_mention(s) {
id.mention()
} else {
return Err(MentionParseError::InvalidMention);
};
Ok(m)
}
}
impl<T> Mentionable for T
where
T: Into<Mention> + Copy,
{
fn mention(&self) -> Mention {
(*self).into()
}
}
macro_rules! mentionable {
($i:ident: $t:ty, $e:expr) => {
impl Mentionable for $t {
#[inline(always)]
fn mention(&self) -> Mention {
let $i = self;
$e.into()
}
}
};
}
#[cfg(feature = "model")]
mentionable!(value: Channel, value.id());
mentionable!(value: GuildChannel, value.id);
mentionable!(value: PrivateChannel, value.id);
mentionable!(value: Member, value.user.id);
mentionable!(value: CurrentUser, value.id);
mentionable!(value: User, value.id);
mentionable!(value: Role, value.id);
#[cfg(feature = "utils")]
#[cfg(test)]
mod test {
use crate::model::prelude::*;
#[test]
fn test_mention() {
let channel = Channel::Guild(GuildChannel {
id: ChannelId::new(4),
..Default::default()
});
let role = Role {
id: RoleId::new(2),
..Default::default()
};
let user = User {
id: UserId::new(6),
..Default::default()
};
let member = Member {
user: user.clone(),
..Default::default()
};
assert_eq!(ChannelId::new(1).mention().to_string(), "<#1>");
#[cfg(feature = "model")]
assert_eq!(channel.mention().to_string(), "<#4>");
assert_eq!(member.mention().to_string(), "<@6>");
assert_eq!(role.mention().to_string(), "<@&2>");
assert_eq!(role.id.mention().to_string(), "<@&2>");
assert_eq!(user.mention().to_string(), "<@6>");
assert_eq!(user.id.mention().to_string(), "<@6>");
}
}