-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathgardener.rs
44 lines (40 loc) · 1.35 KB
/
gardener.rs
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
use std::sync::Arc;
use tbot::Bot;
use tokio::{
self,
sync::Mutex,
time::{self, Duration},
};
use crate::data::Database;
use crate::BOT_ID;
pub fn start_pruning(bot: Bot, db: Arc<Mutex<Database>>) {
let mut interval = time::interval(Duration::from_secs(1 * 24 * 60 * 60));
tokio::spawn(async move {
loop {
interval.tick().await;
if let Err(e) = prune(&bot, &db).await {
crate::print_error(e);
}
}
});
}
async fn prune(bot: &Bot, db: &Mutex<Database>) -> Result<(), tbot::errors::MethodCall> {
let subscribers = db.lock().await.all_subscribers();
for subscriber in subscribers {
let chat_id = tbot::types::chat::Id(subscriber);
let chat = bot.get_chat(chat_id).call().await?;
if chat.kind.is_group() || chat.kind.is_supergroup() || chat.kind.is_channel() {
let me = bot
.get_chat_member(chat_id, *BOT_ID.get().unwrap())
.call()
.await?;
// Bots can only be added as administrators in channel,
// so we don't need to check that.
// And just ignore `can_post_messages` or `can_send_messages`
if me.status.is_left() || me.status.is_kicked() {
db.lock().await.delete_subscriber(subscriber);
}
}
}
Ok(())
}