diff --git a/Cargo.lock b/Cargo.lock index 4840710..1a3ae56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1537,9 +1537,9 @@ checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] name = "syn" -version = "1.0.44" +version = "1.0.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e03e57e4fcbfe7749842d53e24ccb9aa12b7252dbe5e91d2acad31834c8b8fdd" +checksum = "ea9c5432ff16d6152371f808fb5a871cd67368171b09bb21b43df8e4a47a3556" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index fb69aaf..a054768 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,23 +7,23 @@ edition = "2018" [dependencies] serenity = { version = "0.9.0-rc.2", features = ["collector", "rustls_backend"] } dotenv = "0.15" -tokio = { version = "0.2.19", features = ["process"] } -reqwest = "0.10.6" -sqlx = { version = "0.3.5", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal", "chrono"] } -regex = "1.3.9" -async-trait = "0.1.36" -log = "0.4.11" -env_logger = "0.8.1" +tokio = { version = "0.2", features = ["process"] } +reqwest = { version = "0.10", features = ["rustls-tls"] } +sqlx = { version = "0.3", default-features = false, features = ["runtime-tokio", "macros", "mysql", "bigdecimal", "chrono"] } +regex = "1.3" +async-trait = "0.1" +log = "0.4" +env_logger = "0.8" chrono = "0.4" chrono-tz = "0.5" -lazy_static = "1.4.0" -num-integer = "0.1.43" -num-traits = "0.2.12" -custom_error = "1.7.1" -serde = "1.0.115" -serde_json = "1.0.57" -rand = "0.7.3" -Inflector = "0.11.4" +lazy_static = "1.4" +num-integer = "0.1" +num-traits = "0.2" +custom_error = "1.7" +serde = "1.0" +serde_json = "1.0" +rand = "0.7" +Inflector = "0.11" [dependencies.regex_command_attr] path = "./regex_command_attr" diff --git a/src/main.rs b/src/main.rs index d580cb6..32465e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,15 +8,18 @@ mod models; mod time_parser; use serenity::{ + async_trait, cache::Cache, client::{bridge::gateway::GatewayIntents, Client}, framework::Framework, http::{client::Http, CacheHttp}, model::{ + channel::GuildChannel, channel::Message, + guild::{Guild, PartialGuild}, id::{GuildId, UserId}, }, - prelude::TypeMapKey, + prelude::{Context, EventHandler, TypeMapKey}, }; use sqlx::{ @@ -26,7 +29,7 @@ use sqlx::{ use dotenv::dotenv; -use std::{env, sync::Arc}; +use std::{collections::HashMap, env, sync::Arc}; use crate::{ commands::{info_cmds, moderation_cmds, reminder_cmds, todo_cmds}, @@ -56,6 +59,87 @@ impl TypeMapKey for FrameworkCtx { type Value = Arc>; } +struct Handler; + +#[async_trait] +impl EventHandler for Handler { + async fn channel_delete(&self, ctx: Context, channel: &GuildChannel) { + let pool = ctx + .data + .read() + .await + .get::() + .cloned() + .expect("Could not get SQLPool from data"); + + sqlx::query!( + " +DELETE FROM channels WHERE channel = ? + ", + channel.id.as_u64() + ) + .execute(&pool) + .await + .unwrap(); + } + + async fn guild_create(&self, ctx: Context, _guild: Guild, is_new: bool) { + if is_new { + if let Ok(token) = env::var("DISCORDBOTS_TOKEN") { + let guild_count = ctx.cache.guild_count().await; + + let mut hm = HashMap::new(); + hm.insert("server_count", guild_count); + + let client = ctx + .data + .read() + .await + .get::() + .cloned() + .expect("Could not get ReqwestClient from data"); + + let response = client + .post( + format!( + "https://top.gg/api/bots/{}/stats", + ctx.cache.current_user_id().await.as_u64() + ) + .as_str(), + ) + .header("Authorization", token) + .json(&hm) + .send() + .await; + + if let Err(res) = response { + println!("DiscordBots Response: {:?}", res); + } + } + } + } + + async fn guild_delete(&self, ctx: Context, guild: PartialGuild, _guild: Option) { + let pool = ctx + .data + .read() + .await + .get::() + .cloned() + .expect("Could not get SQLPool from data"); + + sqlx::query!( + " +DELETE FROM guilds WHERE guild = ? + ", + guild.id.as_u64() + ) + .execute(&pool) + .await + .unwrap(); + } +} + #[tokio::main] async fn main() -> Result<(), Box> { env_logger::init(); @@ -123,6 +207,7 @@ async fn main() -> Result<(), Box> { | GatewayIntents::GUILDS | GatewayIntents::DIRECT_MESSAGES, ) + .event_handler(Handler) .framework_arc(framework_arc.clone()) .await .expect("Error occurred creating client");