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

The unique Id of the user. Can be used to calculate the account's cration 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;

client.on_message(|_, msg| {
    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.");
            },
        };
    }
});

Deprecated since 0.2.0

: Use tag instead.

Alias of tag.

This is an alias of direct_message.

Examples

Sending a message:

// assuming you are in a context

let _ = message.author.dm("Hello!");

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.

Deprecated since 0.2.0

: Don't use this, since it doesn't fit serenity's design.

Gets a user by its Id over the REST API.

Note: The current user must be a bot user.

Errors

If the cache is enabled, returns a ModelError::InvalidOperationAsUser if the current user is not a bot user.

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

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:

use serenity::model::UserId;
use serenity::CACHE;
use std::thread;
use std::time::Duration;

let special_users = vec![UserId(114941315417899012), UserId(87600987040120832)];

client.on_message(|_ctx, _msg| {
    // normal message handling here
});

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

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;

client.on_message(|_, msg| {
    if msg.content == "!mytag" {
        let content = MessageBuilder::new()
            .push("Your tag is ")
            .push(Bold + msg.author.tag())
            .build();

        let _ = msg.channel_id.say(&content);
    }
});

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.

impl Display for User
[src]

Formats a string which will mention the user.