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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
//! User information-related models. #[cfg(feature = "http")] use crate::http::CacheHttp; use serde_json; use std::fmt; use super::utils::deserialize_u16; use super::prelude::*; use crate::{internal::prelude::*, model::misc::Mentionable}; #[cfg(feature = "model")] use crate::builder::{CreateMessage, EditProfile}; #[cfg(feature = "model")] use crate::http::GuildPagination; #[cfg(all(feature = "cache", feature = "model"))] use parking_lot::RwLock; #[cfg(feature = "model")] use std::fmt::Write; #[cfg(feature = "model")] use std::mem; #[cfg(all(feature = "cache", feature = "model"))] use crate::cache::CacheRwLock; #[cfg(all( feature = "model", any( all(feature = "builder", feature = "client"), feature = "http", ), ))] use serde_json::json; #[cfg(all(feature = "cache", feature = "model"))] use std::sync::Arc; #[cfg(feature = "model")] use crate::utils; #[cfg(feature = "http")] use crate::http::Http; /// Information about the current user. #[derive(Clone, Default, Debug, Deserialize, Serialize)] pub struct CurrentUser { pub id: UserId, pub avatar: Option<String>, #[serde(default)] pub bot: bool, #[serde(deserialize_with = "deserialize_u16")] pub discriminator: u16, pub email: Option<String>, pub mfa_enabled: bool, #[serde(rename = "username")] pub name: String, pub verified: Option<bool>, #[serde(skip)] pub(crate) _nonexhaustive: (), } #[cfg(feature = "model")] impl CurrentUser { /// 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. /// /// # Examples /// /// Print out the current user's avatar url if one is set: /// /// ```rust,no_run /// # #[cfg(feature = "cache")] /// # fn main() { /// # use serenity::{cache::{Cache, CacheRwLock}, model::prelude::*, prelude::*}; /// # use parking_lot::RwLock; /// # use std::sync::Arc; /// # /// # let cache: CacheRwLock = Arc::new(RwLock::new(Cache::default())).into(); /// # let cache = cache.read(); /// // assuming the cache has been unlocked /// let user = &cache.user; /// /// match user.avatar_url() { /// Some(url) => println!("{}'s avatar can be found at {}", user.name, url), /// None => println!("{} does not have an avatar set.", user.name) /// } /// # } /// # /// # #[cfg(not(feature = "cache"))] /// # fn main() {} /// ``` #[inline] pub fn avatar_url(&self) -> Option<String> { avatar_url(self.id, self.avatar.as_ref()) } /// Returns the formatted URL to the user's default avatar URL. /// /// This will produce a PNG URL. #[inline] pub fn default_avatar_url(&self) -> String { default_avatar_url(self.discriminator) } /// Edits the current user's profile settings. /// /// This mutates the current user in-place. /// /// Refer to [`EditProfile`]'s documentation for its methods. /// /// # Examples /// /// Change the avatar: /// /// ```rust,ignore /// # #[cfg(feature = "cache")] /// # fn main() { /// let avatar = serenity::utils::read_image("./avatar.png").unwrap(); /// /// context.cache.write().user.edit(|p| p.avatar(Some(&avatar))); /// # } /// # /// # #[cfg(not(feature = "cache"))] /// # fn main() {} /// ``` /// /// [`EditProfile`]: ../../builder/struct.EditProfile.html #[cfg(feature = "http")] pub fn edit<F>(&mut self, http: impl AsRef<Http>, f: F) -> Result<()> where F: FnOnce(&mut EditProfile) -> &mut EditProfile { let mut map = HashMap::new(); map.insert("username", Value::String(self.name.clone())); if let Some(email) = self.email.as_ref() { map.insert("email", Value::String(email.clone())); } let mut edit_profile = EditProfile(map); f(&mut edit_profile); let map = utils::hashmap_to_json_map(edit_profile.0); match http.as_ref().edit_profile(&map) { Ok(new) => { let _ = mem::replace(self, new); Ok(()) }, Err(why) => Err(why), } } /// Retrieves the URL to the current 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`]. /// /// [`avatar_url`]: #method.avatar_url /// [`default_avatar_url`]: #method.default_avatar_url pub fn face(&self) -> String { self.avatar_url() .unwrap_or_else(|| self.default_avatar_url()) } /// Gets a list of guilds that the current user is in. /// /// # Examples /// /// Print out the names of all guilds the current user is in: /// /// ```rust,no_run /// # #[cfg(feature = "cache")] /// # fn main() { /// # use serenity::{cache::{Cache, CacheRwLock}, http::Http, model::prelude::*, prelude::*}; /// # use parking_lot::RwLock; /// # use std::sync::Arc; /// # /// # let cache: CacheRwLock = Arc::new(RwLock::new(Cache::default())).into(); /// # let cache = cache.read(); /// # let http = Arc::new(Http::default()); /// // assuming the cache has been unlocked /// let user = &cache.user; /// /// if let Ok(guilds) = user.guilds(&http) { /// for (index, guild) in guilds.into_iter().enumerate() { /// println!("{}: {}", index, guild.name); /// } /// } /// # } /// # /// # #[cfg(not(feature = "cache"))] /// # fn main() {} /// ``` #[cfg(feature = "http")] pub fn guilds(&self, http: impl AsRef<Http>) -> Result<Vec<GuildInfo>> { http.as_ref().get_guilds(&GuildPagination::After(GuildId(1)), 100) } /// Returns the invite url for the bot with the given permissions. /// /// This queries the REST API for the client id. /// /// If the permissions passed are empty, the permissions part will be dropped. /// /// # Examples /// /// Get the invite url with no permissions set: /// /// ```rust,no_run /// # #[cfg(feature = "cache")] /// # fn main() { /// # use serenity::{cache::{Cache, CacheRwLock}, http::Http, model::prelude::*, prelude::*}; /// # use parking_lot::RwLock; /// # use std::sync::Arc; /// # /// # let cache: CacheRwLock = Arc::new(RwLock::new(Cache::default())).into(); /// # let mut cache = cache.write(); /// # let http = Arc::new(Http::default()); /// /// use serenity::model::Permissions; /// /// // assuming the cache has been unlocked /// let url = match cache.user.invite_url(&http, Permissions::empty()) { /// Ok(v) => v, /// Err(why) => { /// println!("Error getting invite url: {:?}", why); /// /// return; /// }, /// }; /// /// assert_eq!(url, "https://discordapp.com/api/oauth2/authorize? \ /// client_id=249608697955745802&scope=bot"); /// # } /// # /// # #[cfg(not(feature = "cache"))] /// # fn main() {} /// ``` /// /// Get the invite url with some basic permissions set: /// /// ```rust,no_run /// # #[cfg(feature = "cache")] /// # fn main() { /// # use serenity::{cache::{Cache, CacheRwLock}, http::Http, model::prelude::*, prelude::*}; /// # use parking_lot::RwLock; /// # use std::sync::Arc; /// # /// # let cache: CacheRwLock = Arc::new(RwLock::new(Cache::default())).into(); /// # let mut cache = cache.write(); /// # let http = Arc::new(Http::default()); /// use serenity::model::Permissions; /// /// // assuming the cache has been unlocked /// let url = match cache.user.invite_url(&http, Permissions::READ_MESSAGES | Permissions::SEND_MESSAGES | Permissions::EMBED_LINKS) { /// Ok(v) => v, /// Err(why) => { /// println!("Error getting invite url: {:?}", why); /// /// return; /// }, /// }; /// /// assert_eq!(url, /// "https://discordapp. /// com/api/oauth2/authorize?client_id=249608697955745802&scope=bot&permissions=19456"); /// # } /// # /// # #[cfg(not(feature = "cache"))] /// # fn main() {} /// ``` /// /// # Errors /// /// Returns an /// [`HttpError::UnsuccessfulRequest(Unauthorized)`][`HttpError::UnsuccessfulRequest`] /// If the user is not authorized for this end point. /// /// May return [`Error::Format`] while writing url to the buffer. /// /// [`Error::Format`]: ../../enum.Error.html#variant.Format /// [`HttpError::UnsuccessfulRequest`]: ../../http/enum.HttpError.html#variant.UnsuccessfulRequest #[cfg(feature = "http")] pub fn invite_url(&self, http: impl AsRef<Http>, permissions: Permissions) -> Result<String> { let bits = permissions.bits(); let client_id = http.as_ref().get_current_application_info().map(|v| v.id)?; let mut url = format!( "https://discordapp.com/api/oauth2/authorize?client_id={}&scope=bot", client_id ); if bits != 0 { write!(url, "&permissions={}", bits)?; } Ok(url) } /// Returns a static formatted URL of the user's icon, if one exists. /// /// This will always produce a WEBP image URL. /// /// # Examples /// /// Print out the current user's static avatar url if one is set: /// /// ```rust,no_run /// # #[cfg(feature = "cache")] /// # fn main() { /// # use serenity::{cache::{Cache, CacheRwLock}, model::prelude::*, prelude::*}; /// # use parking_lot::RwLock; /// # use std::sync::Arc; /// # /// # let cache: CacheRwLock = Arc::new(RwLock::new(Cache::default())).into(); /// # let cache = cache.read(); /// // assuming the cache has been unlocked /// let user = &cache.user; /// /// match user.static_avatar_url() { /// Some(url) => println!("{}'s static avatar can be found at {}", user.name, url), /// None => println!("Could not get static avatar for {}.", user.name) /// } /// # } /// # /// # #[cfg(not(feature = "cache"))] /// # fn main() {} /// ``` #[inline] pub fn static_avatar_url(&self) -> Option<String> { static_avatar_url(self.id, self.avatar.as_ref()) } /// Returns the tag of the current user. /// /// # Examples /// /// Print out the current user's distinct identifier (e.g., Username#1234): /// /// ```rust,no_run /// # #[cfg(feature = "cache")] /// # fn main() { /// # use serenity::{cache::{Cache, CacheRwLock}, model::prelude::*, prelude::*}; /// # use parking_lot::RwLock; /// # use std::sync::Arc; /// # /// # let cache: CacheRwLock = Arc::new(RwLock::new(Cache::default())).into(); /// # let cache = cache.read(); /// // assuming the cache has been unlocked /// println!("The current user's distinct identifier is {}", cache.user.tag()); /// # } /// # /// # #[cfg(not(feature = "cache"))] /// # fn main() {} /// ``` #[inline] pub fn tag(&self) -> String { tag(&self.name, self.discriminator) } } /// An enum that represents a default avatar. /// /// The default avatar is calculated via the result of `discriminator % 5`. /// /// The has of the avatar can be retrieved via calling [`name`] on the enum. /// /// [`name`]: #method.name #[derive(Copy, Clone, Debug, Deserialize, Hash, Eq, PartialEq, PartialOrd, Ord, Serialize)] pub enum DefaultAvatar { /// The avatar when the result is `0`. #[serde(rename = "6debd47ed13483642cf09e832ed0bc1b")] Blurple, /// The avatar when the result is `1`. #[serde(rename = "322c936a8c8be1b803cd94861bdfa868")] Grey, /// The avatar when the result is `2`. #[serde(rename = "dd4dbc0016779df1378e7812eabaa04d")] Green, /// The avatar when the result is `3`. #[serde(rename = "0e291f67c9274a1abdddeb3fd919cbaa")] Orange, /// The avatar when the result is `4`. #[serde(rename = "1cbd08c76f8af6dddce02c5138971129")] Red, #[doc(hidden)] __Nonexhaustive, } impl DefaultAvatar { /// Retrieves the String hash of the default avatar. pub fn name(self) -> Result<String> { serde_json::to_string(&self).map_err(From::from) } } /// The representation of a user's status. /// /// # Examples /// /// - [`DoNotDisturb`]; /// - [`Invisible`]. /// /// [`DoNotDisturb`]: #variant.DoNotDisturb /// [`Invisible`]: #variant.Invisible #[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize)] pub enum OnlineStatus { #[serde(rename = "dnd")] DoNotDisturb, #[serde(rename = "idle")] Idle, #[serde(rename = "invisible")] Invisible, #[serde(rename = "offline")] Offline, #[serde(rename = "online")] Online, #[doc(hidden)] __Nonexhaustive, } impl OnlineStatus { pub fn name(&self) -> &str { match *self { OnlineStatus::DoNotDisturb => "dnd", OnlineStatus::Idle => "idle", OnlineStatus::Invisible => "invisible", OnlineStatus::Offline => "offline", OnlineStatus::Online => "online", OnlineStatus::__Nonexhaustive => unreachable!(), } } } impl Default for OnlineStatus { fn default() -> OnlineStatus { OnlineStatus::Online } } /// Information about a user. #[derive(Clone, Debug, Deserialize, Serialize)] pub struct User { /// The unique Id of the user. Can be used to calculate the account's /// creation date. pub id: UserId, /// Optional avatar hash. pub avatar: Option<String>, /// Indicator of whether the user is a bot. #[serde(default)] pub bot: bool, /// The account's discriminator to differentiate the user from others with /// the same [`name`]. The name+discriminator pair is always unique. /// /// [`name`]: #structfield.name #[serde(deserialize_with = "deserialize_u16")] pub discriminator: u16, /// The account's username. Changing username will trigger a discriminator /// change if the username+discriminator pair becomes non-unique. #[serde(rename = "username")] pub name: String, #[serde(skip)] pub(crate) _nonexhaustive: (), } use std::hash::{Hash, Hasher}; #[cfg(feature = "model")] use chrono::{DateTime, FixedOffset}; impl PartialEq for User { fn eq(&self, other: &Self) -> bool { self.id == other.id } } impl Eq for User {} impl Hash for User { fn hash<H: Hasher>(&self, hasher: &mut H) { self.id.hash(hasher); } } #[cfg(feature = "model")] impl User { /// 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. #[inline] pub fn avatar_url(&self) -> Option<String> { avatar_url(self.id, self.avatar.as_ref()) } /// Creates a direct message channel between the [current user] and the /// user. This can also retrieve the channel if one already exists. /// /// [current user]: struct.CurrentUser.html #[inline] #[cfg(feature = "http")] pub fn create_dm_channel(&self, http: impl AsRef<Http>) -> Result<PrivateChannel> { self.id.create_dm_channel(&http) } /// Retrieves the time that this user was created at. #[inline] pub fn created_at(&self) -> DateTime<FixedOffset> { self.id.created_at() } /// Returns the formatted URL to the user's default avatar URL. /// /// This will produce a PNG URL. #[inline] pub fn default_avatar_url(&self) -> String { default_avatar_url(self.discriminator) } /// 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: /// /// ```rust,no_run /// # #[cfg(feature="client")] { /// # use serenity::prelude::*; /// # use serenity::model::prelude::*; /// # /// use serenity::model::Permissions; /// /// struct Handler; /// /// impl EventHandler for Handler { /// # #[cfg(feature = "cache")] /// fn message(&self, ctx: Context, msg: Message) { /// if msg.content == "~help" { /// let url = match ctx.cache.read().user.invite_url(&ctx, 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, /// ); /// /// let dm = msg.author.direct_message(&ctx, |m| { /// m.content(&help) /// }); /// /// match dm { /// Ok(_) => { /// let _ = msg.react(&ctx, '👌'); /// }, /// Err(why) => { /// println!("Err sending help: {:?}", why); /// /// let _ = msg.reply(&ctx, "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. /// /// [`ModelError::MessagingBot`]: ../error/enum.Error.html#variant.MessagingBot /// [`PrivateChannel`]: struct.PrivateChannel.html /// [`User::dm`]: struct.User.html#method.dm // A tale with Clippy: // // A person named Clippy once asked you to unlock a box and take something // from it, but you never re-locked it, so you'll die and the universe will // implode because the box must remain locked unless you're there, and you // can't just borrow that item from it and take it with you forever. // // Instead what you do is unlock the box, take the item out of it, make a // copy of said item, and then re-lock the box, and take your copy of the // item with you. // // The universe is still fine, and nothing implodes. // // (AKA: Clippy is wrong and so we have to mark as allowing this lint.) #[allow(clippy::let_and_return)] #[cfg(all(feature = "builder", feature = "client"))] pub fn direct_message<F>(&self, cache_http: impl CacheHttp, f: F) -> Result<Message> where for <'a, 'b> F: FnOnce(&'b mut CreateMessage<'a>) -> &'b mut CreateMessage<'a> { if self.bot { return Err(Error::Model(ModelError::MessagingBot)); } let mut private_channel_id = None; #[cfg(feature = "cache")] { if let Some(cache) = cache_http.cache() { private_channel_id = cache.read().private_channels .values() .map(|ch| ch.read()) .find(|ch| ch.recipient.read().id == self.id) .map(|ch| ch.id); } } let private_channel_id = match private_channel_id { Some(id) => id, None => { let map = json!({ "recipient_id": self.id.0, }); cache_http.http().create_private_channel(&map)?.id } }; private_channel_id.send_message(&cache_http.http(), f) } /// This is an alias of [direct_message]. /// /// # Examples /// /// Sending a message: /// /// ```rust,ignore /// // 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. /// /// [`ModelError::MessagingBot`]: ../error/enum.Error.html#variant.MessagingBot /// [direct_message]: #method.direct_message #[cfg(all(feature = "builder", feature = "client"))] #[inline] pub fn dm<F>(&self, cache_http: impl CacheHttp, f: F) -> Result<Message> where for <'a, 'b> F: FnOnce(&'b mut CreateMessage<'a>) -> &'b mut CreateMessage<'a> { self.direct_message(cache_http, f) } /// 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`]. /// /// [`avatar_url`]: #method.avatar_url /// [`default_avatar_url`]: #method.default_avatar_url pub fn face(&self) -> String { self.avatar_url() .unwrap_or_else(|| self.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: /// /// ```rust,ignore /// // Assumes a 'guild_id' and `role_id` have already been bound /// let _ = message.author.has_role(guild_id, role_id); /// ``` /// /// [`Guild`]: ../guild/struct.Guild.html /// [`GuildId`]: ../id/struct.GuildId.html /// [`PartialGuild`]: ../guild/struct.PartialGuild.html /// [`Role`]: ../guild/struct.Role.html /// [`Cache`]: ../../cache/struct.Cache.html #[cfg(feature = "client")] pub fn has_role<G, R>(&self, cache_http: impl CacheHttp, guild: G, role: R) -> Result<bool> where G: Into<GuildContainer>, R: Into<RoleId> { self._has_role(cache_http, guild.into(), role.into()) } #[cfg(feature = "client")] fn _has_role( &self, cache_http: impl CacheHttp, guild: GuildContainer, role: RoleId ) -> Result<bool> { match guild { GuildContainer::Guild(partial_guild) => { self._has_role( cache_http, GuildContainer::Id(partial_guild.id), role ) }, GuildContainer::Id(guild_id) => { let mut has_role = None; #[cfg(feature = "cache")] { if let Some(cache) = cache_http.cache() { has_role = cache.read() .guilds .get(&guild_id) .and_then(|g| { g.read().members.get(&self.id) .map(|m| m.roles.contains(&role)) }); } } #[cfg(feature = "http")] { if let Some(has_role) = has_role { Ok(has_role) } else { cache_http.http() .get_member(guild_id.0, self.id.0) .map(|m| m.roles.contains(&role)) } } #[cfg(not(feature = "http"))] { Err(Error::Model(ModelError::ItemMissing)) } }, GuildContainer::__Nonexhaustive => unreachable!(), } } /// Refreshes the information about the user. /// /// Replaces the instance with the data retrieved over the REST API. #[cfg(feature = "http")] pub fn refresh(&mut self, cache_http: impl CacheHttp) -> Result<()> { self.id.to_user(cache_http).map(|replacement| { mem::replace(self, replacement); }) } /// Returns a static formatted URL of the user's icon, if one exists. /// /// This will always produce a WEBP image URL. #[inline] pub fn static_avatar_url(&self) -> Option<String> { static_avatar_url(self.id, self.avatar.as_ref()) } /// 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: /// /// ```rust,no_run /// # #[cfg(feature = "client")] /// # fn main() { /// # use serenity::prelude::*; /// # use serenity::model::prelude::*; /// # /// use serenity::utils::MessageBuilder; /// use serenity::utils::ContentModifier::Bold; /// /// struct Handler; /// /// impl EventHandler for Handler { /// fn message(&self, context: 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(&context.http, &content); /// } /// } /// } /// let mut client = Client::new("token", Handler).unwrap(); /// /// client.start().unwrap(); /// # } /// # /// # #[cfg(not(feature = "client"))] /// # fn main() {} /// ``` #[inline] pub fn tag(&self) -> String { tag(&self.name, self.discriminator) } /// Returns the user's nickname in the given `guild_id`. /// /// If none is used, it returns `None`. #[inline] #[cfg(feature = "http")] pub fn nick_in<G>(&self, cache_http: impl CacheHttp, guild_id: G) -> Option<String> where G: Into<GuildId> { self._nick_in(cache_http, guild_id.into()) } #[cfg(feature = "http")] fn _nick_in(&self, cache_http: impl CacheHttp, guild_id: GuildId) -> Option<String> { #[cfg(feature = "cache")] { if let Some(cache) = cache_http.cache() { return guild_id.to_guild_cached(cache).and_then(|guild| { guild.read().members.get(&self.id).and_then(|member| member.nick.clone()) }); } } guild_id.member(cache_http, &self.id).ok().and_then(|member| member.nick.clone()) } } impl fmt::Display for User { /// Formats a string which will mention the user. // This is in the format of: `<@USER_ID>` fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.id.mention(), f) } } #[cfg(feature = "model")] impl UserId { /// Creates a direct message channel between the [current user] and the /// user. This can also retrieve the channel if one already exists. /// /// [current user]: ../user/struct.CurrentUser.html #[cfg(feature = "http")] pub fn create_dm_channel(self, http: impl AsRef<Http>) -> Result<PrivateChannel> { let map = json!({ "recipient_id": self.0, }); http.as_ref().create_private_channel(&map) } /// Attempts to find a [`User`] by its Id in the cache. /// /// [`User`]: ../user/struct.User.html #[cfg(feature = "cache")] #[inline] pub fn to_user_cached(self, cache: impl AsRef<CacheRwLock>) -> Option<Arc<RwLock<User>>> {cache.as_ref().read().user(self) } /// First attempts to find a [`User`] by its Id in the cache, /// upon failure requests it via the REST API. /// /// **Note**: If the cache is not enabled, /// REST API will be used only. /// /// [`User`]: ../user/struct.User.html #[cfg(feature = "http")] #[inline] pub fn to_user(self, cache_http: impl CacheHttp) -> Result<User> { #[cfg(feature = "cache")] { if let Some(cache) = cache_http.cache() { if let Some(user) = cache.read().user(self) { return Ok(user.read().clone()); } } } cache_http.http().get_user(self.0) } } impl From<CurrentUser> for User { fn from(user: CurrentUser) -> Self { Self { avatar: user.avatar, bot: user.bot, discriminator: user.discriminator, id: user.id, name: user.name, _nonexhaustive: (), } } } impl<'a> From<&'a CurrentUser> for User { fn from(user: &'a CurrentUser) -> Self { Self { avatar: user.avatar.clone(), bot: user.bot, discriminator: user.discriminator, id: user.id, name: user.name.clone(), _nonexhaustive: (), } } } impl From<CurrentUser> for UserId { /// Gets the Id of a `CurrentUser` struct. fn from(current_user: CurrentUser) -> UserId { current_user.id } } impl<'a> From<&'a CurrentUser> for UserId { /// Gets the Id of a `CurrentUser` struct. fn from(current_user: &CurrentUser) -> UserId { current_user.id } } impl From<Member> for UserId { /// Gets the Id of a `Member`. fn from(member: Member) -> UserId { member.user.read().id } } impl<'a> From<&'a Member> for UserId { /// Gets the Id of a `Member`. fn from(member: &Member) -> UserId { member.user.read().id } } impl From<User> for UserId { /// Gets the Id of a `User`. fn from(user: User) -> UserId { user.id } } impl<'a> From<&'a User> for UserId { /// Gets the Id of a `User`. fn from(user: &User) -> UserId { user.id } } #[cfg(feature = "model")] fn avatar_url(user_id: UserId, hash: Option<&String>) -> Option<String> { hash.map(|hash| { let ext = if hash.starts_with("a_") { "gif" } else { "webp" }; cdn!("/avatars/{}/{}.{}?size=1024", user_id.0, hash, ext) }) } #[cfg(feature = "model")] fn default_avatar_url(discriminator: u16) -> String { cdn!("/embed/avatars/{}.png", discriminator % 5u16) } #[cfg(feature = "model")] fn static_avatar_url(user_id: UserId, hash: Option<&String>) -> Option<String> { hash.map(|hash| cdn!("/avatars/{}/{}.webp?size=1024", user_id, hash)) } #[cfg(feature = "model")] fn tag(name: &str, discriminator: u16) -> String { // 32: max length of username // 1: `#` // 4: max length of discriminator let mut tag = String::with_capacity(37); tag.push_str(name); tag.push('#'); let _ = write!(tag, "{:04}", discriminator); tag } #[cfg(test)] mod test { #[cfg(feature = "model")] mod model { use crate::model::id::UserId; use crate::model::user::User; fn gen() -> User { User { id: UserId(210), avatar: Some("abc".to_string()), bot: true, discriminator: 1432, name: "test".to_string(), _nonexhaustive: (), } } #[test] fn test_core() { let mut user = gen(); assert!( user.avatar_url() .unwrap() .ends_with("/avatars/210/abc.webp?size=1024") ); assert!( user.static_avatar_url() .unwrap() .ends_with("/avatars/210/abc.webp?size=1024") ); user.avatar = Some("a_aaa".to_string()); assert!( user.avatar_url() .unwrap() .ends_with("/avatars/210/a_aaa.gif?size=1024") ); assert!( user.static_avatar_url() .unwrap() .ends_with("/avatars/210/a_aaa.webp?size=1024") ); user.avatar = None; assert!(user.avatar_url().is_none()); assert_eq!(user.tag(), "test#1432"); } #[test] fn default_avatars() { let mut user = gen(); user.discriminator = 0; assert!(user.default_avatar_url().ends_with("0.png")); user.discriminator = 1; assert!(user.default_avatar_url().ends_with("1.png")); user.discriminator = 2; assert!(user.default_avatar_url().ends_with("2.png")); user.discriminator = 3; assert!(user.default_avatar_url().ends_with("3.png")); user.discriminator = 4; assert!(user.default_avatar_url().ends_with("4.png")); } } }