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
#![cfg(feature = "cache")]
use std::fmt;
use super::ArgumentConvert;
use crate::model::prelude::*;
use crate::prelude::*;
#[non_exhaustive]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum GuildParseError {
NotFoundOrMalformed,
}
impl std::error::Error for GuildParseError {}
impl fmt::Display for GuildParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotFoundOrMalformed => f.write_str("Guild not found or unknown format"),
}
}
}
#[async_trait::async_trait]
impl ArgumentConvert for Guild {
type Err = GuildParseError;
async fn convert(
ctx: &Context,
_guild_id: Option<GuildId>,
_channel_id: Option<ChannelId>,
s: &str,
) -> Result<Self, Self::Err> {
let guilds = &ctx.cache.guilds;
let lookup_by_id = || guilds.get(&GuildId(s.parse().ok()?)).map(|g| g.clone());
let lookup_by_name = || {
guilds.iter().find_map(|m| {
let guild = m.value();
if guild.name.eq_ignore_ascii_case(s) {
Some(guild.clone())
} else {
None
}
})
};
lookup_by_id().or_else(lookup_by_name).ok_or(GuildParseError::NotFoundOrMalformed)
}
}