Add timer and better handling of status
Some checks failed
ci/woodpecker/push/build Pipeline was canceled
Some checks failed
ci/woodpecker/push/build Pipeline was canceled
This commit is contained in:
@@ -12,6 +12,8 @@ use serenity::{
|
||||
},
|
||||
};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub struct Handler {
|
||||
pub db: Arc<Database>,
|
||||
@@ -19,11 +21,37 @@ pub struct Handler {
|
||||
|
||||
#[async_trait]
|
||||
impl serenity::client::EventHandler for Handler {
|
||||
async fn ready(&self, _ctx: Context, ready: Ready) {
|
||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||
let shard_info = ready
|
||||
.shard
|
||||
.map_or("N/A".to_string(), |s| format!("{}/{}", s.id.0, s.total));
|
||||
println!("{} is connected! [Shard {}]", ready.user.name, shard_info);
|
||||
|
||||
let db = Arc::clone(&self.db);
|
||||
tokio::spawn(async move {
|
||||
let mut last_guild_count = 0i64;
|
||||
let mut last_ban_count = 0i64;
|
||||
loop {
|
||||
let guild_count = ctx.cache.guild_count() as i64;
|
||||
let ban_count = db.total_bans().unwrap_or(0);
|
||||
if guild_count != last_guild_count || ban_count != last_ban_count {
|
||||
ctx.set_presence(
|
||||
Some(ActivityData::watching(format!(
|
||||
"{} guilds | {} bans",
|
||||
guild_count, ban_count
|
||||
))),
|
||||
OnlineStatus::Online,
|
||||
);
|
||||
println!(
|
||||
"Status updated: {} guilds | {} total bans",
|
||||
guild_count, ban_count
|
||||
);
|
||||
last_guild_count = guild_count;
|
||||
last_ban_count = ban_count;
|
||||
}
|
||||
sleep(Duration::from_secs(300)).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async fn guild_create(&self, ctx: Context, guild: Guild, _is_new: Option<bool>) {
|
||||
|
||||
Reference in New Issue
Block a user