2022-02-19 22:11:21 +00:00
|
|
|
use std::{collections::HashMap, env, sync::atomic::Ordering};
|
2022-02-19 13:28:24 +00:00
|
|
|
|
2022-03-21 23:11:52 +00:00
|
|
|
use log::{error, info, warn};
|
2022-02-19 18:21:11 +00:00
|
|
|
use poise::{
|
2022-04-09 11:21:28 +00:00
|
|
|
async_trait,
|
2022-02-19 18:21:11 +00:00
|
|
|
serenity::{model::interactions::Interaction, utils::shard_id},
|
|
|
|
serenity_prelude as serenity,
|
|
|
|
};
|
|
|
|
|
2022-04-09 11:21:28 +00:00
|
|
|
use crate::{component_models::ComponentDataModel, Data, Error, GuildId};
|
|
|
|
|
|
|
|
struct Handler;
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
impl serenity::EventHandler for Handler {
|
|
|
|
async fn guild_create(&self, ctx: serenity::Context, guild: serenity::Guild, is_new: bool) {
|
|
|
|
if is_new {
|
|
|
|
let guild_id = guild.id.as_u64().to_owned();
|
|
|
|
|
|
|
|
// todo
|
|
|
|
// sqlx::query!("INSERT INTO guilds (guild) VALUES (?)", guild_id)
|
|
|
|
// .execute(&data.database)
|
|
|
|
// .await
|
|
|
|
// .unwrap();
|
|
|
|
|
|
|
|
//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);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-19 18:21:11 +00:00
|
|
|
|
|
|
|
pub async fn listener(
|
|
|
|
ctx: &serenity::Context,
|
2022-04-09 11:21:28 +00:00
|
|
|
event: &serenity::Event,
|
2022-02-19 18:21:11 +00:00
|
|
|
data: &Data,
|
|
|
|
) -> Result<(), Error> {
|
2022-02-19 14:32:03 +00:00
|
|
|
match event {
|
2022-04-09 11:21:28 +00:00
|
|
|
serenity::Event::Ready(_) => {
|
2022-03-21 23:11:52 +00:00
|
|
|
info!("Cache Ready! Preparing extra processes");
|
2022-02-19 13:28:24 +00:00
|
|
|
|
2022-04-03 20:53:28 +00:00
|
|
|
if data
|
|
|
|
.is_loop_running
|
|
|
|
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
|
|
|
|
.is_ok()
|
|
|
|
{
|
2022-03-21 23:11:52 +00:00
|
|
|
let kill_tx = data.broadcast.clone();
|
|
|
|
let kill_recv = data.broadcast.subscribe();
|
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
let ctx1 = ctx.clone();
|
|
|
|
let ctx2 = ctx.clone();
|
2022-02-19 13:28:24 +00:00
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
let pool1 = data.database.clone();
|
|
|
|
let pool2 = data.database.clone();
|
2022-02-19 13:28:24 +00:00
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
let run_settings = env::var("DONTRUN").unwrap_or_else(|_| "".to_string());
|
2022-02-19 13:28:24 +00:00
|
|
|
|
2022-02-19 14:32:03 +00:00
|
|
|
if !run_settings.contains("postman") {
|
|
|
|
tokio::spawn(async move {
|
2022-03-21 23:11:52 +00:00
|
|
|
match postman::initialize(kill_recv, ctx1, &pool1).await {
|
|
|
|
Ok(_) => {}
|
|
|
|
Err(e) => {
|
|
|
|
error!("postman exiting: {}", e);
|
|
|
|
}
|
|
|
|
};
|
2022-02-19 14:32:03 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
warn!("Not running postman")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !run_settings.contains("web") {
|
|
|
|
tokio::spawn(async move {
|
2022-03-21 23:11:52 +00:00
|
|
|
reminder_web::initialize(kill_tx, ctx2, pool2).await.unwrap();
|
2022-02-19 14:32:03 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
warn!("Not running web")
|
|
|
|
}
|
2022-02-19 13:28:24 +00:00
|
|
|
}
|
2022-02-19 14:32:03 +00:00
|
|
|
}
|
2022-04-09 11:21:28 +00:00
|
|
|
serenity::Event::ChannelDelete(event) => {
|
|
|
|
sqlx::query!("DELETE FROM channels WHERE channel = ?", event.channel.id().0)
|
2022-02-19 22:11:21 +00:00
|
|
|
.execute(&data.database)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2022-02-19 14:32:03 +00:00
|
|
|
}
|
2022-04-09 11:21:28 +00:00
|
|
|
serenity::Event::GuildDelete(event) => {
|
|
|
|
let _ = sqlx::query!("DELETE FROM guilds WHERE guild = ?", event.guild.id.0)
|
2022-02-19 14:32:03 +00:00
|
|
|
.execute(&data.database)
|
|
|
|
.await;
|
|
|
|
}
|
2022-04-09 11:21:28 +00:00
|
|
|
serenity::Event::InteractionCreate(event) => match &event.interaction {
|
2022-02-19 13:28:24 +00:00
|
|
|
Interaction::MessageComponent(component) => {
|
2022-02-19 18:21:11 +00:00
|
|
|
let component_model = ComponentDataModel::from_custom_id(&component.data.custom_id);
|
|
|
|
|
2022-04-09 11:21:28 +00:00
|
|
|
component_model.act(ctx, data, &component).await;
|
2022-02-19 13:28:24 +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
|
|
|
}
|