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
use super::ArgumentConvert;
use crate::{model::prelude::*, prelude::*};
#[non_exhaustive]
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum RoleParseError {
NotInGuild,
NotInCache,
NotFoundOrMalformed,
}
impl std::error::Error for RoleParseError {}
impl std::fmt::Display for RoleParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::NotInGuild => f.write_str("Must invoke this operation in a guild"),
Self::NotInCache => f.write_str("Guild's roles were not found in cache"),
Self::NotFoundOrMalformed => f.write_str("Role not found or unknown format"),
}
}
}
#[cfg(feature = "cache")]
#[async_trait::async_trait]
impl ArgumentConvert for Role {
type Err = RoleParseError;
async fn convert(
ctx: &Context,
guild_id: Option<GuildId>,
_channel_id: Option<ChannelId>,
s: &str,
) -> Result<Self, Self::Err> {
let roles = ctx
.cache
.guild_roles(guild_id.ok_or(RoleParseError::NotInGuild)?)
.await
.ok_or(RoleParseError::NotInCache)?;
if let Some(role_id) = s.parse::<u64>().ok().or_else(|| crate::utils::parse_role(s)) {
if let Some(role) = roles.get(&RoleId(role_id)) {
return Ok(role.clone());
}
}
if let Some(role) = roles.values().find(|role| role.name.eq_ignore_ascii_case(s)) {
return Ok(role.clone());
}
Err(RoleParseError::NotFoundOrMalformed)
}
}