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 form = vec![
50            ("room_id", room_id.to_string()),
51            ("tuid", tuid.to_string()),
52            ("mobile_app", "web".to_string()),
53            ("hour", hour.to_string()),
54            ("csrf_token", csrf.clone()),
55            ("csrf", csrf)
56        ];
57
58        // if let Some(msg) = msg {
59        //     form.push(("msg", msg.to_string()));
60        // }
61
62        self
63            .post("https://api.live.bilibili.com/xlive/web-ucenter/v1/banned/AddSilentUser")
64            .form(&form)
65            .send_bpi("禁言观众").await
66    }
67
68    /// 查询直播间禁言列表
69    ///
70    pub async fn live_list_silent_users(
71        &self,
72        room_id: i64,
73        ps: i32
74    ) -> Result<BpiResponse<SilentUserListData>, BpiError> {
75        let csrf = self.csrf()?;
76
77        let form = vec![
78            ("room_id", room_id.to_string()),
79            ("ps", ps.to_string()),
80            ("csrf_token", csrf.clone()),
81            ("csrf", csrf)
82        ];
83
84        self
85            .post("https://api.live.bilibili.com/xlive/web-ucenter/v1/banned/GetSilentUserList")
86            .form(&form)
87            .send_bpi("查询直播间禁言列表").await
88    }
89
90    /// 解除禁言
91    ///
92    pub async fn live_del_block_user(
93        &self,
94        roomid: i64,
95        id: i64
96    ) -> Result<BpiResponse<serde_json::Value>, BpiError> {
97        let csrf = self.csrf()?;
98
99        let form = vec![
100            ("room_id", roomid.to_string()),
101            ("tuid", id.to_string()),
102            ("csrf_token", csrf.clone()),
103            ("csrf", csrf)
104        ];
105
106        self
107            .post("https://api.live.bilibili.com/xlive/web-ucenter/v1/banned/DelSilentUser")
108            .form(&form)
109            .send_bpi("解除禁言").await
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[tokio::test]
118    async fn test_get_silent_user_list() {
119        let bpi = BpiClient::new();
120        let resp = bpi.live_list_silent_users(3818081, 1).await.unwrap();
121        tracing::info!("{:?}", resp);
122    }
123
124    #[tokio::test]
125    async fn test_add_silent_user() {
126        let bpi = BpiClient::new();
127        let resp = bpi.live_add_silent_user(3818081, 316183842, 0).await.unwrap();
128        tracing::info!("{:?}", resp);
129    }
130
131    #[tokio::test]
132    async fn test_del_silent_user_list() {
133        let bpi = BpiClient::new();
134        let resp = bpi.live_del_block_user(3818081, 316183842).await.unwrap();
135        tracing::info!("{:?}", resp);
136    }
137}