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

The unique Id of the user. Can be used to calculate the account's creation date.

Optional avatar hash.

Indicator of whether the user is a bot.

The account's discriminator to differentiate the user from others with the same name. The name+discriminator pair is always unique.

The account's username. Changing username will trigger a discriminator change if the username+discriminator pair becomes non-unique.

Methods

impl User
[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.

Creates a direct message channel between the current user and the user. This can also retrieve the channel if one already exists.

Retrieves the time that this user was created at.

Returns the formatted URL to the user's default avatar URL.

This will produce a PNG URL.

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.

This is an alias of direct_message.

Examples

Sending a message:

This example is not tested
// 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.

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.

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:

This example is not tested
// Assumes a 'guild_id' and `role_id` have already been bound
let _ = message.author.has_role(guild_id, role_id);

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());

Returns a static formatted URL of the user's icon, if one exists.

This will always produce a WEBP image URL.

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]

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]

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

impl Clone for User
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl Debug for User
[src]

Formats the value using the given formatter. Read more

impl PartialEq for User
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Eq for User
[src]

impl Hash for User
[src]

Feeds this value into the given [Hasher]. Read more

Feeds a slice of this type into the given [Hasher]. Read more

impl Display for User
[src]

Formats a string which will mention the user.

impl From<CurrentUser> for User
[src]

Performs the conversion.

impl<'a> From<&'a CurrentUser> for User
[src]

Performs the conversion.

impl From<User> for UserId
[src]

Gets the Id of a User.

impl<'a> From<&'a User> for UserId
[src]

Gets the Id of a User.

Auto Trait Implementations

impl Send for User

impl Sync for User