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
//! Provides a trait to convert strings into serenity's guild-specific models.
//!
//! The trait provides two methods:
//! - [`from_guild_and_str`]
//! - [`from_guild_id_and_str`]
//!
//! The first method is available only when `cache` feature is enabled. The
//! second method is always available.
//!
//! ## Limitation
//!
//! If the `cache` feature is not enabled, an argument is only treated as an ID
//! or mention when trying to convert to `Member`. It is not treated as user
//! name, nickname or user tag.
//!
//! ## Example
//!
//! Bring [`Conversion`] trait into scope.
//!
//! ```
//! use serenity_utils::conversion::Conversion;
//! ```
//!
//! If cache is enabled and you require `Guild` for other purposes too,
//! use [`from_guild_and_str`] method so the guild is not fetched multiple times.
//!
//! ```
//! # use serenity::{model::prelude::{Guild, Member, Message}, prelude::Context};
//! # #[allow(deprecated)]
//! # use serenity_utils::conversion::Conversion;
//! #
//! async fn foo(ctx: &Context, msg: &Message, arg: &str) {
//!     let guild = match msg.guild(&ctx.cache) {
//!         Some(g) => g,
//!         None => return,
//!     };
//!
//!     // Tries to get member from guild and the argument.
//! #   #[allow(deprecated)]
//!     let opt_member = Member::from_guild_and_str(&guild, arg).await;
//! }
//! ```
//!
//! If cache is disabled or if cache is enabled but you don't require `Guild`
//! for other purposes, use [`from_guild_id_and_str`] method.
//!
//! ```
//! # use serenity::{model::prelude::{Guild, Message, Role}, prelude::Context};
//! # use serenity_utils::conversion::Conversion;
//! #
//! async fn bar(ctx: &Context, msg: &Message, arg: &str) {
//!     // Tries to get role from guild id and the argument.
//!     if let Some(guild_id) = msg.guild_id {
//! #      #[allow(deprecated)]
//!         let opt_role = Role::from_guild_id_and_str(ctx, guild_id, arg).await;
//!     }
//! }
//! ```
//!
//! [`from_guild_and_str`]: Conversion::from_guild_and_str
//! [`from_guild_id_and_str`]: Conversion::from_guild_id_and_str

use std::collections::HashMap;

use serenity::model::prelude::*;
use serenity::prelude::Context;
use serenity::{async_trait, utils};

/// A trait to convert a string into serenity's models.
///
/// It provides two methods to convert a string into a guild-specific model.
/// The first method, [`from_guild_and_str`], is available only if cache is enabled.
/// The second method, [`from_guild_id_and_str`], is always available.
///
/// The second method tries to use the cache if it is enabled. Otherwise, it
/// gets data over the REST API.
///
/// ## Conversion Strategy
///
/// Conversion follows this strategy:
/// - Converting argument into a ID and then fetching model using the ID.
/// - Converting argument into a mention and then fetching model using the
///     extracted ID.
/// - Treating argument as model's name.
///
/// **Note:** For [`Member`], nickname and user tag are considered along
/// with the user name.
///
/// ## Limitation
///
/// An argument is only treated as an ID or mention when trying to
/// convert to [`Member`] if the `cache` feature and the `GUILDS`
/// and `GUILD_PRESENCES` intents are not enabled. It is not treated as
/// user name, nickname or tag.
///
/// ## Implementation
///
/// To implement this trait for a custom type, you have to implement both
/// [`from_guild_and_str`] and [`from_guild_id_and_str`] methods.
/// The strategy you use may depend on your model.
///
/// [`from_guild_and_str`]: Conversion::from_guild_and_str
/// [`from_guild_id_and_str`]: Conversion::from_guild_id_and_str
#[async_trait]
pub trait Conversion {
    /// The type of the model to convert to.
    type Item;

    /// Converts `arg` into the specified type, if possible.
    #[cfg(feature = "cache")]
    async fn from_guild_and_str(guild: &Guild, arg: &str) -> Option<Self::Item>
    where
        Self: Sized;

    async fn from_guild_id_and_str(
        ctx: &Context,
        guild_id: GuildId,
        arg: &str,
    ) -> Option<Self::Item>
    where
        Self: Sized;
}

#[async_trait]
impl Conversion for Role {
    type Item = Self;

    /// Converts `arg` into a [`Role`] object.
    #[cfg(feature = "cache")]
    async fn from_guild_and_str(guild: &Guild, arg: &str) -> Option<Self>
    where
        Self: Sized,
    {
        let roles = &guild.roles;

        role_from_mapping(arg, roles).await
    }

    async fn from_guild_id_and_str(
        ctx: &Context,
        guild_id: GuildId,
        arg: &str,
    ) -> Option<Self::Item>
    where
        Self: Sized,
    {
        #[cfg(feature = "cache")]
        {
            if let Some(roles) = ctx.cache.guild_roles(guild_id) {
                return role_from_mapping(arg, &roles).await;
            }
        }

        // Get guild's roles using http requests.
        let roles = ctx.http.get_guild_roles(guild_id.0).await.ok()?;
        match arg.parse::<u64>() {
            // `arg` is role ID.
            Ok(id) => roles.iter().find(|r| r.id.0 == id).cloned(),
            Err(_) => match utils::parse_role(arg) {
                // `arg` is role mention.
                Some(id) => roles.iter().find(|r| r.id.0 == id).cloned(),
                // `arg` is role name.
                None => roles.iter().find(|r| r.name == arg).cloned(),
            },
        }
    }
}

#[async_trait]
impl Conversion for Member {
    type Item = Self;

    /// Converts `arg` into a [`Member`] object.
    #[cfg(feature = "cache")]
    async fn from_guild_and_str(guild: &Guild, arg: &str) -> Option<Self>
    where
        Self: Sized,
    {
        let members = &guild.members;

        member_from_mapping(arg, members).await
    }

    async fn from_guild_id_and_str(
        ctx: &Context,
        guild_id: GuildId,
        arg: &str,
    ) -> Option<Self::Item>
    where
        Self: Sized,
    {
        #[cfg(feature = "cache")]
        {
            if let Some(members) = ctx.cache.guild_field(guild_id, |g| g.members.clone()) {
                return member_from_mapping(arg, &members).await;
            }
        }

        let id = match arg.parse::<u64>() {
            // `arg` is a user ID.
            Ok(id) => id,
            Err(_) => match utils::parse_username(arg) {
                Some(id) => id,
                None => return None,
            },
        };

        ctx.http.get_member(guild_id.0, id).await.ok()
    }
}

#[async_trait]
impl Conversion for GuildChannel {
    type Item = Self;

    /// Converts `arg` into a [`GuildChannel`] object.
    #[cfg(feature = "cache")]
    async fn from_guild_and_str(guild: &Guild, arg: &str) -> Option<Self>
    where
        Self: Sized,
    {
        let channels = &guild.channels;

        channel_from_mapping(arg, channels).await
    }

    async fn from_guild_id_and_str(
        ctx: &Context,
        guild_id: GuildId,
        arg: &str,
    ) -> Option<Self::Item>
    where
        Self: Sized,
    {
        #[cfg(feature = "cache")]
        {
            if let Some(channels) = ctx.cache.guild_field(guild_id, |g| g.channels.clone()) {
                return channel_from_mapping(arg, &channels).await;
            }
        }

        // Get guild's roles using http requests.
        let channels = ctx.http.get_channels(guild_id.0).await.ok()?;
        match arg.parse::<u64>() {
            // `arg` is channel ID.
            Ok(id) => channels.iter().find(|c| c.id.0 == id).cloned(),
            Err(_) => match utils::parse_channel(arg) {
                // `arg` is channel mention.
                Some(id) => channels.iter().find(|c| c.id.0 == id).cloned(),
                // `arg` is channel name.
                None => channels.iter().find(|c| c.name == arg).cloned(),
            },
        }
    }
}

async fn role_from_mapping(arg: &str, roles: &HashMap<RoleId, Role>) -> Option<Role> {
    match arg.parse::<u64>() {
        // `arg` is a role ID.
        Ok(id) => roles.get(&RoleId(id)).cloned(),
        Err(_) => match utils::parse_role(arg) {
            // `arg` is a role mention.
            Some(id) => roles.get(&RoleId(id)).cloned(),
            // `arg` is a role name.
            None => roles.values().find(|r| r.name == arg).cloned(),
        },
    }
}

async fn member_from_mapping(arg: &str, members: &HashMap<UserId, Member>) -> Option<Member> {
    match arg.parse::<u64>() {
        // `arg` is a user ID.
        Ok(id) => members.get(&UserId(id)).cloned(),
        Err(_) => match utils::parse_username(arg) {
            // `arg` is a member mention.
            Some(id) => members.get(&UserId(id)).cloned(),
            // `arg` is a member's name or nickname.
            None => members
                .values()
                .find(|m| {
                    m.display_name().as_str() == arg || m.user.name == arg || m.user.tag() == arg
                })
                .cloned(),
        },
    }
}

async fn channel_from_mapping(
    arg: &str,
    channels: &HashMap<ChannelId, Channel>,
) -> Option<GuildChannel> {
    let get_guild_channel = |channel| {
        if let &Channel::Guild(ref c) = channel {
            Some(c)
        } else {
            None
        }
    };

    match arg.parse::<u64>() {
        // `arg` is a channel ID.
        Ok(id) => channels.get(&ChannelId(id)).and_then(get_guild_channel),
        Err(_) => match utils::parse_channel(arg) {
            // `arg` is a channel mention.
            Some(id) => channels.get(&ChannelId(id)).and_then(get_guild_channel),
            // `arg` is a channel name.
            None => channels.values().find_map(|c| get_guild_channel(c).filter(|c| c.name == arg)),
        },
    }
    .cloned()
}