Skip to main content

bpi_rs/live/
silent_user_manage.rs

1use serde::{ Deserialize, Serialize };
2
3use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
4
5#[derive(Debug, Serialize, Clone, Deserialize)]
6pub struct SilentUserInfo {
7    /// 禁言者uid
8    pub tuid: i64,
9    /// 禁言者昵称
10    pub tname: String,
11    /// 发起者uid
12    pub uid: i64,
13    /// 发起者昵称
14    pub name: String,
15    /// 禁言时间
16    pub ctime: String,
17    /// 禁言记录Id
18    pub id: i64,
19    /// 不明
20    pub is_anchor: i32,
21    /// 禁言者头像
22    pub face: String,
23    /// 发起者权限
24    pub admin_level: i32,
25}
26
27#[derive(Debug, Serialize, Clone, Deserialize)]
28pub struct SilentUserListData {
29    /// 禁言列表
30    pub data: Vec<SilentUserInfo>,
31    /// 禁言观众数量
32    pub total: i32,
33    /// 页码总数量
34    pub total_page: i32,
35}
36
37impl BpiClient {
38    /// 禁言观众
39    /// tuid: 用户uid
40    /// hour: -1永久 0本场直播
41    pub async fn live_add_silent_user(
42        &self,
43        room_id: i64,
44        tuid: i64,
45        hour: i32
46    ) -> Result<BpiResponse<serde_json::Value>, BpiError> {
47        let csrf = self.csrf()?;
48
49        let block_type = if hour == 0 {"2".to_string()} else {"1".to_string()};
50        
51        let form = vec![
52            ("room_id", room_id.to_string()),
53            ("tuid", tuid.to_string()),
54            ("mobile_app", "web".to_string()),
55            ("type", block_type),
56            ("hour", hour.to_string()),
57            ("csrf_token", csrf.clone()),
58            ("csrf", csrf)
59        ];
60
61        // if let Some(msg) = msg {
62        //     form.push(("msg", msg.to_string()));
63        // }
64
65        self
66            .post("https://api.live.bilibili.com/xlive/web-ucenter/v1/banned/AddSilentUser")
67            .form(&form)
68            .send_bpi("禁言观众").await
69    }
70
71    /// 查询直播间禁言列表
72    ///
73    pub async fn live_list_silent_users(
74        &self,
75        room_id: i64,
76        ps: i32
77    ) -> Result<BpiResponse<SilentUserListData>, BpiError> {
78        let csrf = self.csrf()?;
79
80        let form = vec![
81            ("room_id", room_id.to_string()),
82            ("ps", ps.to_string()),
83            ("csrf_token", csrf.clone()),
84            ("csrf", csrf)
85        ];
86
87        self
88            .post("https://api.live.bilibili.com/xlive/web-ucenter/v1/banned/GetSilentUserList")
89            .form(&form)
90            .send_bpi("查询直播间禁言列表").await
91    }
92
93    /// 解除禁言
94    ///
95    pub async fn live_del_block_user(
96        &self,
97        roomid: i64,
98        id: i64
99    ) -> Result<BpiResponse<serde_json::Value>, BpiError> {
100        let csrf = self.csrf()?;
101
102        let form = vec![
103            ("room_id", roomid.to_string()),
104            ("tuid", id.to_string()),
105            ("csrf_token", csrf.clone()),
106            ("csrf", csrf)
107        ];
108
109        self
110            .post("https://api.live.bilibili.com/xlive/web-ucenter/v1/banned/DelSilentUser")
111            .form(&form)
112            .send_bpi("解除禁言").await
113    }
114}
115
116#[cfg(test)]
117mod tests {
118    use super::*;
119
120    #[tokio::test]
121    async fn test_get_silent_user_list() {
122        let bpi = BpiClient::new();
123        let resp = bpi.live_list_silent_users(3818081, 1).await.unwrap();
124        tracing::info!("{:?}", resp);
125    }
126
127    #[tokio::test]
128    async fn test_add_silent_user() {
129        let bpi = BpiClient::new();
130        let resp = bpi.live_add_silent_user(3818081, 316183842, 0).await.unwrap();
131        tracing::info!("{:?}", resp);
132    }
133
134    #[tokio::test]
135    async fn test_del_silent_user_list() {
136        let bpi = BpiClient::new();
137        let resp = bpi.live_del_block_user(3818081, 316183842).await.unwrap();
138        tracing::info!("{:?}", resp);
139    }
140}