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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use reqwest::Client;
use serde_json::to_string;
use crate::types::{AddChannelRecipientSchema, ModifyChannelPositionsSchema};
use crate::{
errors::{ChorusError, ChorusResult},
instance::ChorusUser,
ratelimiter::ChorusRequest,
types::{
Channel, ChannelModifySchema, GetChannelMessagesSchema, LimitType, Message, Snowflake,
},
};
impl Channel {
/// Retrieves a channel from the server.
///
/// # Reference
/// See <https://docs.discord.food/resources/channel#get-channel>
pub async fn get(user: &mut ChorusUser, channel_id: Snowflake) -> ChorusResult<Channel> {
let chorus_request = ChorusRequest {
request: Client::new().get(format!(
"{}/channels/{}",
user.belongs_to.read().unwrap().urls.api.clone(),
channel_id
)),
limit_type: LimitType::Channel(channel_id),
}
.with_headers_for(user);
chorus_request.send_and_deserialize_response::<Channel>(user).await
}
/// Deletes self.
///
/// Requires the [`MANAGE_CHANNELS`](crate::types::PermissionFlags::MANAGE_CHANNELS) permission in a guild, or
/// the [`MANAGE_THREADS`](crate::types::PermissionFlags::MANAGE_THREADS) permission if the channel is a thread.
///
/// # Reference
/// See <https://docs.discord.food/resources/channel#delete-channel>
pub async fn delete(
self,
audit_log_reason: Option<String>,
user: &mut ChorusUser,
) -> ChorusResult<()> {
let url = format!(
"{}/channels/{}",
user.belongs_to.read().unwrap().urls.api,
self.id,
);
let request = ChorusRequest {
request: Client::new().delete(url),
limit_type: LimitType::Channel(self.id),
}
.with_maybe_audit_log_reason(audit_log_reason)
.with_headers_for(user);
request.send_and_handle_as_result(user).await
}
/// Modifies a channel with the provided data.
/// Returns the new Channel.
///
/// Requires the [`MANAGE_CHANNELS`](crate::types::PermissionFlags::MANAGE_CHANNELS) permission in a guild.
///
/// If modifying permission overwrites, the [`MANAGE_ROLES`](crate::types::PermissionFlags::MANAGE_ROLES) permission is required.
/// Only permissions you have in the guild or parent channel (if applicable) can be allowed/denied
/// (unless you have a [`MANAGE_ROLES`](crate::types::PermissionFlags::MANAGE_ROLES) overwrite in the channel).
///
/// If modifying a thread and setting `archived` to `false`, when `locked` is also `false`, only the [`SEND_MESSAGES`](crate::types::PermissionFlags::SEND_MESSAGES) permission is required.
/// Otherwise, requires the [`MANAGE_THREADS`](crate::types::PermissionFlags::MANAGE_THREADS) permission. Requires the thread to have `archived` set to `false` or be set to `false` in the request.
///
/// # Reference
/// See <https://docs.discord.food/resources/channel#modify-channel>
pub async fn modify(
&self,
modify_data: ChannelModifySchema,
audit_log_reason: Option<String>,
user: &mut ChorusUser,
) -> ChorusResult<Channel> {
let channel_id = self.id;
let url = format!(
"{}/channels/{}",
user.belongs_to.read().unwrap().urls.api,
channel_id
);
let request = ChorusRequest {
request: Client::new().patch(url).json(&modify_data),
limit_type: LimitType::Channel(channel_id),
}
.with_maybe_audit_log_reason(audit_log_reason)
.with_headers_for(user);
request.send_and_deserialize_response::<Channel>(user).await
}
/// Fetches recent messages from a channel.
///
/// If operating on a guild channel, this endpoint requires the [`VIEW_CHANNEL`](crate::types::PermissionFlags::VIEW_CHANNEL) permission.
///
/// If the user is missing the [`READ_MESSAGE_HISTORY`](crate::types::PermissionFlags::READ_MESSAGE_HISTORY) permission,
/// this method returns an empty list.
///
/// # Reference
/// See <https://docs.discord.food/resources/message#get-messages>
pub async fn messages(
range: GetChannelMessagesSchema,
channel_id: Snowflake,
user: &mut ChorusUser,
) -> Result<Vec<Message>, ChorusError> {
let url = format!(
"{}/channels/{}/messages",
user.belongs_to.read().unwrap().urls.api,
channel_id
);
let mut chorus_request = ChorusRequest {
request: Client::new().get(url),
limit_type: Default::default(),
}
.with_headers_for(user);
chorus_request.request = chorus_request.request.query(&range);
chorus_request
.send_and_deserialize_response::<Vec<Message>>(user)
.await
}
/// Adds a recipient to a group DM.
///
/// # Reference:
/// See <https://docs.discord.food/resources/channel#add-channel-recipient>
pub async fn add_channel_recipient(
&self,
recipient_id: Snowflake,
user: &mut ChorusUser,
add_channel_recipient_schema: Option<AddChannelRecipientSchema>,
) -> ChorusResult<()> {
let mut request = Client::new().put(format!(
"{}/channels/{}/recipients/{}",
user.belongs_to.read().unwrap().urls.api,
self.id,
recipient_id
));
if let Some(schema) = add_channel_recipient_schema {
request = request.json(&schema);
}
ChorusRequest {
request,
limit_type: LimitType::Channel(self.id),
}
.with_headers_for(user)
.send_and_handle_as_result(user)
.await
}
/// Removes a recipient from a group DM.
///
/// # Reference:
/// See <https://docs.discord.food/resources/channel#remove-channel-recipient>
pub async fn remove_channel_recipient(
&self,
recipient_id: Snowflake,
user: &mut ChorusUser,
) -> ChorusResult<()> {
let url = format!(
"{}/channels/{}/recipients/{}",
user.belongs_to.read().unwrap().urls.api,
self.id,
recipient_id
);
let request = ChorusRequest {
request: Client::new().delete(url),
limit_type: LimitType::Channel(self.id),
}
.with_headers_for(user);
request.send_and_handle_as_result(user).await
}
/// Modifies the positions of a set of channel objects for the guild. Requires the `MANAGE_CHANNELS` permission.
/// Only channels to be modified are required.
///
/// # Reference:
/// See <https://docs.discord.food/resources/channel#modify-guild-channel-positions>
pub async fn modify_positions(
schema: Vec<ModifyChannelPositionsSchema>,
guild_id: Snowflake,
user: &mut ChorusUser,
) -> ChorusResult<()> {
let url = format!(
"{}/guilds/{}/channels",
user.belongs_to.read().unwrap().urls.api,
guild_id
);
let request = ChorusRequest {
request: Client::new().patch(url).json(&schema),
limit_type: LimitType::Guild(guild_id),
}
.with_headers_for(user);
request.send_and_handle_as_result(user).await
}
}