Struct serenity::model::user::User [−][src]
pub struct User { pub id: UserId, pub avatar: Option<String>, pub bot: bool, pub discriminator: u16, pub name: String, }
Information about a user.
Fields
id: UserId
The unique Id of the user. Can be used to calculate the account's creation date.
avatar: Option<String>
Optional avatar hash.
bot: bool
Indicator of whether the user is a bot.
discriminator: u16
The account's discriminator to differentiate the user from others with
the same name
. The name+discriminator pair is always unique.
name: String
The account's username. Changing username will trigger a discriminator change if the username+discriminator pair becomes non-unique.
Methods
impl User
[src]
impl User
pub fn avatar_url(&self) -> Option<String>
[src]
pub fn avatar_url(&self) -> Option<String>
Returns the formatted URL of the user's icon, if one exists.
This will produce a WEBP image URL, or GIF if the user has a GIF avatar.
pub fn create_dm_channel(&self) -> Result<PrivateChannel>
[src]
pub fn create_dm_channel(&self) -> Result<PrivateChannel>
Creates a direct message channel between the current user and the user. This can also retrieve the channel if one already exists.
pub fn created_at(&self) -> NaiveDateTime
[src]
pub fn created_at(&self) -> NaiveDateTime
Retrieves the time that this user was created at.
pub fn default_avatar_url(&self) -> String
[src]
pub fn default_avatar_url(&self) -> String
Returns the formatted URL to the user's default avatar URL.
This will produce a PNG URL.
pub fn direct_message<F>(&self, f: F) -> Result<Message> where
F: FnOnce(CreateMessage) -> CreateMessage,
[src]
pub fn direct_message<F>(&self, f: F) -> Result<Message> where
F: FnOnce(CreateMessage) -> CreateMessage,
Sends a message to a user through a direct message channel. This is a channel that can only be accessed by you and the recipient.
Examples
When a user sends a message with a content of "~help"
, DM the author a
help message, and then react with '👌'
to verify message sending:
use serenity::model::Permissions; use serenity::CACHE; struct Handler; impl EventHandler for Handler { fn message(&self, _: Context, msg: Message) { if msg.content == "~help" { let cache = CACHE.read(); let url = match cache.user.invite_url(Permissions::empty()) { Ok(v) => v, Err(why) => { println!("Error creating invite url: {:?}", why); return; }, }; let help = format!( "Helpful info here. Invite me with this link: <{}>", url, ); match msg.author.direct_message(|m| m.content(&help)) { Ok(_) => { let _ = msg.react('👌'); }, Err(why) => { println!("Err sending help: {:?}", why); let _ = msg.reply("There was an error DMing you help."); }, }; } } } let mut client = Client::new("token", Handler);
Examples
Returns a ModelError::MessagingBot
if the user being direct messaged
is a bot user.
pub fn dm<F: FnOnce(CreateMessage) -> CreateMessage>(
&self,
f: F
) -> Result<Message>
[src]
pub fn dm<F: FnOnce(CreateMessage) -> CreateMessage>(
&self,
f: F
) -> Result<Message>
This is an alias of direct_message.
Examples
Sending a message:
// assuming you are in a context let _ = message.author.dm("Hello!");
Examples
Returns a ModelError::MessagingBot
if the user being direct messaged
is a bot user.
pub fn face(&self) -> String
[src]
pub fn face(&self) -> String
Retrieves the URL to the user's avatar, falling back to the default avatar if needed.
This will call avatar_url
first, and if that returns None
, it
then falls back to default_avatar_url
.
pub fn has_role<G, R>(&self, guild: G, role: R) -> bool where
G: Into<GuildContainer>,
R: Into<RoleId>,
[src]
pub fn has_role<G, R>(&self, guild: G, role: R) -> bool where
G: Into<GuildContainer>,
R: Into<RoleId>,
Check if a user has a Role
. This will retrieve the Guild
from
the Cache
if it is available, and then check if that guild has the
given Role
.
Three forms of data may be passed in to the guild parameter: either a
PartialGuild
, a GuildId
, or a u64
.
Examples
Check if a guild has a Role
by Id:
// Assumes a 'guild_id' and `role_id` have already been bound let _ = message.author.has_role(guild_id, role_id);
pub fn refresh(&mut self) -> Result<()>
[src]
pub fn refresh(&mut self) -> Result<()>
Refreshes the information about the user.
Replaces the instance with the data retrieved over the REST API.
Examples
If maintaing a very long-running bot, you may want to periodically refresh information about certain users if the state becomes out-of-sync:
struct Handler; impl EventHandler for Handler { fn message(&self, _: Context, _: Message) { // normal message handling here } } let mut client = Client::new("token", Handler).unwrap(); use serenity::model::id::UserId; use serenity::CACHE; use std::thread; use std::time::Duration; let special_users = vec![UserId(114941315417899012), UserId(87600987040120832)]; // start a new thread to periodically refresh the special users' data // every 12 hours let handle = thread::spawn(move || { // 12 hours in seconds let duration = Duration::from_secs(43200); loop { thread::sleep(duration); let cache = CACHE.read(); for id in &special_users { if let Some(user) = cache.user(*id) { if let Err(why) = user.write().refresh() { println!("Error refreshing {}: {:?}", id, why); } } } } }); println!("{:?}", client.start());
pub fn static_avatar_url(&self) -> Option<String>
[src]
pub fn static_avatar_url(&self) -> Option<String>
Returns a static formatted URL of the user's icon, if one exists.
This will always produce a WEBP image URL.
pub fn tag(&self) -> String
[src]
pub fn tag(&self) -> String
Returns the "tag" for the user.
The "tag" is defined as "username#discriminator", such as "zeyla#5479".
Examples
Make a command to tell the user what their tag is:
use serenity::utils::MessageBuilder; use serenity::utils::ContentModifier::Bold; struct Handler; impl EventHandler for Handler { fn message(&self, _: Context, msg: Message) { if msg.content == "!mytag" { let content = MessageBuilder::new() .push("Your tag is ") .push(Bold + msg.author.tag()) .build(); let _ = msg.channel_id.say(&content); } } } let mut client = Client::new("token", Handler).unwrap(); client.start().unwrap();
Trait Implementations
impl Mentionable for User
[src]
impl Mentionable for User
fn mention(&self) -> String
[src]
fn mention(&self) -> String
Creates a mentionable string, that will be able to notify and/or create a link to the item. Read more
impl FromStr for User
[src]
impl FromStr for User
type Err = UserParseError
The associated error which can be returned from parsing.
fn from_str(s: &str) -> StdResult<Self, Self::Err>
[src]
fn from_str(s: &str) -> StdResult<Self, Self::Err>
Parses a string s
to return a value of this type. Read more
impl Clone for User
[src]
impl Clone for User
fn clone(&self) -> User
[src]
fn clone(&self) -> User
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl Debug for User
[src]
impl Debug for User
fn fmt(&self, f: &mut Formatter) -> Result
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter. Read more
impl PartialEq for User
[src]
impl PartialEq for User
fn eq(&self, other: &Self) -> bool
[src]
fn eq(&self, other: &Self) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
fn ne(&self, other: &Rhs) -> bool
This method tests for !=
.
impl Eq for User
[src]
impl Eq for User
impl Hash for User
[src]
impl Hash for User
fn hash<H: Hasher>(&self, hasher: &mut H)
[src]
fn hash<H: Hasher>(&self, hasher: &mut H)
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0[src]
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl Display for User
[src]
impl Display for User
impl From<CurrentUser> for User
[src]
impl From<CurrentUser> for User
fn from(user: CurrentUser) -> Self
[src]
fn from(user: CurrentUser) -> Self
Performs the conversion.
impl<'a> From<&'a CurrentUser> for User
[src]
impl<'a> From<&'a CurrentUser> for User
fn from(user: &'a CurrentUser) -> Self
[src]
fn from(user: &'a CurrentUser) -> Self
Performs the conversion.
impl From<User> for UserId
[src]
impl From<User> for UserId
impl<'a> From<&'a User> for UserId
[src]
impl<'a> From<&'a User> for UserId