From b32d972115fa4b9d93315af5c289f59c1dad5788 Mon Sep 17 00:00:00 2001 From: Chen-Pang He Date: Mon, 6 May 2024 02:19:15 +0800 Subject: [PATCH] Update top.gg stats every 3 hours --- Cargo.toml | 4 +++- src/main.rs | 22 ++++++++++------- src/topgg.rs | 67 ---------------------------------------------------- 3 files changed, 17 insertions(+), 76 deletions(-) delete mode 100644 src/topgg.rs diff --git a/Cargo.toml b/Cargo.toml index 6e2e846..63e2943 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "natsuki" -version = "3.0.3" +version = "3.0.4" authors = [ "Chen-Pang He (https://jdh8.org)", "u/YourWrstNightmare (https://www.reddit.com/user/YourWrstNightmare)", @@ -18,6 +18,7 @@ anyhow = "1.0.75" base64 = "0.22.1" bitflags = "2.4.0" chrono = "0.4.30" +command_attr = "0.5.1" csscolorparser = "0.6.2" futures = "0.3.28" image = "0.25.0" @@ -31,5 +32,6 @@ shuttle-runtime = "0.44.0" shuttle-serenity = "0.44.0" strum = { version = "0.26", features = ["derive"] } tokio = { version = "1.32.0", features = ["full"] } +topgg = { version = "1.4.1", features = ["autoposter", "serenity-cached"] } tree_magic_mini = "3.0.3" webp = "0.3.0" diff --git a/src/main.rs b/src/main.rs index 602b9a8..46d7842 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,6 @@ mod fun; mod information; mod tools; mod weeb; -mod topgg; use anyhow::Context as _; use poise::serenity_prelude as serenity; @@ -57,9 +56,13 @@ fn get_commands() -> Vec> { async fn main( #[shuttle_runtime::Secrets] secrets: shuttle_runtime::SecretStore, ) -> shuttle_serenity::ShuttleSerenity { - let poster = topgg::Poster::new(secrets.get("TOP_GG_TOKEN")); let token = secrets.get("TOKEN").context("Discord token not found")?; + let poster = secrets.get("TOP_GG_TOKEN").map(|token| { + let client = topgg::Client::new(token); + topgg::Autoposter::serenity(&client, std::time::Duration::from_secs(10800)).handler() + }); + let framework = poise::Framework::builder() .options(poise::FrameworkOptions { commands: secrets.get("CLEAR").map_or_else(get_commands, |_| Vec::new()), @@ -80,12 +83,15 @@ async fn main( }) .build(); - Ok(serenity::ClientBuilder::new(token, serenity::GatewayIntents::non_privileged()) - .framework(framework) - .event_handler(poster) - .await - .map_err(shuttle_runtime::CustomError::new)? - .into()) + let client = serenity::ClientBuilder + ::new(token, serenity::GatewayIntents::non_privileged()) + .framework(framework); + + let client = match poster { + Some(p) => client.event_handler_arc(p), + None => client, + }; + Ok(client.await.map_err(shuttle_runtime::CustomError::new)?.into()) } #[macro_export] diff --git a/src/topgg.rs b/src/topgg.rs deleted file mode 100644 index 502c51e..0000000 --- a/src/topgg.rs +++ /dev/null @@ -1,67 +0,0 @@ -use crate::bot_id; -use poise::serenity_prelude as serenity; -use tokio::sync::Mutex; - -#[derive(Debug, Default, Clone, Copy)] -struct Stats { - guilds: usize, - shards: u32, -} - -impl Stats { - fn serialize(self) -> serde_json::Value { - if self.shards == 0 { - serde_json::json!({ - "server_count": self.guilds, - }) - } - else { - serde_json::json!({ - "server_count": self.guilds, - "shard_count": self.shards, - }) - } - } -} - -async fn post(token: &str, stats: Stats) { - let _ = reqwest::Client::new() - .post(concat!("https://top.gg/api/bots/", bot_id!(), "/stats")) - .header("Authorization", token) - .json(&stats.serialize()) - .send().await; -} - -#[derive(Debug, Default)] -pub struct Poster { - token: Option, - stats: Mutex, -} - -impl Poster { - pub fn new(token: Option) -> Self { - Self { token, ..Default::default() } - } -} - -#[allow(clippy::significant_drop_tightening)] -#[serenity::async_trait] -impl serenity::EventHandler for Poster { - async fn ready(&self, _: serenity::Context, ready: serenity::Ready) { - let Some(token) = self.token.as_deref() else { return }; - let guilds = ready.guilds.len(); - - match ready.shard.map_or(0, |s| s.total) { - s@0..=1 => post(token, Stats { guilds, shards: s }).await, - shards => { - let mut stats = self.stats.lock().await; - stats.guilds += guilds; - stats.shards += 1; - - if stats.shards == shards { - post(token, *stats).await; - } - }, - }; - } -} \ No newline at end of file