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
use super::ArgumentConvert;
use crate::{model::prelude::*, prelude::*};
#[non_exhaustive]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum UserParseError {
NotFoundOrMalformed,
}
impl std::error::Error for UserParseError {}
impl std::fmt::Display for UserParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NotFoundOrMalformed => f.write_str("User not found or unknown format"),
}
}
}
#[cfg(feature = "cache")]
#[async_trait::async_trait]
impl ArgumentConvert for User {
type Err = UserParseError;
async fn convert(
ctx: &Context,
guild_id: Option<GuildId>,
channel_id: Option<ChannelId>,
s: &str,
) -> Result<Self, Self::Err> {
let users = ctx.cache.users.read().await;
let lookup_by_id = || users.get(&UserId(s.parse().ok()?));
let lookup_by_mention = || users.get(&UserId(crate::utils::parse_username(s)?));
let lookup_by_name_and_discrim = || {
let (name, discrim) = crate::utils::parse_user_tag(s)?;
users
.values()
.find(|user| user.discriminator == discrim && user.name.eq_ignore_ascii_case(name))
};
let lookup_by_name = || users.values().find(|user| user.name == s);
if let Some(user) = lookup_by_id()
.or_else(lookup_by_mention)
.or_else(lookup_by_name_and_discrim)
.or_else(lookup_by_name)
{
return Ok(user.clone());
}
if let Ok(member) = Member::convert(ctx, guild_id, channel_id, s).await {
return Ok(member.user);
}
Err(UserParseError::NotFoundOrMalformed)
}
}