forked from Skynet/discord-bot
feat: moved the update server icon to the main thread
This commit is contained in:
parent
227db8a741
commit
feff293043
5 changed files with 40 additions and 86 deletions
40
src/main.rs
40
src/main.rs
|
@ -1,9 +1,11 @@
|
|||
pub mod commands;
|
||||
|
||||
use crate::commands::role_adder::tools::on_role_change;
|
||||
use crate::commands::server_icon::user::current::icon::get_current_icon;
|
||||
use chrono::{Days, SecondsFormat, TimeDelta, Utc};
|
||||
use serenity::all::{
|
||||
ActivityData, Command, CommandDataOptionValue, CreateMessage, EditInteractionResponse, GuildId, GuildMemberUpdateEvent, GuildMembersChunkEvent,
|
||||
Interaction,
|
||||
ActivityData, Command, CommandDataOptionValue, CreateAttachment, CreateMessage, EditInteractionResponse, GuildId, GuildMemberUpdateEvent,
|
||||
GuildMembersChunkEvent, Interaction,
|
||||
};
|
||||
use serenity::{
|
||||
async_trait,
|
||||
|
@ -17,6 +19,7 @@ use serenity::{
|
|||
Client,
|
||||
};
|
||||
use skynet_discord_bot::common::database::{db_init, get_server_config, get_server_config_bulk, get_server_member, DataBase};
|
||||
use skynet_discord_bot::common::server_icon::{get_config_icons, update_icon};
|
||||
use skynet_discord_bot::common::set_roles::committee::update_committees;
|
||||
use skynet_discord_bot::common::set_roles::{committee, normal};
|
||||
use skynet_discord_bot::common::wolves::cns::get_wolves;
|
||||
|
@ -103,6 +106,39 @@ impl EventHandler for Handler {
|
|||
});
|
||||
}
|
||||
|
||||
{
|
||||
// this updates teh server icon once a day
|
||||
let ctx_task = Arc::clone(&ctx);
|
||||
tokio::spawn(async move {
|
||||
let db_lock = {
|
||||
let data_read = ctx_task.data.read().await;
|
||||
data_read.get::<DataBase>().expect("Expected Database in TypeMap.").clone()
|
||||
};
|
||||
let db = db_lock.read().await;
|
||||
let config_lock = {
|
||||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
||||
};
|
||||
|
||||
let config_global = config_lock.read().await;
|
||||
let config_toml = get_config_icons::minimal();
|
||||
|
||||
loop {
|
||||
println!("Update - Logo - Start");
|
||||
// even though this task will run every 5 min it will only actually change the icon when its a day old
|
||||
if let Some(logo) = get_current_icon(&db).await {
|
||||
let now = Utc::now();
|
||||
let yesterday = now.checked_sub_days(Days::new(1)).unwrap_or_default();
|
||||
if logo.date < yesterday.to_rfc3339_opts(SecondsFormat::Millis, true) {
|
||||
update_icon::update_icon_main(&ctx, &db, &config_global, &config_toml).await;
|
||||
}
|
||||
}
|
||||
println!("Update - Logo - End");
|
||||
tokio::time::sleep(Duration::from_secs(60 * 5)).await;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Now that the loop is running, we set the bool to true
|
||||
self.is_loop_running.swap(true, Ordering::Relaxed);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue