spectacles_rest/views/
guild.rs

1use futures::future::Future;
2use reqwest::Method;
3
4use spectacles_model::channel::{Channel, CreateChannelOptions};
5use spectacles_model::guild::{AddMemberOptions, CreateRoleOptions, GetAuditLogOptions, Guild, GuildAuditLog, GuildBan, GuildEmbed, GuildIntegration, GuildMember, GuildPrune, ListMembersOptions, ModifyGuildEmbedOptions, ModifyGuildIntegrationOptions, ModifyGuildOptions, ModifyMemberOptions, ModifyRoleOptions, Role};
6use spectacles_model::invite::Invite;
7use spectacles_model::message::{CreateEmojiOptions, Emoji, Webhook};
8use spectacles_model::snowflake::Snowflake;
9use spectacles_model::voice::VoiceRegion;
10
11use crate::{Error, RestClient};
12use crate::Endpoint;
13
14/// A view for interfacing with a Discord guild.
15pub struct GuildView {
16    id: u64,
17    client: RestClient,
18}
19
20impl GuildView {
21    pub(crate) fn new(id: u64, client: RestClient) -> Self {
22        Self {
23            id,
24            client,
25        }
26    }
27    /// Modifies the settings of this guild.
28    pub fn modify(&self, opts: ModifyGuildOptions) -> impl Future<Item=Guild, Error=Error> {
29        self.client.request(
30            Endpoint::new(
31                Method::PATCH,
32                format!("/guilds/{}", self.id),
33            ).json(opts)
34        )
35    }
36
37    /// Deletes this guild from Discord.
38    pub fn delete(&self) -> impl Future<Item=(), Error=Error> {
39        self.client.request(Endpoint::new(
40            Method::DELETE,
41            format!("/guilds/{}", self.id),
42        ))
43    }
44
45    /// Fetches all channels in the current guild.
46    pub fn get_channels(&self) -> impl Future<Item=Vec<Channel>, Error=Error> {
47        self.client.request(Endpoint::new(
48            Method::GET,
49            format!("/guilds/{}/channels", self.id),
50        ))
51    }
52
53    /// Creates a new channel in this guild.
54    pub fn create_channel(&self, opts: CreateChannelOptions) -> impl Future<Item=Channel, Error=Error> {
55        self.client.request(
56            Endpoint::new(
57                Method::POST,
58                format!("/guilds/{}/channels", self.id),
59            ).json(opts)
60        )
61    }
62
63    /// Modifies a set of channel positions in this guild.
64    pub fn modify_channel_positions(&self) {}
65
66    /// Gets a list of webhooks that belong to this guild.
67    pub fn get_webhooks(&self) -> impl Future<Item=Webhook, Error=Error> {
68        self.client.request(Endpoint::new(
69            Method::GET,
70            format!("/guilds/{}/webhooks", self.id),
71        ))
72    }
73
74    /// Gets a guild member of the specified user id.
75    pub fn get_member(&self, id: &Snowflake) -> impl Future<Item=GuildMember, Error=Error> {
76        self.client.request(Endpoint::new(
77            Method::GET,
78            format!("/guilds/{}/members/{}", self.id, id.0),
79        ))
80    }
81
82    /// Lists all guild members in the current guild.
83    pub fn list_members(&self, opts: ListMembersOptions) -> impl Future<Item=Vec<GuildMember>, Error=Error> {
84        self.client.request(
85            Endpoint::new(
86                Method::GET,
87                format!("/guilds/{}/members", self.id),
88            ).query(opts)
89        )
90    }
91
92    /// Adds a member to the guild, using an Oauth2 access token.
93    pub fn add_member(&self, id: &Snowflake, opts: AddMemberOptions) -> impl Future<Item=GuildMember, Error=Error> {
94        self.client.request(
95            Endpoint::new(
96                Method::PUT,
97                format!("/guilds/{}/members/{}", self.id, id.0),
98            ).json(opts)
99        )
100    }
101
102    /// Adds a role to the specified guild member.
103    pub fn add_member_role(&self, member: &Snowflake, role: &Snowflake) -> impl Future<Item=(), Error=Error> {
104        self.client.request_empty(
105            Endpoint::new(
106                Method::PUT,
107                format!("/guilds/{}/members/{}/roles/{}", self.id, member.0, role.0),
108            )
109        )
110    }
111
112    /// Gets a list of emojis in the guild.
113    pub fn get_emojis(&self) -> impl Future<Item=Vec<Emoji>, Error=Error> {
114        self.client.request(Endpoint::new(
115            Method::GET,
116            format!("/guilds/{}/emojis", self.id),
117        ))
118    }
119
120    /// Gets a a single emoji by snowflake in the guild.
121    pub fn get_emoji(&self, emoji: &Snowflake) -> impl Future<Item=Emoji, Error=Error> {
122        self.client.request(Endpoint::new(
123            Method::GET,
124            format!("/guilds/{}/emojis/{}", self.id, emoji.0),
125        ))
126    }
127
128    /// Creates a new emoji in this guild, with the provided options.
129    pub fn create_emoji(&self, opts: CreateEmojiOptions) -> impl Future<Item=Emoji, Error=Error> {
130        self.client.request(
131            Endpoint::new(
132                Method::POST,
133                format!("/guilds/{}/emojis", self.id),
134            ).json(opts)
135        )
136    }
137
138    /// Modifies the provided emoji with the given options.
139    pub fn modify_emoji(&self, id: &Snowflake, opts: ModifyGuildEmbedOptions) -> impl Future<Item=Emoji, Error=Error> {
140        self.client.request(
141            Endpoint::new(
142                Method::PATCH,
143                format!("/guilds/{}/emojis/{}", self.id, id.0),
144            ).json(opts)
145        )
146    }
147
148    /// Deletes the provided emoji from the guild.
149    pub fn delete_emoji(&self, id: &Snowflake) -> impl Future<Item=(), Error=Error> {
150        self.client.request_empty(Endpoint::new(
151            Method::DELETE,
152            format!("/guilds/{}/emojis/{}", self.id, id.0),
153        ))
154    }
155
156    /// Fetches the audit log for this guild.
157    pub fn get_audit_log(&self, opts: GetAuditLogOptions) -> impl Future<Item=GuildAuditLog, Error=Error> {
158        self.client.request(Endpoint::new(
159            Method::GET,
160            format!("/guilds/{}/audit-logs", self.id),
161        ).query(opts))
162    }
163
164    /// Gets a list of bans in the guild.
165    pub fn get_bans(&self) -> impl Future<Item=Vec<GuildBan>, Error=Error> {
166        self.client.request(Endpoint::new(
167            Method::GET,
168            format!("/guilds/{}/bans", self.id),
169        ))
170    }
171
172    /// Gets a a single ban for the provided user in the guild.
173    pub fn get_ban(&self, user: &Snowflake) -> impl Future<Item=GuildBan, Error=Error> {
174        self.client.request(Endpoint::new(
175            Method::GET,
176            format!("/guilds/{}/bans/{}", self.id, user.0),
177        ))
178    }
179
180    /// Removes a ban for the provided user from the guild.
181    pub fn remove_ban(&self, user: &Snowflake) -> impl Future<Item=(), Error=Error> {
182        self.client.request_empty(Endpoint::new(
183            Method::DELETE,
184            format!("/guilds/{}/bans/{}", self.id, user.0),
185        ))
186    }
187
188    /// Gets a collection of roles from the guild.
189    pub fn get_roles(&self) -> impl Future<Item=Vec<Role>, Error=Error> {
190        self.client.request(Endpoint::new(
191            Method::GET,
192            format!("/guilds/{}/roles", self.id),
193        ))
194    }
195
196    /// Creates a role in the guild.
197    pub fn create_role(&self, opts: CreateRoleOptions) -> impl Future<Item=Role, Error=Error> {
198        self.client.request(
199            Endpoint::new(
200                Method::POST,
201                format!("/guilds/{}/roles", self.id),
202            ).json(opts)
203        )
204    }
205
206    /// Modifies the provided role in the guild.
207    pub fn modify_role(&self, role: &Snowflake, opts: ModifyRoleOptions) -> impl Future<Item=Role, Error=Error> {
208        self.client.request(
209            Endpoint::new(
210                Method::PATCH,
211                format!("/guilds/{}/roles/{}", self.id, role.0),
212            ).json(opts)
213        )
214    }
215
216    /// Gets the number of members who would be pruned in a prune operation.
217    pub fn get_prune_count(&self, days: i32) -> impl Future<Item=GuildPrune, Error=Error> {
218        let query = json!({
219            "days": days
220        });
221
222        self.client.request(
223            Endpoint::new(
224                Method::GET,
225                format!("/guilds/{}/prune", self.id),
226            ).query(query)
227        )
228    }
229
230    /// Prunes guild members, according to the provided options.
231    pub fn prune_members(&self, days: i32, compute: bool) -> impl Future<Item=GuildPrune, Error=Error> {
232        let body = json!({
233            "days": days,
234            "compute_prune_count": compute
235        });
236
237        self.client.request(
238            Endpoint::new(
239                Method::POST,
240                format!("/guilds/{}/prune", self.id),
241            ).json(body)
242        )
243    }
244
245    /// Gets a list of voice regions for the guild.
246    pub fn get_voice_regions(&self) -> impl Future<Item=Vec<VoiceRegion>, Error=Error> {
247        self.client.request(Endpoint::new(
248            Method::GET,
249            format!("/guilds/{}/regions", self.id),
250        ))
251    }
252
253    /// Gets a list of guild invites.
254    pub fn get_invites(&self) -> impl Future<Item=Vec<Invite>, Error=Error> {
255        self.client.request(Endpoint::new(
256            Method::GET,
257            format!("/guilds/{}/invites", self.id),
258        ))
259    }
260
261    /// Gets a list of guild integrations.
262    pub fn get_integrations(&self) -> impl Future<Item=Vec<GuildIntegration>, Error=Error> {
263        self.client.request(Endpoint::new(
264            Method::GET,
265            format!("/guilds/{}/integrations", self.id),
266        ))
267    }
268
269    /// Attaches an integration from the current user to the guild.
270    pub fn add_integration(&self, kind: &str, id: &Snowflake) -> impl Future<Item=(), Error=Error> {
271        let body = json!({
272            "type": kind,
273            "id": id
274        });
275
276        self.client.request_empty(
277            Endpoint::new(
278                Method::POST,
279                format!("/guilds/{}/integrations", self.id),
280            ).json(body)
281        )
282    }
283
284
285    /// Modifies the behavior and settings of a guild integration.
286    pub fn modify_integration(&self, id: &Snowflake, opts: ModifyGuildIntegrationOptions) -> impl Future<Item=(), Error=Error> {
287        self.client.request_empty(
288            Endpoint::new(
289                Method::PATCH,
290                format!("/guilds/{}/integrations/{}", self.id, id.0),
291            ).json(opts)
292        )
293    }
294
295    /// Deletes a guild integration by the provided ID.
296    pub fn delete_integration(&self, id: &Snowflake) -> impl Future<Item=(), Error=Error> {
297        self.client.request_empty(Endpoint::new(
298            Method::DELETE,
299            format!("/guilds/{}/integrations/{}", self.id, id.0),
300        ))
301    }
302
303    /// Syncs guild integration by the provided ID.
304    pub fn sync_integration(&self, id: &Snowflake) -> impl Future<Item=(), Error=Error> {
305        self.client.request_empty(Endpoint::new(
306            Method::POST,
307            format!("/guilds/{}/integrations/{}", self.id, id.0),
308        ))
309    }
310
311    /// Gets the embed object of this guild.
312    pub fn get_embed(&self) -> impl Future<Item=GuildEmbed, Error=Error> {
313        self.client.request(Endpoint::new(
314            Method::GET,
315            format!("/guilds/{}/embed", self.id),
316        ))
317    }
318
319    /// Modifies the current guild embed.
320    pub fn modify_embed(&self, opts: ModifyGuildEmbedOptions) -> impl Future<Item=GuildEmbed, Error=Error> {
321        self.client.request(
322            Endpoint::new(
323                Method::PATCH,
324                format!("/guilds/{}/embed", self.id),
325            ).json(opts)
326        )
327    }
328
329    /// Modifies a set of role positions in the guild.
330    pub fn modify_role_positions(&self) {}
331
332    /// Removes a role from the specified guild member.
333    pub fn remove_member_role(&self, member: &Snowflake, role: &Snowflake) -> impl Future<Item=(), Error=Error> {
334        self.client.request_empty(Endpoint::new(
335            Method::DELETE,
336            format!("/guilds/{}/members/{}/roles/{}", self.id, member.0, role.0),
337        ))
338    }
339
340    /// Modifies a guild member in this guild.
341    pub fn modify_member(&self, id: &Snowflake, opts: ModifyMemberOptions) -> impl Future<Item=(), Error=Error> {
342        self.client.request_empty(
343            Endpoint::new(
344                Method::PATCH,
345                format!("/guilds/{}/members/{}", self.id, id.0),
346            ).json(opts)
347        )
348    }
349
350    /// Removes a guild member from the guild.
351    pub fn remove_member(&self, member: &Snowflake) -> impl Future<Item=(), Error=Error> {
352        self.client.request_empty(Endpoint::new(
353            Method::DELETE,
354            format!("/guilds/{}/members/{}", self.id, member.0),
355        ))
356    }
357
358    /// Sets the nickname of the current client user.
359    pub fn set_current_user_nick(&self, nick: &str) -> impl Future<Item=String, Error=Error> {
360        let json = json!({
361            "nick": nick
362        });
363
364        self.client.request(
365            Endpoint::new(
366                Method::PATCH,
367                format!("/guilds/{}/members/@me/nick", self.id),
368            ).json(json)
369        )
370    }
371}