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
use crate::error::{KickApiError, Result};
use crate::models::{BanRequest, UnbanRequest};
use reqwest;
/// Moderation API - handles ban/unban endpoints
///
/// Scopes required: `moderation:ban`
pub struct ModerationApi<'a> {
client: &'a reqwest::Client,
token: &'a Option<String>,
base_url: &'a str,
}
impl<'a> ModerationApi<'a> {
/// Create a new ModerationApi instance
pub(crate) fn new(
client: &'a reqwest::Client,
token: &'a Option<String>,
base_url: &'a str,
) -> Self {
Self {
client,
token,
base_url,
}
}
/// Ban or timeout a user in a channel
///
/// If `duration` is provided in the request, this is a timeout (temporary ban).
/// If `duration` is `None`, this is a permanent ban.
///
/// Requires OAuth token with `moderation:ban` scope
///
/// # Example
/// ```no_run
/// use kick_api::BanRequest;
///
/// // Permanent ban
/// let request = BanRequest {
/// broadcaster_user_id: 12345,
/// user_id: 67890,
/// reason: Some("Breaking rules".to_string()),
/// duration: None,
/// };
/// client.moderation().ban(request).await?;
/// ```
pub async fn ban(&self, request: BanRequest) -> Result<()> {
super::require_token(self.token)?;
let url = format!("{}/moderation/bans", self.base_url);
let request = self
.client
.post(&url)
.header("Accept", "*/*")
.bearer_auth(self.token.as_ref().unwrap())
.json(&request);
let response = crate::http::send_with_retry(self.client, request).await?;
if response.status().is_success() {
Ok(())
} else {
Err(KickApiError::ApiError(format!(
"Failed to ban user: {}",
response.status()
)))
}
}
/// Unban a user in a channel
///
/// Requires OAuth token with `moderation:ban` scope
///
/// # Example
/// ```no_run
/// use kick_api::UnbanRequest;
///
/// let request = UnbanRequest {
/// broadcaster_user_id: 12345,
/// user_id: 67890,
/// };
/// client.moderation().unban(request).await?;
/// ```
pub async fn unban(&self, request: UnbanRequest) -> Result<()> {
super::require_token(self.token)?;
let url = format!("{}/moderation/bans", self.base_url);
let request = self
.client
.delete(&url)
.header("Accept", "*/*")
.bearer_auth(self.token.as_ref().unwrap())
.json(&request);
let response = crate::http::send_with_retry(self.client, request).await?;
if response.status().is_success() {
Ok(())
} else {
Err(KickApiError::ApiError(format!(
"Failed to unban user: {}",
response.status()
)))
}
}
}