djs_rs/manager/guild_channel.rs
1//! [`GuildChannelManager`].
2
3use std::sync::Arc;
4
5use djs_cache::Backend;
6use djs_collection::Collection;
7use djs_model::{
8 channel::Channel,
9 id::{ChannelId, GuildId},
10 rest::{CreateGuildChannel, ModifyGuildChannelPosition},
11};
12use djs_rest::Error;
13
14use super::FetchedThreads;
15use crate::Context;
16
17/// The channels of one guild, as a whole (D-234). Returned by [`Context::guild_channels`].
18///
19/// Operations on one channel by its ID (fetch, edit, delete, permission overwrites) are on
20/// [`Context::channels`]. The cached channels of a guild are listed by
21/// [`CacheView::in_guild`](djs_cache::CacheView::in_guild) on `ctx.channels().cache()`.
22///
23/// discord.js counterpart: `GuildChannelManager` (`guild.channels`).
24#[derive(Debug)]
25pub struct GuildChannelManager<'a, B: Backend> {
26 ctx: &'a Context<B>,
27 guild_id: GuildId,
28}
29
30impl<B: Backend> Clone for GuildChannelManager<'_, B> {
31 fn clone(&self) -> Self {
32 *self
33 }
34}
35
36impl<B: Backend> Copy for GuildChannelManager<'_, B> {}
37
38impl<'a, B: Backend> GuildChannelManager<'a, B> {
39 pub(crate) const fn new(ctx: &'a Context<B>, guild_id: GuildId) -> Self {
40 Self { ctx, guild_id }
41 }
42
43 /// The guild these channels belong to.
44 ///
45 /// discord.js counterpart: `GuildChannelManager#guild`.
46 #[must_use]
47 pub const fn guild_id(self) -> GuildId {
48 self.guild_id
49 }
50
51 /// Every channel of the guild except threads, in Discord's order ([Get Guild
52 /// Channels](djs_rest::Rest::get_guild_channels)); the channels are cached.
53 ///
54 /// discord.js counterpart: `GuildChannelManager#fetch()`.
55 ///
56 /// # Errors
57 ///
58 /// Returns the REST error if the request failed.
59 pub async fn list(self) -> Result<Collection<ChannelId, Arc<Channel>>, Error> {
60 let channels = self.ctx.rest().get_guild_channels(self.guild_id).await?;
61 Ok(channels
62 .iter()
63 .map(|channel| (channel.id(), self.ctx.store_channel(channel)))
64 .collect())
65 }
66
67 /// Creates a channel ([Create Guild Channel](djs_rest::Rest::create_guild_channel)); it is
68 /// cached.
69 ///
70 /// discord.js counterpart: `GuildChannelManager#create`.
71 ///
72 /// # Errors
73 ///
74 /// Returns the REST error if the request failed (it needs `MANAGE_CHANNELS`).
75 pub async fn create(
76 self,
77 body: &CreateGuildChannel,
78 reason: Option<&str>,
79 ) -> Result<Arc<Channel>, Error> {
80 let channel = self
81 .ctx
82 .rest()
83 .create_guild_channel(self.guild_id, body, reason)
84 .await?;
85 Ok(self.ctx.store_channel(&channel))
86 }
87
88 /// Moves channels ([Modify Guild Channel
89 /// Positions](djs_rest::Rest::modify_guild_channel_positions)); the cache follows the
90 /// `CHANNEL_UPDATE` events Discord sends.
91 ///
92 /// discord.js counterpart: `GuildChannelManager#setPositions`.
93 ///
94 /// # Errors
95 ///
96 /// Returns the REST error if the request failed (it needs `MANAGE_CHANNELS`).
97 pub async fn set_positions(
98 self,
99 positions: &[ModifyGuildChannelPosition],
100 reason: Option<&str>,
101 ) -> Result<(), Error> {
102 self.ctx
103 .rest()
104 .modify_guild_channel_positions(self.guild_id, positions, reason)
105 .await
106 }
107 /// The guild's active threads, with the bot's memberships ([List Active Guild
108 /// Threads](djs_rest::Rest::list_active_guild_threads)); the threads are cached.
109 ///
110 /// discord.js counterpart: `GuildChannelManager#fetchActiveThreads`.
111 ///
112 /// # Errors
113 ///
114 /// Returns the REST error if the request failed.
115 pub async fn list_active_threads(self) -> Result<FetchedThreads, Error> {
116 let active = self
117 .ctx
118 .rest()
119 .list_active_guild_threads(self.guild_id)
120 .await?;
121 Ok(self
122 .ctx
123 .fetched_threads(&active.threads, active.members, false))
124 }
125}