From ecaa382a1e1c2fdc961b678b6a60ff43fc10587e Mon Sep 17 00:00:00 2001 From: jude Date: Sun, 11 Sep 2022 17:38:53 +0100 Subject: [PATCH] Add join message --- src/event_handlers.rs | 100 ++++++++++++++++++++++++++---------------- src/main.rs | 10 +---- 2 files changed, 64 insertions(+), 46 deletions(-) diff --git a/src/event_handlers.rs b/src/event_handlers.rs index 6c9ff56..698a34e 100644 --- a/src/event_handlers.rs +++ b/src/event_handlers.rs @@ -1,11 +1,12 @@ use std::{collections::HashMap, env}; +use log::error; use poise::{ serenity_prelude as serenity, serenity_prelude::{model::application::interaction::Interaction, utils::shard_id}, }; -use crate::{component_models::ComponentDataModel, Data, Error}; +use crate::{component_models::ComponentDataModel, Data, Error, THEME_COLOR}; pub async fn listener( ctx: &serenity::Context, @@ -26,46 +27,36 @@ pub async fn listener( if *is_new { let guild_id = guild.id.as_u64().to_owned(); - sqlx::query!("INSERT INTO guilds (guild) VALUES (?)", guild_id) + sqlx::query!("INSERT IGNORE INTO guilds (guild) VALUES (?)", guild_id) .execute(&data.database) - .await - .unwrap(); + .await?; - if let Ok(token) = env::var("DISCORDBOTS_TOKEN") { - let shard_count = ctx.cache.shard_count(); - let current_shard_id = shard_id(guild_id, shard_count); + if let Err(e) = post_guild_count(ctx, &data.http, guild_id).await { + error!("DiscordBotList: {:?}", e); + } - let guild_count = ctx - .cache - .guilds() - .iter() - .filter(|g| { - shard_id(g.as_u64().to_owned(), shard_count) == current_shard_id + let default_channel = guild.default_channel_guaranteed(); + + if let Some(default_channel) = default_channel { + default_channel + .send_message(&ctx, |m| { + m.embed(|e| { + e.title("Thank you for adding Reminder Bot!").description( + "To get started: +• Set your timezone with `/timezone` +• Set up permissions in Server Settings 🠚 Integrations 🠚 Reminder Bot (desktop only) +• Create your first reminder with `/remind` + +__Support__ +If you need any support, please come and ask us! Join our [Discord](https://discord.jellywx.com). + +__Updates__ +To stay up to date on the latest features and fixes, join our [Discord](https://discord.jellywx.com). +", + ).color(*THEME_COLOR) + }) }) - .count() as u64; - - let mut hm = HashMap::new(); - hm.insert("server_count", guild_count); - hm.insert("shard_id", current_shard_id); - hm.insert("shard_count", shard_count); - - let response = data - .http - .post( - format!( - "https://top.gg/api/bots/{}/stats", - ctx.cache.current_user_id().as_u64() - ) - .as_str(), - ) - .header("Authorization", token) - .json(&hm) - .send() - .await; - - if let Err(res) = response { - println!("DiscordBots Response: {:?}", res); - } + .await?; } } } @@ -86,3 +77,38 @@ pub async fn listener( Ok(()) } + +async fn post_guild_count( + ctx: &serenity::Context, + http: &reqwest::Client, + guild_id: u64, +) -> Result<(), reqwest::Error> { + if let Ok(token) = env::var("DISCORDBOTS_TOKEN") { + let shard_count = ctx.cache.shard_count(); + let current_shard_id = shard_id(guild_id, shard_count); + + let guild_count = ctx + .cache + .guilds() + .iter() + .filter(|g| shard_id(g.as_u64().to_owned(), shard_count) == current_shard_id) + .count() as u64; + + let mut hm = HashMap::new(); + hm.insert("server_count", guild_count); + hm.insert("shard_id", current_shard_id); + hm.insert("shard_count", shard_count); + + http.post( + format!("https://top.gg/api/bots/{}/stats", ctx.cache.current_user_id().as_u64()) + .as_str(), + ) + .header("Authorization", token) + .json(&hm) + .send() + .await + .map(|_| ()) + } else { + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index 2fb657a..3a5fbf1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -181,15 +181,7 @@ async fn _main(tx: Sender<()>) -> Result<(), Box> { .token(discord_token) .user_data_setup(move |ctx, _bot, framework| { Box::pin(async move { - register_application_commands( - ctx, - framework, - env::var("DEBUG_GUILD") - .map(|inner| GuildId(inner.parse().expect("DEBUG_GUILD not valid"))) - .ok(), - ) - .await - .unwrap(); + register_application_commands(ctx, framework, None).await.unwrap(); let kill_tx = tx.clone(); let kill_recv = tx.subscribe();