feat: setup update users to be every 5 min while using the cache
This commit is contained in:
parent
2b2dfc2531
commit
04aa0e63d4
1 changed files with 47 additions and 4 deletions
51
src/main.rs
51
src/main.rs
|
@ -2,7 +2,8 @@ pub mod commands;
|
|||
|
||||
use crate::commands::role_adder::tools::on_role_change;
|
||||
use serenity::all::{
|
||||
ActivityData, Command, CommandDataOptionValue, CreateMessage, EditInteractionResponse, GuildId, GuildMemberUpdateEvent, Interaction,
|
||||
ActivityData, Command, CommandDataOptionValue, CreateMessage, EditInteractionResponse, GuildId, GuildMemberUpdateEvent, GuildMembersChunkEvent,
|
||||
Interaction,
|
||||
};
|
||||
use serenity::{
|
||||
async_trait,
|
||||
|
@ -15,15 +16,20 @@ use serenity::{
|
|||
},
|
||||
Client,
|
||||
};
|
||||
use skynet_discord_bot::common::database::{db_init, get_server_config, get_server_member, DataBase};
|
||||
use skynet_discord_bot::common::database::{db_init, get_server_config, get_server_config_bulk, get_server_member, DataBase};
|
||||
use skynet_discord_bot::common::set_roles::committee::update_committees;
|
||||
use skynet_discord_bot::common::set_roles::normal;
|
||||
use skynet_discord_bot::common::wolves::committees::Committees;
|
||||
use skynet_discord_bot::{get_config, Config};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
struct Handler;
|
||||
struct Handler {
|
||||
is_loop_running: AtomicBool,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
|
@ -35,6 +41,41 @@ impl EventHandler for Handler {
|
|||
println!("Cache built successfully!");
|
||||
}
|
||||
|
||||
async fn guild_members_chunk(&self, ctx: Context, chunk: GuildMembersChunkEvent) {
|
||||
if (chunk.chunk_index + 1) == chunk.chunk_count {
|
||||
// from https://github.com/serenity-rs/serenity/blob/18349f7bba43acad4261103eb38fe01d93f382df/examples/e13_parallel_loops/src/main.rs#L48
|
||||
let ctx = Arc::new(ctx);
|
||||
|
||||
if !self.is_loop_running.load(Ordering::Relaxed) {
|
||||
// We have to clone the Arc, as it gets moved into the new thread.
|
||||
|
||||
{
|
||||
// this is to update member roles every 5 min
|
||||
let ctx1 = Arc::clone(&ctx);
|
||||
tokio::spawn(async move {
|
||||
let db_lock = {
|
||||
let data_read = ctx1.data.read().await;
|
||||
data_read.get::<DataBase>().expect("Expected Database in TypeMap.").clone()
|
||||
};
|
||||
let db = db_lock.read().await;
|
||||
|
||||
loop {
|
||||
println!("User update - Start");
|
||||
for server_config in get_server_config_bulk(&db).await {
|
||||
normal::update_server(&ctx, &server_config, &[], &[]).await;
|
||||
}
|
||||
println!("User update - 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handles previously linked accounts joining the server
|
||||
async fn guild_member_addition(&self, ctx: Context, new_member: Member) {
|
||||
let db_lock = {
|
||||
|
@ -291,7 +332,9 @@ async fn main() {
|
|||
let intents = GatewayIntents::GUILDS | GatewayIntents::GUILD_MESSAGES | GatewayIntents::MESSAGE_CONTENT | GatewayIntents::GUILD_MEMBERS;
|
||||
// Build our client.
|
||||
let mut client = Client::builder(&config.discord_token, intents)
|
||||
.event_handler(Handler {})
|
||||
.event_handler(Handler {
|
||||
is_loop_running: AtomicBool::new(false),
|
||||
})
|
||||
.cache_settings(serenity::cache::Settings::default())
|
||||
.await
|
||||
.expect("Error creating client");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue