spectacles_model/
channel.rs

1//! Structures related to a Channel on Discord.
2use chrono::{DateTime, FixedOffset};
3use serde_repr::{Deserialize_repr, Serialize_repr};
4
5use crate::{Snowflake, User};
6
7/// A guild or DM channel on Discord.
8#[derive(Serialize, Deserialize, Debug, Clone, Default)]
9pub struct Channel {
10    /// The channel ID of this channel.
11    pub id: Snowflake,
12    /// The type of channel.
13    #[serde(rename = "type")]
14    pub kind: Option<ChannelType>,
15    /// The guild ID of this channel.
16    #[serde(default)]
17    pub guild_id: Option<Snowflake>,
18    /// The position of this channel.
19    #[serde(default)]
20    pub position: Option<i32>,
21    /// The explicit permission overwrites for members and roles.
22    #[serde(default)]
23    pub permission_overwrites: Option<Vec<PermissionOverwrites>>,
24    /// The name of the channel.
25    #[serde(default)]
26    pub name: Option<String>,
27    /// The topic of this channel.
28    #[serde(default)]
29    pub topic: Option<String>,
30    /// Whether or not this channel is an NSFW channel.
31    #[serde(default)]
32    pub nsfw: Option<bool>,
33    /// The ID of the last message sent in this channel.
34    #[serde(default)]
35    pub last_message_id: Option<Snowflake>,
36    /// The bitrate of this channel.
37    #[serde(default)]
38    pub bitrate: Option<i32>,
39    /// The user limit, if voice.
40    #[serde(default)]
41    pub user_limit: Option<i32>,
42    /// The cooldown between sending messages in this channel, in seconds.
43    #[serde(default)]
44    pub rate_limit_per_user: Option<i32>,
45    /// The recepients, if DM.
46    #[serde(default)]
47    pub recipients: Option<Vec<User>>,
48    /// The channel's icon hash if any.
49    #[serde(default)]
50    pub icon: Option<String>,
51    /// The ID of the creator, if a DM.
52    #[serde(default)]
53    pub owner_id: Option<Snowflake>,
54    /// The application ID, if the channel was created by a bot.
55    #[serde(default)]
56    pub application_id: Option<Snowflake>,
57    /// The ID of the parent category.
58    #[serde(default)]
59    pub parent_id: Option<Snowflake>,
60    /// When the last message was pinned.
61    #[serde(default)]
62    pub last_pin_timestamp: Option<DateTime<FixedOffset>>
63}
64
65impl ToString for Channel {
66    fn to_string(&self) -> String {
67        format!("<#{}>", self.id.0)
68    }
69}
70
71/// Options for creating a Discord channel.
72#[derive(Serialize, Clone, Debug, Default)]
73pub struct CreateChannelOptions {
74    name: String,
75    #[serde(rename = "type")]
76    kind: Option<ChannelType>,
77    #[serde(skip_serializing_if = "Option::is_none")]
78    position: Option<i32>,
79    #[serde(skip_serializing_if = "Option::is_none")]
80    topic: Option<String>,
81    #[serde(skip_serializing_if = "Option::is_none")]
82    nsfw: Option<bool>,
83    #[serde(skip_serializing_if = "Option::is_none")]
84    rate_limit_per_user: Option<i8>,
85    #[serde(skip_serializing_if = "Option::is_none")]
86    bitrate: Option<i32>,
87    #[serde(skip_serializing_if = "Option::is_none")]
88    user_limit: Option<i32>,
89    #[serde(skip_serializing_if = "Option::is_none")]
90    permission_overwrites: Option<Vec<PermissionOverwrites>>,
91    #[serde(skip_serializing_if = "Option::is_none")]
92    parent_id: Option<Snowflake>,
93}
94
95/// Options for modifying a Discord channel.
96#[derive(Serialize, Clone, Debug, Default)]
97pub struct ModifyChannelOptions {
98    #[serde(skip_serializing_if = "Option::is_none")]
99    name: Option<String>,
100    #[serde(skip_serializing_if = "Option::is_none")]
101    position: Option<i32>,
102    #[serde(skip_serializing_if = "Option::is_none")]
103    topic: Option<String>,
104    #[serde(skip_serializing_if = "Option::is_none")]
105    nsfw: Option<bool>,
106    #[serde(skip_serializing_if = "Option::is_none")]
107    rate_limit_per_user: Option<i8>,
108    #[serde(skip_serializing_if = "Option::is_none")]
109    bitrate: Option<i32>,
110    #[serde(skip_serializing_if = "Option::is_none")]
111    user_limit: Option<i32>,
112    /*#[serde(skip_serializing_if = "Option::is_none")]
113    permission_overwrites: Option<Vec<PermissionOverwrites>>,*/
114    #[serde(skip_serializing_if = "Option::is_none")]
115    parent_id: Option<Snowflake>
116}
117
118impl ModifyChannelOptions {
119    pub fn new() -> Self {
120        ModifyChannelOptions::default()
121    }
122
123    /// Sets a new name for this channel.
124    pub fn name(mut self, new_name: &str) -> Self {
125        self.name = Some(new_name.to_string());
126        self
127    }
128
129    /// Sets a new position for this channel.
130    pub fn set_position(mut self, pos: i32) -> Self {
131        self.position = Some(pos);
132        self
133    }
134
135    /// Sets a new topic for this channel.
136    pub fn topic(mut self, top: &str) -> Self {
137        self.topic = Some(top.to_string());
138        self
139    }
140
141    /// Changes the NSFW flag for this channel.
142    pub fn nsfw(mut self, opt: bool) -> Self {
143        self.nsfw = Some(opt);
144        self
145    }
146
147    /// Modifies this channel's message rate limit per user.
148    pub fn rate_limit_per_user(mut self, secs: i8) -> Self {
149        self.rate_limit_per_user = Some(secs);
150        self
151    }
152
153    /// Modifies this channel's user limit, if a voice channel.
154    pub fn user_limit(mut self, limit: i32) -> Self {
155        self.user_limit = Some(limit);
156        self
157    }
158
159    /*
160    /// Modifies this channel's permission overwrites.
161    pub fn overwrites(mut self, ows: Vec<PermissionOverwrites>) -> Self {
162        self.permission_overwrites = Some(ows);
163        self
164    }
165    */
166
167    /// Modifies this channel's parent category ID.
168    pub fn parent_id(mut self, id: u64) -> Self {
169        self.parent_id = Some(id.into());
170        self
171    }
172}
173/// A channel permission overwrite.
174#[derive(Serialize, Deserialize, Debug, Clone, Default)]
175pub struct PermissionOverwrites {
176    /// The ID of the role or user.
177    pub id: Snowflake,
178    /// What this ID is for.
179    #[serde(rename = "type")]
180    pub kind: String,
181    /// The allowed permission bitfield.
182    pub allow: i32,
183    /// The denied permissions bitfield.
184    pub deny: i32
185}
186
187/// Represents the possible Channel types,
188#[derive(Serialize_repr, Deserialize_repr, Debug, Clone)]
189#[repr(u8)]
190pub enum ChannelType {
191    Text,
192    DM,
193    Voice,
194    GroupDM,
195    Category,
196    News,
197    Store
198}