refluxer 0.2.0

Rust API wrapper for Fluxer
Documentation
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
use crate::error::HttpError;
use crate::http::client::HttpClient;
use crate::http::routing::Route;
use crate::model::audit_log::AuditLog;
use crate::model::channel::{Channel, ChannelType};
use crate::model::guild::{Guild, Member};
use crate::model::id::{ChannelId, GuildId, RoleId, UserId};
use serde::{Deserialize, Serialize};

impl HttpClient {
    /// Create a new guild owned by the current user.
    pub async fn create_guild(&self, params: &CreateGuild) -> Result<Guild, HttpError> {
        self.request(Route::CreateGuild, Some(params)).await
    }

    /// Delete a guild. Requires sudo credentials (password + optional MFA).
    /// The body accepts any serializable sudo shape required by the deployment.
    pub async fn delete_guild(
        &self,
        guild_id: GuildId,
        sudo: &impl Serialize,
    ) -> Result<(), HttpError> {
        self.request_empty(Route::DeleteGuild2 { guild_id }, Some(sudo))
            .await
    }

    /// Transfer guild ownership to another member. Requires sudo credentials
    /// on the caller's side; body shape is left to the caller.
    pub async fn transfer_guild_ownership(
        &self,
        guild_id: GuildId,
        body: &impl Serialize,
    ) -> Result<Guild, HttpError> {
        self.request(Route::TransferGuildOwnership { guild_id }, Some(body))
            .await
    }

    pub async fn toggle_detached_banner(
        &self,
        guild_id: GuildId,
        enabled: bool,
    ) -> Result<(), HttpError> {
        #[derive(Serialize)]
        struct Body {
            enabled: bool,
        }

        self.request_empty(
            Route::ToggleDetachedBanner { guild_id },
            Some(&Body { enabled }),
        )
        .await
    }

    pub async fn toggle_text_channel_flexible_names(
        &self,
        guild_id: GuildId,
        enabled: bool,
    ) -> Result<(), HttpError> {
        #[derive(Serialize)]
        struct Body {
            enabled: bool,
        }

        self.request_empty(
            Route::ToggleTextChannelFlexibleNames { guild_id },
            Some(&Body { enabled }),
        )
        .await
    }

    /// Reorder the hoist positions of one or more roles.
    pub async fn update_role_hoist_positions(
        &self,
        guild_id: GuildId,
        positions: &[RoleHoistPosition],
    ) -> Result<(), HttpError> {
        self.request_empty(
            Route::UpdateRoleHoistPositions { guild_id },
            Some(&positions),
        )
        .await
    }

    /// Reset all role hoist positions to defaults.
    pub async fn reset_role_hoist_positions(&self, guild_id: GuildId) -> Result<(), HttpError> {
        self.request_empty(Route::ResetRoleHoistPositions { guild_id }, None::<&()>)
            .await
    }

    pub async fn get_guild(&self, guild_id: GuildId) -> Result<Guild, HttpError> {
        self.request_no_body(Route::GetGuild { guild_id }).await
    }
    pub async fn get_guild_preview(&self, guild_id: GuildId) -> Result<Guild, HttpError> {
        self.request_no_body(Route::GetGuildPreview { guild_id })
            .await
    }
    pub async fn modify_guild(
        &self,
        guild_id: GuildId,
        params: &ModifyGuild,
    ) -> Result<Guild, HttpError> {
        self.request(Route::ModifyGuild { guild_id }, Some(params))
            .await
    }
    pub async fn get_guild_channels(&self, guild_id: GuildId) -> Result<Vec<Channel>, HttpError> {
        self.request_no_body(Route::GetGuildChannels { guild_id })
            .await
    }
    pub async fn create_guild_channel(
        &self,
        guild_id: GuildId,
        params: &CreateGuildChannel,
    ) -> Result<Channel, HttpError> {
        self.request(Route::CreateGuildChannel { guild_id }, Some(params))
            .await
    }
    pub async fn get_guild_members(&self, guild_id: GuildId) -> Result<Vec<Member>, HttpError> {
        self.request_no_body(Route::GetGuildMembers { guild_id })
            .await
    }
    pub async fn get_guild_member(
        &self,
        guild_id: GuildId,
        user_id: UserId,
    ) -> Result<Member, HttpError> {
        self.request_no_body(Route::GetGuildMember { guild_id, user_id })
            .await
    }
    pub async fn modify_guild_member(
        &self,
        guild_id: GuildId,
        user_id: UserId,
        params: &ModifyGuildMember,
    ) -> Result<Member, HttpError> {
        self.request(Route::ModifyGuildMember { guild_id, user_id }, Some(params))
            .await
    }
    pub async fn modify_current_member(
        &self,
        guild_id: GuildId,
        params: &ModifyCurrentMember,
    ) -> Result<Member, HttpError> {
        self.request(Route::ModifyCurrentMember { guild_id }, Some(params))
            .await
    }

    /// Fetch the current user's member object for a guild.
    pub async fn get_current_guild_member(&self, guild_id: GuildId) -> Result<Member, HttpError> {
        self.request_no_body(Route::GetCurrentGuildMember { guild_id })
            .await
    }

    /// List guilds the current user is in. `params` supports pagination.
    pub async fn list_current_user_guilds(
        &self,
        params: &ListCurrentUserGuilds,
    ) -> Result<Vec<Guild>, HttpError> {
        self.request_no_body(Route::ListCurrentUserGuilds {
            query: params.to_query(),
        })
        .await
    }

    /// Leave a guild.
    pub async fn leave_guild(&self, guild_id: GuildId) -> Result<(), HttpError> {
        self.request_empty(Route::LeaveGuild { guild_id }, None::<&()>)
            .await
    }

    /// List audit log entries for a guild.
    pub async fn list_guild_audit_logs(
        &self,
        guild_id: GuildId,
        params: &ListAuditLogsParams,
    ) -> Result<AuditLog, HttpError> {
        self.request_no_body(Route::ListGuildAuditLogs {
            guild_id,
            query: params.to_query(),
        })
        .await
    }

    /// Search guild members by name, role, and other criteria.
    pub async fn search_guild_members(
        &self,
        guild_id: GuildId,
        params: &GuildMemberSearch,
    ) -> Result<GuildMemberSearchResponse, HttpError> {
        self.request(Route::SearchGuildMembers { guild_id }, Some(params))
            .await
    }

    /// Reorder channels in a guild. Each entry moves one channel to a new
    /// position; server applies them atomically.
    pub async fn update_guild_channel_positions(
        &self,
        guild_id: GuildId,
        positions: &[ChannelPositionUpdate],
    ) -> Result<(), HttpError> {
        self.request_empty(
            Route::UpdateGuildChannelPositions { guild_id },
            Some(&positions),
        )
        .await
    }

    /// Fetch a guild's vanity URL invite code and its usage counter.
    pub async fn get_guild_vanity_url(&self, guild_id: GuildId) -> Result<VanityUrl, HttpError> {
        self.request_no_body(Route::GetGuildVanityUrl { guild_id })
            .await
    }

    /// Set or clear a guild's vanity URL. Pass `None` to clear.
    pub async fn update_guild_vanity_url(
        &self,
        guild_id: GuildId,
        code: Option<&str>,
    ) -> Result<VanityUrl, HttpError> {
        #[derive(Serialize)]
        struct Body<'a> {
            code: Option<&'a str>,
        }
        self.request(
            Route::UpdateGuildVanityUrl { guild_id },
            Some(&Body { code }),
        )
        .await
    }

    /// Reorder roles in a guild.
    pub async fn update_guild_role_positions(
        &self,
        guild_id: GuildId,
        positions: &[RolePositionUpdate],
    ) -> Result<Vec<crate::model::guild::Role>, HttpError> {
        self.request(
            Route::UpdateGuildRolePositions { guild_id },
            Some(&positions),
        )
        .await
    }
}

// ---------------------------------------------------------------------------
// Request bodies
// ---------------------------------------------------------------------------

#[derive(Debug, serde::Serialize)]
pub struct CreateGuildChannel {
    pub name: String,
    #[serde(rename = "type")]
    pub kind: ChannelType,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub topic: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nsfw: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<ChannelId>,
}

impl CreateGuildChannel {
    pub fn text(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            kind: ChannelType::Text,
            topic: None,
            nsfw: None,
            parent_id: None,
        }
    }
    pub fn voice(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            kind: ChannelType::Voice,
            topic: None,
            nsfw: None,
            parent_id: None,
        }
    }
}

#[derive(Debug, Default, serde::Serialize)]
pub struct ModifyGuild {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub afk_channel_id: Option<ChannelId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub afk_timeout: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system_channel_id: Option<ChannelId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rules_channel_id: Option<ChannelId>,
}

#[derive(Debug, Default, serde::Serialize)]
pub struct ModifyGuildMember {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nick: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub roles: Option<Vec<RoleId>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mute: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub deaf: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub channel_id: Option<ChannelId>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub communication_disabled_until: Option<String>,
}

impl ModifyGuildMember {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn nick(mut self, nick: impl Into<String>) -> Self {
        self.nick = Some(nick.into());
        self
    }
    pub fn mute(mut self, mute: bool) -> Self {
        self.mute = Some(mute);
        self
    }
    pub fn deaf(mut self, deaf: bool) -> Self {
        self.deaf = Some(deaf);
        self
    }
    pub fn roles(mut self, roles: Vec<RoleId>) -> Self {
        self.roles = Some(roles);
        self
    }
    pub fn timeout(mut self, until: impl Into<String>) -> Self {
        self.communication_disabled_until = Some(until.into());
        self
    }
}

#[derive(Debug, Default, serde::Serialize)]
pub struct ModifyCurrentMember {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nick: Option<String>,
}

impl ModifyCurrentMember {
    pub fn nick(nick: impl Into<String>) -> Self {
        Self {
            nick: Some(nick.into()),
        }
    }
}

#[derive(Debug, Default, Clone)]
pub struct ListAuditLogsParams {
    pub user_id: Option<UserId>,
    /// Numeric `AuditLogEvent` value — e.g. `22` for `MemberBanAdd`.
    pub action_type: Option<u32>,
    pub before: Option<String>,
    pub after: Option<String>,
    pub limit: Option<u32>,
}

impl ListAuditLogsParams {
    fn to_query(&self) -> String {
        let mut parts: Vec<String> = Vec::new();
        if let Some(v) = self.user_id {
            parts.push(format!("user_id={v}"));
        }
        if let Some(v) = self.action_type {
            parts.push(format!("action_type={v}"));
        }
        if let Some(v) = &self.before {
            parts.push(format!("before={}", urlencoding::encode(v)));
        }
        if let Some(v) = &self.after {
            parts.push(format!("after={}", urlencoding::encode(v)));
        }
        if let Some(v) = self.limit {
            parts.push(format!("limit={v}"));
        }
        if parts.is_empty() {
            String::new()
        } else {
            format!("?{}", parts.join("&"))
        }
    }
}

#[derive(Debug, Default, Clone)]
pub struct ListCurrentUserGuilds {
    pub before: Option<GuildId>,
    pub after: Option<GuildId>,
    pub limit: Option<u32>,
}

impl ListCurrentUserGuilds {
    fn to_query(&self) -> String {
        let mut parts: Vec<String> = Vec::new();
        if let Some(v) = self.before {
            parts.push(format!("before={v}"));
        }
        if let Some(v) = self.after {
            parts.push(format!("after={v}"));
        }
        if let Some(v) = self.limit {
            parts.push(format!("limit={v}"));
        }
        if parts.is_empty() {
            String::new()
        } else {
            format!("?{}", parts.join("&"))
        }
    }
}

#[derive(Debug, Default, Serialize)]
pub struct GuildMemberSearch {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub query: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<u32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<u64>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub role_ids: Vec<RoleId>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct GuildMemberSearchResponse {
    pub guild_id: GuildId,
    pub members: Vec<GuildMemberSearchResult>,
    pub page_result_count: u64,
    pub total_result_count: u64,
    pub indexing: bool,
}

#[derive(Debug, Clone, Deserialize)]
pub struct GuildMemberSearchResult {
    pub user_id: UserId,
    pub username: String,
    #[serde(default)]
    pub global_name: Option<String>,
    #[serde(default)]
    pub nickname: Option<String>,
    #[serde(default)]
    pub role_ids: Vec<RoleId>,
    #[serde(default)]
    pub joined_at: Option<f64>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ChannelPositionUpdate {
    pub id: ChannelId,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub position: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub lock_permissions: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub parent_id: Option<ChannelId>,
}

#[derive(Debug, Clone, Serialize)]
pub struct RolePositionUpdate {
    pub id: RoleId,
    pub position: i32,
}

#[derive(Debug, Clone, Deserialize)]
pub struct VanityUrl {
    #[serde(default)]
    pub code: Option<String>,
    #[serde(default)]
    pub uses: u32,
}

#[derive(Debug, Serialize)]
pub struct CreateGuild {
    pub name: String,
    /// Base64-encoded data URI for the guild icon, e.g.
    /// `data:image/png;base64,iVBORw0KGgo...`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub empty_features: Option<bool>,
}

impl CreateGuild {
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            icon: None,
            empty_features: None,
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct RoleHoistPosition {
    pub id: RoleId,
    pub hoist_position: i64,
}