Struct serenity::model::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 cration 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]
fn avatar_url(&self) -> Option<String>
[src]
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.
fn create_dm_channel(&self) -> Result<PrivateChannel>
[src]
Creates a direct message channel between the current user and the user. This can also retrieve the channel if one already exists.
fn created_at(&self) -> NaiveDateTime
[src]
Retrieves the time that this user was created at.
fn default_avatar_url(&self) -> String
[src]
Returns the formatted URL to the user's default avatar URL.
This will produce a PNG URL.
fn direct_message<F>(&self, f: F) -> Result<Message> where
F: FnOnce(CreateMessage) -> CreateMessage,
[src]
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 on_message(&self, _: Context, msg: Message) { if msg.content == "~help" { let url = match CACHE.read() { Ok(v) => { match v.user.invite_url(Permissions::empty()) { Ok(v) => v, Err(why) => { println!("Error creating invite url: {:?}", why); return; }, } }, Err(why) => { println!("Error reading from CACHE: {:?}", 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.
fn dm<F: FnOnce(CreateMessage) -> CreateMessage>(&self, f: F) -> Result<Message>
[src]
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.
fn face(&self) -> String
[src]
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
.
fn has_role<G, R>(&self, guild: G, role: R) -> bool where
G: Into<GuildContainer>,
R: Into<RoleId>,
[src]
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
Guild
itself, a GuildId
, or a u64
.
Examples
Check if a guild has a Role
by Id:
// Assumes a 'guild' and `role_id` have already been bound let _ = message.author.has_role(guild, role_id);
fn refresh(&mut self) -> Result<()>
[src]
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 on_message(&self, _: Context, _: Message) { // normal message handling here } } let mut client = Client::new("token", Handler); use serenity::model::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().unwrap(); for id in &special_users { if let Some(user) = cache.user(*id) { if let Err(why) = user.write().unwrap().refresh() { println!("Error refreshing {}: {:?}", id, why); } } } } }); println!("{:?}", client.start());
fn static_avatar_url(&self) -> Option<String>
[src]
Returns a static formatted URL of the user's icon, if one exists.
This will always produce a WEBP image URL.
fn tag(&self) -> String
[src]
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 on_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); client.start().unwrap();
Trait Implementations
impl Mentionable for User
[src]
fn mention(&self) -> String
[src]
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]
type Err = UserParseError
The associated error which can be returned from parsing.
fn from_str(s: &str) -> StdResult<Self, Self::Err>
[src]
Parses a string s
to return a value of this type. Read more
impl Clone for User
[src]
fn clone(&self) -> User
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Debug for User
[src]
impl Eq for User
[src]
impl PartialEq for User
[src]
fn eq(&self, __arg_0: &User) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &User) -> bool
[src]
This method tests for !=
.
impl Hash for User
[src]
fn hash<__H: Hasher>(&self, __arg_0: &mut __H)
[src]
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]
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more