djs_rs/manager/role.rs
1//! [`RoleManager`].
2
3use std::{future::Future, sync::Arc};
4
5use djs_cache::{Backend, GuildView};
6use djs_collection::Collection;
7use djs_model::{
8 guild::Role,
9 id::{GuildId, RoleId},
10 rest::{CreateGuildRole, ModifyGuildRole, ModifyGuildRolePosition},
11};
12use djs_rest::Error;
13
14use super::Fetch;
15use crate::Context;
16
17/// The roles of one guild. Returned by [`Context::roles`].
18///
19/// discord.js counterpart: `RoleManager` (`guild.roles`).
20#[derive(Debug)]
21pub struct RoleManager<'a, B: Backend> {
22 ctx: &'a Context<B>,
23 guild_id: GuildId,
24}
25
26impl<B: Backend> Clone for RoleManager<'_, B> {
27 fn clone(&self) -> Self {
28 *self
29 }
30}
31
32impl<B: Backend> Copy for RoleManager<'_, B> {}
33
34impl<'a, B: Backend> RoleManager<'a, B> {
35 pub(crate) const fn new(ctx: &'a Context<B>, guild_id: GuildId) -> Self {
36 Self { ctx, guild_id }
37 }
38
39 /// The guild these roles belong to.
40 ///
41 /// discord.js counterpart: `RoleManager#guild`.
42 #[must_use]
43 pub const fn guild_id(self) -> GuildId {
44 self.guild_id
45 }
46
47 /// The cached roles of the guild, if the client has a cache.
48 ///
49 /// discord.js counterpart: `RoleManager#cache`.
50 #[must_use]
51 pub fn cache(self) -> Option<GuildView<'a, RoleId, Role, B>> {
52 self.ctx.cache().map(|cache| cache.roles(self.guild_id))
53 }
54
55 /// A role: the cached one, or [Get Guild Role](djs_rest::Rest::get_guild_role), whose
56 /// result is cached.
57 ///
58 /// discord.js counterpart: `RoleManager#fetch(id)`.
59 ///
60 /// # Errors
61 ///
62 /// Returns the REST error if Discord was asked and the request failed.
63 pub fn fetch(
64 self,
65 role_id: RoleId,
66 ) -> Fetch<Arc<Role>, impl Future<Output = Result<Arc<Role>, Error>> + Send + 'a> {
67 let cached = self.ctx.ops().role(self.guild_id, role_id);
68 let Self { ctx, guild_id } = self;
69 Fetch::new(cached, async move {
70 let role = ctx.rest().get_guild_role(guild_id, role_id).await?;
71 Ok(ctx.store_role(guild_id, &role))
72 })
73 }
74
75 /// Every role of the guild, in Discord's order ([Get Guild
76 /// Roles](djs_rest::Rest::get_guild_roles)); the roles are cached.
77 ///
78 /// discord.js counterpart: `RoleManager#fetch()`.
79 ///
80 /// # Errors
81 ///
82 /// Returns the REST error if the request failed.
83 pub async fn list(self) -> Result<Collection<RoleId, Arc<Role>>, Error> {
84 let roles = self.ctx.rest().get_guild_roles(self.guild_id).await?;
85 Ok(roles
86 .iter()
87 .map(|role| (role.id, self.ctx.store_role(self.guild_id, role)))
88 .collect())
89 }
90 /// Creates a role ([Create Guild Role](djs_rest::Rest::create_guild_role)); it is cached.
91 ///
92 /// discord.js counterpart: `RoleManager#create`.
93 ///
94 /// # Errors
95 ///
96 /// Returns the REST error if the request failed (it needs `MANAGE_ROLES`).
97 pub async fn create(
98 self,
99 body: &CreateGuildRole,
100 reason: Option<&str>,
101 ) -> Result<Arc<Role>, Error> {
102 let role = self
103 .ctx
104 .rest()
105 .create_guild_role(self.guild_id, body, reason)
106 .await?;
107 Ok(self.ctx.store_role(self.guild_id, &role))
108 }
109
110 /// Edits a role ([Modify Guild Role](djs_rest::Rest::modify_guild_role)); the result is
111 /// cached.
112 ///
113 /// discord.js counterpart: `RoleManager#edit` / `Role#edit`.
114 ///
115 /// # Errors
116 ///
117 /// Returns the REST error if the request failed (the role must be below the bot's highest
118 /// role).
119 pub async fn edit(
120 self,
121 role_id: RoleId,
122 body: &ModifyGuildRole,
123 reason: Option<&str>,
124 ) -> Result<Arc<Role>, Error> {
125 let role = self
126 .ctx
127 .rest()
128 .modify_guild_role(self.guild_id, role_id, body, reason)
129 .await?;
130 Ok(self.ctx.store_role(self.guild_id, &role))
131 }
132
133 /// Deletes a role ([Delete Guild Role](djs_rest::Rest::delete_guild_role)). The cache is
134 /// updated by the `GUILD_ROLE_DELETE` event that follows (D-236).
135 ///
136 /// discord.js counterpart: `RoleManager#delete` / `Role#delete`.
137 ///
138 /// # Errors
139 ///
140 /// Returns the REST error if the request failed.
141 pub async fn delete(self, role_id: RoleId, reason: Option<&str>) -> Result<(), Error> {
142 self.ctx
143 .rest()
144 .delete_guild_role(self.guild_id, role_id, reason)
145 .await
146 }
147
148 /// Moves roles ([Modify Guild Role
149 /// Positions](djs_rest::Rest::modify_guild_role_positions)), returning every role of the
150 /// guild in Discord's order; the roles are cached.
151 ///
152 /// discord.js counterpart: `RoleManager#setPositions`.
153 ///
154 /// # Errors
155 ///
156 /// Returns the REST error if the request failed.
157 pub async fn set_positions(
158 self,
159 positions: &[ModifyGuildRolePosition],
160 reason: Option<&str>,
161 ) -> Result<Collection<RoleId, Arc<Role>>, Error> {
162 let roles = self
163 .ctx
164 .rest()
165 .modify_guild_role_positions(self.guild_id, positions, reason)
166 .await?;
167 Ok(roles
168 .iter()
169 .map(|role| (role.id, self.ctx.store_role(self.guild_id, role)))
170 .collect())
171 }
172}