2022-09-07 17:27:13 +00:00
|
|
|
use std::{collections::HashMap, env};
|
2022-02-19 13:28:24 +00:00
|
|
|
|
2022-09-11 16:38:53 +00:00
|
|
|
use log::error;
|
2022-02-19 18:21:11 +00:00
|
|
|
use poise::{
|
|
|
|
serenity_prelude as serenity,
|
2022-09-08 16:58:05 +00:00
|
|
|
serenity_prelude::{model::application::interaction::Interaction, utils::shard_id},
|
2022-02-19 18:21:11 +00:00
|
|
|
};
|
|
|
|
|
2022-09-11 16:38:53 +00:00
|
|
|
use crate::{component_models::ComponentDataModel, Data, Error, THEME_COLOR};
|
2022-02-19 18:21:11 +00:00
|
|
|
|
|
|
|
pub async fn listener(
|
|
|
|
ctx: &serenity::Context,
|
2022-04-19 14:23:27 +00:00
|
|
|
event: &poise::Event<'_>,
|
2022-02-19 18:21:11 +00:00
|
|
|
data: &Data,
|
|
|
|
) -> Result<(), Error> {
|
2022-02-19 14:32:03 +00:00
|
|
|
match event {
|
2022-05-13 11:43:27 +00:00
|
|
|
poise::Event::Ready { .. } => {
|
|
|
|
ctx.set_activity(serenity::Activity::watching("for /remind")).await;
|
|
|
|
}
|
2022-04-19 14:23:27 +00:00
|
|
|
poise::Event::ChannelDelete { channel } => {
|
|
|
|
sqlx::query!("DELETE FROM channels WHERE channel = ?", channel.id.as_u64())
|
2022-02-19 22:11:21 +00:00
|
|
|
.execute(&data.database)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-02-19 14:32:03 +00:00
|
|
|
}
|
2022-04-19 14:23:27 +00:00
|
|
|
poise::Event::GuildCreate { guild, is_new } => {
|
|
|
|
if *is_new {
|
|
|
|
let guild_id = guild.id.as_u64().to_owned();
|
|
|
|
|
2022-09-11 16:38:53 +00:00
|
|
|
sqlx::query!("INSERT IGNORE INTO guilds (guild) VALUES (?)", guild_id)
|
2022-04-19 14:23:27 +00:00
|
|
|
.execute(&data.database)
|
2022-09-11 16:38:53 +00:00
|
|
|
.await?;
|
2022-04-19 14:23:27 +00:00
|
|
|
|
2022-09-11 16:38:53 +00:00
|
|
|
if let Err(e) = post_guild_count(ctx, &data.http, guild_id).await {
|
|
|
|
error!("DiscordBotList: {:?}", e);
|
|
|
|
}
|
2022-04-19 14:23:27 +00:00
|
|
|
|
2022-09-11 16:38:53 +00:00
|
|
|
let default_channel = guild.default_channel_guaranteed();
|
2022-04-19 14:23:27 +00:00
|
|
|
|
2022-09-11 16:38:53 +00:00
|
|
|
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`
|
2022-04-19 14:23:27 +00:00
|
|
|
|
2022-09-11 16:38:53 +00:00
|
|
|
__Support__
|
|
|
|
If you need any support, please come and ask us! Join our [Discord](https://discord.jellywx.com).
|
2022-04-19 14:23:27 +00:00
|
|
|
|
2022-09-11 16:38:53 +00:00
|
|
|
__Updates__
|
|
|
|
To stay up to date on the latest features and fixes, join our [Discord](https://discord.jellywx.com).
|
|
|
|
",
|
|
|
|
).color(*THEME_COLOR)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.await?;
|
2022-04-19 14:23:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
poise::Event::GuildDelete { incomplete, .. } => {
|
|
|
|
let _ = sqlx::query!("DELETE FROM guilds WHERE guild = ?", incomplete.id.0)
|
2022-02-19 14:32:03 +00:00
|
|
|
.execute(&data.database)
|
|
|
|
.await;
|
|
|
|
}
|
2022-05-13 22:08:52 +00:00
|
|
|
poise::Event::InteractionCreate { interaction } => {
|
|
|
|
if let Interaction::MessageComponent(component) = interaction {
|
2022-02-19 18:21:11 +00:00
|
|
|
let component_model = ComponentDataModel::from_custom_id(&component.data.custom_id);
|
|
|
|
|
2022-04-19 14:23:27 +00:00
|
|
|
component_model.act(ctx, data, component).await;
|
2022-02-19 13:28:24 +00:00
|
|
|
}
|
2022-05-13 22:08:52 +00:00
|
|
|
}
|
2022-02-19 14:32:03 +00:00
|
|
|
_ => {}
|
2022-02-19 13:28:24 +00:00
|
|
|
}
|
2022-02-19 14:32:03 +00:00
|
|
|
|
|
|
|
Ok(())
|
2022-02-19 13:28:24 +00:00
|
|
|
}
|
2022-09-11 16:38:53 +00:00
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
|
|
|
}
|