2025-07-20 23:40:34 +01:00
|
|
|
use serenity::{
|
2025-07-21 00:38:59 +01:00
|
|
|
all::{ChunkGuildFilter, GuildId, GuildMembersChunkEvent},
|
2025-07-20 23:40:34 +01:00
|
|
|
async_trait,
|
|
|
|
client::{Context, EventHandler},
|
2025-07-21 00:38:59 +01:00
|
|
|
model::gateway::GatewayIntents,
|
2025-07-20 23:40:34 +01:00
|
|
|
Client,
|
|
|
|
};
|
2025-07-21 00:38:59 +01:00
|
|
|
use skynet_discord_bot::{
|
|
|
|
common::{
|
|
|
|
database::{db_init, get_server_config_bulk, DataBase},
|
|
|
|
set_roles::normal,
|
|
|
|
},
|
|
|
|
get_config, Config,
|
|
|
|
};
|
2025-07-21 04:29:03 +01:00
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
2025-07-20 23:40:34 +01:00
|
|
|
use std::{process, sync::Arc};
|
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let config = get_config();
|
|
|
|
let db = match db_init(&config).await {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(_) => return,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Intents are a bitflag, bitwise operations can be used to dictate which intents to use
|
|
|
|
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)
|
2025-07-21 04:29:03 +01:00
|
|
|
.event_handler(Handler {
|
|
|
|
server_count: Default::default(),
|
|
|
|
server_cached: Default::default(),
|
|
|
|
})
|
2025-07-20 23:40:34 +01:00
|
|
|
.cache_settings(serenity::cache::Settings::default())
|
|
|
|
.await
|
|
|
|
.expect("Error creating client");
|
|
|
|
|
|
|
|
{
|
|
|
|
let mut data = client.data.write().await;
|
|
|
|
|
|
|
|
data.insert::<Config>(Arc::new(RwLock::new(config)));
|
|
|
|
data.insert::<DataBase>(Arc::new(RwLock::new(db)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Err(why) = client.start().await {
|
2025-07-21 00:50:20 +01:00
|
|
|
println!("Client error: {why:?}");
|
2025-07-20 23:40:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-21 04:29:03 +01:00
|
|
|
struct Handler {
|
|
|
|
server_count: AtomicUsize,
|
|
|
|
server_cached: AtomicUsize,
|
|
|
|
}
|
2025-07-20 23:40:34 +01:00
|
|
|
#[async_trait]
|
|
|
|
impl EventHandler for Handler {
|
|
|
|
async fn cache_ready(&self, ctx: Context, guilds: Vec<GuildId>) {
|
2025-07-21 04:29:03 +01:00
|
|
|
self.server_count.swap(guilds.len(), Ordering::SeqCst);
|
2025-07-20 23:40:34 +01:00
|
|
|
for guild in guilds {
|
|
|
|
ctx.shard.chunk_guild(guild, Some(2000), false, ChunkGuildFilter::None, None);
|
|
|
|
}
|
2025-07-21 04:29:03 +01:00
|
|
|
println!("Cache loaded {}", &self.server_count.load(Ordering::SeqCst));
|
2025-07-20 23:40:34 +01:00
|
|
|
}
|
|
|
|
|
2025-07-20 23:46:30 +01:00
|
|
|
async fn guild_members_chunk(&self, ctx: Context, chunk: GuildMembersChunkEvent) {
|
|
|
|
if (chunk.chunk_index + 1) == chunk.chunk_count {
|
2025-07-21 04:29:03 +01:00
|
|
|
self.server_cached.fetch_add(1, Ordering::SeqCst);
|
|
|
|
if (self.server_cached.load(Ordering::SeqCst) + 1) == self.server_count.load(Ordering::SeqCst) {
|
|
|
|
println!("Cache built successfully!");
|
|
|
|
|
|
|
|
// this goes into each server and sets roles for each wolves member
|
|
|
|
check_bulk(&ctx).await;
|
2025-07-20 23:40:34 +01:00
|
|
|
|
2025-07-21 04:29:03 +01:00
|
|
|
// finish up
|
|
|
|
process::exit(0);
|
|
|
|
}
|
2025-07-20 23:46:30 +01:00
|
|
|
}
|
2025-07-20 23:40:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-07-20 23:46:30 +01:00
|
|
|
async fn check_bulk(ctx: &Context) {
|
2025-07-20 23:40:34 +01:00
|
|
|
let db_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<DataBase>().expect("Expected Config in TypeMap.").clone()
|
|
|
|
};
|
|
|
|
|
|
|
|
let db = db_lock.read().await;
|
|
|
|
|
|
|
|
for server_config in get_server_config_bulk(&db).await {
|
2025-07-21 00:38:59 +01:00
|
|
|
normal::update_server(ctx, &server_config, &[], &[]).await;
|
2025-07-20 23:40:34 +01:00
|
|
|
}
|
|
|
|
}
|