serenity/utils/argument_convert/
guild.rs

1use std::fmt;
2
3use super::ArgumentConvert;
4use crate::model::prelude::*;
5use crate::prelude::*;
6
7/// Error that can be returned from [`Guild::convert`].
8#[non_exhaustive]
9#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
10pub enum GuildParseError {
11    /// The provided guild string failed to parse, or the parsed result cannot be found in the
12    /// cache.
13    NotFoundOrMalformed,
14    /// No cache, so no guild search could be done.
15    NoCache,
16}
17
18impl std::error::Error for GuildParseError {}
19
20impl fmt::Display for GuildParseError {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        match self {
23            Self::NotFoundOrMalformed => f.write_str("Guild not found or unknown format"),
24            Self::NoCache => f.write_str("No cached list of guilds was provided"),
25        }
26    }
27}
28
29/// Look up a Guild, either by ID or by a string case-insensitively.
30///
31/// Requires the cache feature to be enabled.
32#[async_trait::async_trait]
33impl ArgumentConvert for Guild {
34    type Err = GuildParseError;
35
36    async fn convert(
37        ctx: impl CacheHttp,
38        _guild_id: Option<GuildId>,
39        _channel_id: Option<ChannelId>,
40        s: &str,
41    ) -> Result<Self, Self::Err> {
42        let guilds = &ctx.cache().ok_or(GuildParseError::NoCache)?.guilds;
43
44        let lookup_by_id = || guilds.get(&s.parse().ok()?).map(|g| g.clone());
45
46        let lookup_by_name = || {
47            guilds.iter().find_map(|m| {
48                let guild = m.value();
49                guild.name.eq_ignore_ascii_case(s).then(|| guild.clone())
50            })
51        };
52
53        lookup_by_id().or_else(lookup_by_name).ok_or(GuildParseError::NotFoundOrMalformed)
54    }
55}