Skip to main content

djs_rs/manager/
stage_instance.rs

1//! [`StageInstanceManager`].
2
3use std::{future::Future, sync::Arc};
4
5use djs_cache::{Backend, GuildView};
6use djs_model::{
7    id::{ChannelId, GuildId, StageInstanceId},
8    rest::{CreateStageInstance, ModifyStageInstance},
9    stage_instance::StageInstance,
10};
11use djs_rest::Error;
12
13use super::Fetch;
14use crate::Context;
15
16/// The live stages of one guild, by their stage channel. Returned by
17/// [`Context::stage_instances`].
18///
19/// discord.js counterpart: `StageInstanceManager` (`guild.stageInstances`).
20#[derive(Debug)]
21pub struct StageInstanceManager<'a, B: Backend> {
22    ctx: &'a Context<B>,
23    guild_id: GuildId,
24}
25
26impl<B: Backend> Clone for StageInstanceManager<'_, B> {
27    fn clone(&self) -> Self {
28        *self
29    }
30}
31
32impl<B: Backend> Copy for StageInstanceManager<'_, B> {}
33
34impl<'a, B: Backend> StageInstanceManager<'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 stages belong to.
40    ///
41    /// discord.js counterpart: `StageInstanceManager#guild`.
42    #[must_use]
43    pub const fn guild_id(self) -> GuildId {
44        self.guild_id
45    }
46
47    /// The cached stage instances of the guild (by their own ID), if the client has a cache.
48    ///
49    /// discord.js counterpart: `StageInstanceManager#cache`.
50    #[must_use]
51    pub fn cache(self) -> Option<GuildView<'a, StageInstanceId, StageInstance, B>> {
52        self.ctx
53            .cache()
54            .map(|cache| cache.stage_instances(self.guild_id))
55    }
56
57    /// The stage instance of a stage channel: the cached one, or [Get Stage
58    /// Instance](djs_rest::Rest::get_stage_instance), whose result is cached.
59    ///
60    /// discord.js counterpart: `StageInstanceManager#fetch(channel)`.
61    ///
62    /// # Errors
63    ///
64    /// Returns the REST error if Discord was asked and the request failed (404 when the
65    /// stage is not live).
66    pub fn fetch(
67        self,
68        channel_id: ChannelId,
69    ) -> Fetch<
70        Arc<StageInstance>,
71        impl Future<Output = Result<Arc<StageInstance>, Error>> + Send + 'a,
72    > {
73        let cached = self.ctx.ops().stage_instance_of(self.guild_id, channel_id);
74        let Self { ctx, guild_id } = self;
75        Fetch::new(cached, async move {
76            let stage = ctx.rest().get_stage_instance(channel_id).await?;
77            Ok(ctx.store_stage_instance(guild_id, &stage))
78        })
79    }
80
81    /// Opens a stage in a stage channel ([Create Stage
82    /// Instance](djs_rest::Rest::create_stage_instance)); it is cached.
83    ///
84    /// discord.js counterpart: `StageInstanceManager#create`.
85    ///
86    /// # Errors
87    ///
88    /// Returns the REST error if the request failed (the bot must be a stage moderator).
89    pub async fn create(
90        self,
91        body: &CreateStageInstance,
92        reason: Option<&str>,
93    ) -> Result<Arc<StageInstance>, Error> {
94        let stage = self.ctx.rest().create_stage_instance(body, reason).await?;
95        Ok(self.ctx.store_stage_instance(self.guild_id, &stage))
96    }
97
98    /// Changes a live stage's topic or privacy ([Modify Stage
99    /// Instance](djs_rest::Rest::modify_stage_instance)); the result is cached.
100    ///
101    /// discord.js counterpart: `StageInstanceManager#edit`.
102    ///
103    /// # Errors
104    ///
105    /// Returns the REST error if the request failed.
106    pub async fn edit(
107        self,
108        channel_id: ChannelId,
109        body: &ModifyStageInstance,
110        reason: Option<&str>,
111    ) -> Result<Arc<StageInstance>, Error> {
112        let stage = self
113            .ctx
114            .rest()
115            .modify_stage_instance(channel_id, body, reason)
116            .await?;
117        Ok(self.ctx.store_stage_instance(self.guild_id, &stage))
118    }
119
120    /// Ends a stage ([Delete Stage Instance](djs_rest::Rest::delete_stage_instance)). The
121    /// cache is updated by the `STAGE_INSTANCE_DELETE` event that follows (D-236).
122    ///
123    /// discord.js counterpart: `StageInstanceManager#delete`.
124    ///
125    /// # Errors
126    ///
127    /// Returns the REST error if the request failed.
128    pub async fn delete(self, channel_id: ChannelId, reason: Option<&str>) -> Result<(), Error> {
129        self.ctx
130            .rest()
131            .delete_stage_instance(channel_id, reason)
132            .await
133    }
134}