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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
use super::{Member, PermissionOverwrite, Permissions};
use crate::{HttpClient, Message, User};
/// Represents a Discord channel (text, voice, DM, etc.)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum ChannelType {
GuildText = 0,
DM = 1,
GuildVoice = 2,
GroupDM = 3,
GuildCategory = 4,
GuildAnnouncement = 5,
AnnouncementThread = 10,
PublicThread = 11,
PrivateThread = 12,
GuildStageVoice = 13,
GuildDirectory = 14,
GuildForum = 15,
GuildMedia = 16,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Channel {
/// Unique ID of the channel
pub id: String,
/// Type of the channel (text, voice, DM, etc.)
#[serde(rename = "type")]
pub kind: ChannelType,
/// Id of the guild (if applicable)
pub guild_id: Option<String>,
/// Position of the channel in the guild (if applicable)
pub position: Option<i32>,
/// Explicit permission overwrites for roles/users in this channel
#[serde(default)]
pub permission_overwrites: Vec<PermissionOverwrite>,
/// Name of the channel (if applicable)
pub name: Option<String>,
/// Topic of the channel (if applicable)
pub topic: Option<String>,
/// Whether the channel is NSFW (if applicable)
#[serde(default)]
pub nsfw: bool,
/// Id of the last message sent in the channel (if applicable)
pub last_message_id: Option<String>,
/// Bitrate (for voice channels)
pub bitrate: Option<u64>,
/// User limit (for voice channels)
pub user_limit: Option<u64>,
/// Rate limit per user (for text channels)
pub rate_limit_per_user: Option<u64>,
/// recipients (for DM channels)
pub recipients: Option<Vec<User>>,
/// Icon hash (for group DM channels)
pub icon: Option<String>,
/// Owner ID (for group DM channels)
pub owner_id: Option<String>,
/// Application ID (for group DM channels)
pub application_id: Option<String>,
/// Whether the channel is managed
#[serde(default)]
pub managed: bool,
/// Channel's parent category ID (if applicable)
pub parent_id: Option<String>,
/// The channel's last pinned message ID (if applicable)
pub last_pin_timestamp: Option<String>,
/// The channel's rtc region (for voice channels)
pub rtc_region: Option<String>,
/// The channel's video quality mode (for voice channels)
pub video_quality_mode: Option<u8>,
/// The channel's message count (for threads)
pub message_count: Option<u64>,
/// The channel's member count (for threads)
pub member_count: Option<u64>,
/// Thread metdata
pub thread_metadata: Option<ThreadMetadata>,
/// Thread member object (for threads the current user has joined)
pub member: Option<ThreadMember>,
/// Default auto archive duration for threads in this channel (if applicable)
pub default_auto_archive_duration: Option<u64>,
/// Permissions (for threads)
pub permissions: Option<Permissions>,
/// Flags
pub flags: Option<u64>,
/// Total number of messages in the thread, even when messages are deleted (if applicable)
pub total_messages: Option<u64>,
/// Available tags in a guild forum channel
pub available_tags: Option<Vec<ForumTag>>,
/// Applied tags IDs in a thread in a guild forum channel
pub applied_tags: Option<Vec<String>>,
/// Default sort order type for a guild forum channel
pub default_sort_order: Option<u8>,
/// Default forum layout view for a guild forum channel
pub default_forum_layout: Option<u8>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThreadMember {
/// The ID of the thread
#[serde(rename = "id")]
pub thread_id: String,
/// The ID of the user
pub user_id: String,
/// The timestamp when the user joined the thread
pub join_timestamp: String,
/// The flags for the user in the thread
pub flags: u64,
/// Whether the user has muted the thread
#[serde(default)]
pub muted: bool,
/// The member object for the user
#[serde(default)]
pub member: Option<Member>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChannelMention {
/// Unique ID of the channel
pub id: String,
/// Id of the guild the channel belongs to
pub guild_id: String,
/// Name of the channel
pub name: String,
/// Type of the channel (text, voice, DM, etc.)
#[serde(rename = "type")]
pub kind: ChannelType,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForumTag {
/// The id of the tag
pub id: Option<String>,
/// The name of the tag
pub name: String,
/// Moderated (whether users can add this tag to their threads)
#[serde(default)]
pub moderated: bool,
/// Custom emoji ID associated with the tag (if any)
pub emoji_id: Option<String>,
/// Emoji name associated with the tag (if any, used if emoji_id is null)
pub emoji_name: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThreadMetadata {
/// Whether the thread is archived
pub archived: bool,
/// Timestamp when the thread was archived
pub archive_timestamp: String,
/// Whether the thread is locked
pub locked: bool,
/// Whether non-moderators can unarchive the thread
pub invitable: Option<bool>,
/// Create Timestamp of the thread (for threads created before 2022-01-09)
pub create_timestamp: Option<String>,
}
impl Channel {
/// Helper method to check if the channel is a DM or Group DM
pub fn is_dm(&self) -> bool {
matches!(self.kind, ChannelType::DM | ChannelType::GroupDM)
}
/// Helper method to get the mention string for the channel
pub fn mention(&self) -> String {
if self.is_dm() {
format!("<@{}>", self.id)
} else {
format!("<#{}>", self.id)
}
}
/// Get the guild for this channel (if applicable)
///
/// # Example
/// ```ignore
/// use diself::{HttpClient, model::channel::Channel};
///
/// async fn example(http: &HttpClient, channel: &Channel) {
/// if let Some(guild) = channel.guild(http).await {
/// println!("Channel is in guild: {}", guild.name);
/// } else {
/// println!("Channel is not in a guild");
/// }
/// }
/// ```
pub async fn guild(&self, http: &HttpClient) -> Option<crate::model::guild::Guild> {
if let Some(guild_id) = &self.guild_id {
let url = crate::http::api_url(&format!("/guilds/{}", guild_id));
if let Ok(response) = http.get(&url).await {
match serde_json::from_value(response) {
Ok(guild) => return Some(guild),
Err(e) => {
eprintln!("Failed to deserialize guild: {}", e);
return None;
}
}
}
None
} else {
None
}
}
/// Sends a message to this channel
pub async fn send(
&self,
http: &HttpClient,
content: impl Into<String>,
) -> Result<Message, crate::error::Error> {
// Sending a message always goes through the channel message endpoint,
// including DM channels.
let url = crate::http::api_url(&format!("/channels/{}/messages", self.id));
let body = serde_json::json!({
"content": content.into()
});
let response = http.post(&url, body).await?;
let message: Message = serde_json::from_value(response)?;
Ok(message)
}
/// Fetches messages from this channel. (`GET /channels/{channel_id}/messages`) SEE: <https://docs.discord.food/resources/message#get-messages>
/// # Params
/// - around?: Snowflake - Get messages around this message ID
/// - before?: Snowflake - Get messages before this message ID
/// - after?: Snowflake - Get messages after this message ID
/// - limit?: number - Max number of messages to return (1-100, default 50)
pub async fn messages(
&self,
http: &HttpClient,
around: Option<String>,
before: Option<String>,
after: Option<String>,
limit: Option<u8>,
) -> Result<Vec<Message>, crate::error::Error> {
let mut url = crate::http::api_url(&format!("/channels/{}/messages", self.id));
let mut query_params = vec![];
if let Some(around) = around {
query_params.push(("around", around));
}
if let Some(before) = before {
query_params.push(("before", before));
}
if let Some(after) = after {
query_params.push(("after", after));
}
if let Some(limit) = limit {
query_params.push(("limit", limit.to_string()));
}
if !query_params.is_empty() {
url.push('?');
url.push_str(
&query_params
.iter()
.map(|(k, v)| format!("{}={}", k, v))
.collect::<Vec<_>>()
.join("&"),
);
}
let response = http.get(&url).await?;
let messages: Vec<Message> = serde_json::from_value(response)?;
Ok(messages)
}
/// Fetches a single message by ID from this channel. (`GET /channels/{channel_id}/messages/{message_id}`) SEE: <https://docs.discord.food/resources/message#get-message>
pub async fn get_message(
&self,
http: &HttpClient,
message_id: impl AsRef<str>,
) -> Result<Message, crate::error::Error> {
let url = crate::http::api_url(&format!(
"/channels/{}/messages/{}",
self.id,
message_id.as_ref()
));
let response = http.get(&url).await?;
let message: Message = serde_json::from_value(response)?;
Ok(message)
}
}