2024-02-14 12:44:29 +00:00
|
|
|
use chrono::{Datelike, Local};
|
2024-02-14 12:22:29 +00:00
|
|
|
|
2024-02-16 20:54:07 +00:00
|
|
|
use serenity::{all::{GuildId, UserId}, builder::{CreateEmbed, CreateMessage}, client::Context, http::Http, model::Colour};
|
2024-02-14 12:22:29 +00:00
|
|
|
|
2024-02-15 19:27:52 +00:00
|
|
|
use anyhow::Ok;
|
|
|
|
|
2024-02-16 20:54:07 +00:00
|
|
|
use sqlx::{Connection, FromRow, SqliteConnection};
|
2024-02-15 19:27:52 +00:00
|
|
|
|
|
|
|
use crate::util::security::dotenv_var;
|
2024-02-16 20:54:07 +00:00
|
|
|
use crate::util::debug::send_error;
|
|
|
|
use crate::util::utilities;
|
2024-02-15 19:27:52 +00:00
|
|
|
|
2024-02-16 20:54:07 +00:00
|
|
|
use std::sync::Arc;
|
2024-02-14 12:22:29 +00:00
|
|
|
|
2024-02-16 20:54:07 +00:00
|
|
|
// pub async fn notice_wrapper(http: Arc<Http>) {
|
2024-02-14 12:22:29 +00:00
|
|
|
pub async fn notice_wrapper(ctx: Context) {
|
2024-02-16 20:54:07 +00:00
|
|
|
match notice(ctx.http.clone()).await {
|
2024-02-14 12:22:29 +00:00
|
|
|
Err(e) => {
|
|
|
|
send_error(ctx.http.clone(), e.to_string()).await;
|
2024-02-14 12:44:29 +00:00
|
|
|
return;
|
2024-02-15 19:27:52 +00:00
|
|
|
},
|
|
|
|
Result::Ok(_) => return
|
2024-02-14 12:22:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:07 +00:00
|
|
|
async fn announce_event(guild_id: GuildId, name: &str, special_message: &str, http: Arc<Http>) -> anyhow::Result<()> {
|
|
|
|
let mut event_embed = CreateEmbed::new()
|
|
|
|
.color(Colour::new(rand::random::<u32>() % 0xFFFFFF))
|
|
|
|
.title("Today's event is:");
|
|
|
|
|
|
|
|
let system_channel = utilities::get_system_channel(guild_id, http.clone()).await?;
|
|
|
|
|
|
|
|
if special_message.contains("http") {
|
|
|
|
event_embed = event_embed.description(name);
|
|
|
|
system_channel.send_message(http.clone(),
|
|
|
|
CreateMessage::new().add_embed(event_embed.clone()).content(special_message)).await?;
|
|
|
|
// Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
event_embed = event_embed.field(name, special_message, true);
|
|
|
|
system_channel.send_message(http.clone(),
|
|
|
|
CreateMessage::new().add_embed(event_embed)).await?;
|
|
|
|
|
|
|
|
Ok(())
|
2024-02-15 19:27:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:07 +00:00
|
|
|
async fn celebrate_birthday(guild_id: GuildId, user_id: UserId, nick: &str, http: Arc<Http>) -> anyhow::Result<()> {
|
|
|
|
let system_channel = utilities::get_system_channel(guild_id, http.clone()).await?;
|
|
|
|
|
|
|
|
let embed = CreateEmbed::new()
|
|
|
|
.color(Colour::new(rand::random::<u32>() % 0xFFFFFF))
|
|
|
|
.title(format!("HAPPY BIRTHDAY {}!", nick))
|
|
|
|
.description(format!("<@{}>'s birthday is today!!! Yay!", user_id.get()));
|
|
|
|
|
|
|
|
system_channel.send_message(http.clone(), CreateMessage::new().add_embed(embed.clone())).await?;
|
|
|
|
|
|
|
|
Ok(())
|
2024-02-15 19:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, FromRow, Debug)]
|
|
|
|
struct BirthdayRow {
|
|
|
|
#[sqlx(try_from="i64")]
|
|
|
|
id: u64,
|
2024-02-14 12:44:29 +00:00
|
|
|
nick: String,
|
2024-02-14 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 19:27:52 +00:00
|
|
|
#[derive(Clone, FromRow, Debug)]
|
|
|
|
struct EventRow {
|
2024-02-16 20:54:07 +00:00
|
|
|
id: u32,
|
2024-02-15 19:27:52 +00:00
|
|
|
#[sqlx(try_from="i64")]
|
|
|
|
guild: u64,
|
|
|
|
name: String,
|
2024-02-16 20:54:07 +00:00
|
|
|
year: i32,
|
2024-02-15 19:27:52 +00:00
|
|
|
special_message: String,
|
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:07 +00:00
|
|
|
async fn notice(http: Arc<Http>) -> anyhow::Result<()> {
|
2024-02-15 19:27:52 +00:00
|
|
|
use anyhow::Context;
|
|
|
|
|
2024-02-14 12:22:29 +00:00
|
|
|
let local = Local::now();
|
|
|
|
let day = local.day();
|
|
|
|
let month = local.month();
|
2024-02-16 20:54:07 +00:00
|
|
|
let year = local.year();
|
2024-02-14 12:22:29 +00:00
|
|
|
|
2024-02-15 19:27:52 +00:00
|
|
|
let db_path = dotenv_var("DATABASE_URL").context("DATABASE_URL not found in env")?;
|
2024-02-14 12:22:29 +00:00
|
|
|
|
2024-02-15 19:27:52 +00:00
|
|
|
let mut db = SqliteConnection::connect(db_path.as_str()).await?;
|
|
|
|
|
|
|
|
let birtdays = sqlx::query_as::<_, BirthdayRow>(
|
2024-02-16 20:54:07 +00:00
|
|
|
"SELECT id, nick FROM birthdays
|
2024-02-15 19:27:52 +00:00
|
|
|
WHERE day=? AND month=?;"
|
|
|
|
)
|
|
|
|
.bind(day)
|
|
|
|
.bind(month)
|
|
|
|
.fetch_all(&mut db)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
let global_events = sqlx::query_as::<_, EventRow>(
|
2024-02-16 20:54:07 +00:00
|
|
|
"SELECT id, guild, name, year, special_message from events
|
2024-02-15 19:27:52 +00:00
|
|
|
WHERE day=? AND month=? AND guild=0;"
|
2024-02-14 12:44:29 +00:00
|
|
|
)
|
2024-02-15 19:27:52 +00:00
|
|
|
.bind(day)
|
|
|
|
.bind(month)
|
|
|
|
.fetch_all(&mut db)
|
2024-02-14 12:44:29 +00:00
|
|
|
.await?;
|
2024-02-14 12:22:29 +00:00
|
|
|
|
2024-02-16 20:54:07 +00:00
|
|
|
let guilds = http.get_guilds(None, None).await?;
|
2024-02-15 19:27:52 +00:00
|
|
|
|
|
|
|
for guild in guilds {
|
|
|
|
let guild_id = guild.id;
|
|
|
|
|
|
|
|
for bd in &birtdays {
|
|
|
|
let user_id = UserId::new(bd.id);
|
2024-02-16 20:54:07 +00:00
|
|
|
guild_id.member(http.clone(), user_id).await?;
|
2024-02-15 19:27:52 +00:00
|
|
|
|
2024-02-16 20:54:07 +00:00
|
|
|
celebrate_birthday(guild_id, user_id, bd.nick.as_str(), http.clone()).await?;
|
2024-02-15 19:27:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-16 20:54:07 +00:00
|
|
|
// TODO if has year delete it from announce and delete
|
|
|
|
|
2024-02-15 19:27:52 +00:00
|
|
|
for e in &global_events {
|
2024-02-16 20:54:07 +00:00
|
|
|
if e.year != 0 && e.year != year {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
announce_event(guild_id, e.name.as_str(), e.special_message.as_str(), http.clone()).await?;
|
2024-02-15 19:27:52 +00:00
|
|
|
}
|
2024-02-14 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 19:27:52 +00:00
|
|
|
let global_events = sqlx::query_as::<_, EventRow>(
|
2024-02-16 20:54:07 +00:00
|
|
|
"SELECT id, guild, name, year, special_message from events
|
2024-02-15 19:27:52 +00:00
|
|
|
WHERE day=? AND month=? AND guild!=0;"
|
|
|
|
)
|
|
|
|
.bind(day)
|
|
|
|
.bind(month)
|
|
|
|
.fetch_all(&mut db)
|
|
|
|
.await?;
|
2024-02-14 12:44:29 +00:00
|
|
|
|
2024-02-16 20:54:07 +00:00
|
|
|
// TODO if has year delete it from announce and delete
|
|
|
|
|
2024-02-15 19:27:52 +00:00
|
|
|
for e in &global_events {
|
2024-02-16 20:54:07 +00:00
|
|
|
announce_event(GuildId::new(e.guild), e.name.as_str(), e.special_message.as_str(), http.clone()).await?;
|
2024-02-15 19:27:52 +00:00
|
|
|
}
|
2024-02-14 12:22:29 +00:00
|
|
|
|
|
|
|
Ok(())
|
2024-02-14 12:44:29 +00:00
|
|
|
}
|
|
|
|
|