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
use std::default::Default;
use internal::prelude::*;
use model::{permissions, Permissions, Role};

/// A builer to create or edit a [`Role`] for use via a number of model methods.
///
/// These are:
///
/// - [`PartialGuild::create_role`]
/// - [`Guild::create_role`]
/// - [`Guild::edit_role`]
/// - [`GuildId::create_role`]
/// - [`GuildId::edit_role`]
/// - [`Role::edit`]
///
/// Defaults are provided for each parameter on role creation.
///
/// # Examples
///
/// Create a hoisted, mentionable role named `"a test role"`:
///
/// ```rust,no_run
/// # use serenity::model::{ChannelId, GuildId};
/// # let (channel_id, guild_id) = (ChannelId(1), GuildId(2));
/// #
/// // assuming a `channel_id` and `guild_id` has been bound
///
/// let role = guild_id.create_role(|r| r
///     .hoist(true)
///     .mentionable(true)
///     .name("a test role"));
/// ```
///
/// [`PartialGuild::create_role`]: ../model/struct.PartialGuild.html#method.create_role
/// [`Guild::create_role`]: ../model/struct.Guild.html#method.create_role
/// [`Guild::edit_role`]: ../model/struct.Guild.html#method.edit_role
/// [`GuildId::create_role`]: ../model/struct.GuildId.html#method.create_role
/// [`GuildId::edit_role`]: ../model/struct.GuildId.html#method.edit_role
/// [`Role`]: ../model/struct.Role.html
/// [`Role::edit`]: ../model/struct.Role.html#method.edit
#[derive(Clone, Debug)]
pub struct EditRole(pub JsonMap);

impl EditRole {
    /// Creates a new builder with the values of the given [`Role`].
    ///
    /// [`Role`]: ../model/struct.Role.html
    pub fn new(role: &Role) -> Self {
        let mut map = Map::new();

        #[cfg(feature = "utils")]
        {
            map.insert(
                "color".to_owned(),
                Value::Number(Number::from(role.colour.0)),
            );
        }

        #[cfg(not(feature = "utils"))]
        {
            map.insert("color".to_owned(), Value::Number(Number::from(role.colour)));
        }

        map.insert("hoist".to_owned(), Value::Bool(role.hoist));
        map.insert("managed".to_owned(), Value::Bool(role.managed));
        map.insert("mentionable".to_owned(), Value::Bool(role.mentionable));
        map.insert("name".to_owned(), Value::String(role.name.clone()));
        map.insert(
            "permissions".to_owned(),
            Value::Number(Number::from(role.permissions.bits())),
        );
        map.insert(
            "position".to_owned(),
            Value::Number(Number::from(role.position)),
        );

        EditRole(map)
    }

    /// Sets the colour of the role.
    pub fn colour(mut self, colour: u64) -> Self {
        self.0
            .insert("color".to_owned(), Value::Number(Number::from(colour)));

        self
    }

    /// Whether or not to hoist the role above lower-positioned role in the user
    /// list.
    pub fn hoist(mut self, hoist: bool) -> Self {
        self.0.insert("hoist".to_owned(), Value::Bool(hoist));

        self
    }

    /// Whether or not to make the role mentionable, notifying its users.
    pub fn mentionable(mut self, mentionable: bool) -> Self {
        self.0
            .insert("mentionable".to_owned(), Value::Bool(mentionable));

        self
    }

    /// The name of the role to set.
    pub fn name(mut self, name: &str) -> Self {
        self.0
            .insert("name".to_owned(), Value::String(name.to_owned()));

        self
    }

    /// The set of permissions to assign the role.
    pub fn permissions(mut self, permissions: Permissions) -> Self {
        self.0.insert(
            "permissions".to_owned(),
            Value::Number(Number::from(permissions.bits())),
        );

        self
    }

    /// The position to assign the role in the role list. This correlates to the
    /// role's position in the user list.
    pub fn position(mut self, position: u8) -> Self {
        self.0
            .insert("position".to_owned(), Value::Number(Number::from(position)));

        self
    }
}

impl Default for EditRole {
    /// Creates a builder with default parameters.
    ///
    /// The defaults are:
    ///
    /// - **color**: 10070709
    /// - **hoist**: false
    /// - **mentionable**: false
    /// - **name**: new role
    /// - **permissions**: the [general permissions set]
    /// - **position**: 1
    ///
    /// [general permissions set]: ../model/permissions/constant.PRESET_GENERAL.html
    fn default() -> EditRole {
        let mut map = Map::new();
        let permissions = Number::from(permissions::PRESET_GENERAL.bits());

        map.insert("color".to_owned(), Value::Number(Number::from(10_070_709)));
        map.insert("hoist".to_owned(), Value::Bool(false));
        map.insert("mentionable".to_owned(), Value::Bool(false));
        map.insert("name".to_owned(), Value::String("new role".to_owned()));
        map.insert("permissions".to_owned(), Value::Number(permissions));
        map.insert("position".to_owned(), Value::Number(Number::from(1)));

        EditRole(map)
    }
}