feat: added script to clean up the committee server if it got flooded with extra channels
This commit is contained in:
parent
b8ffd42184
commit
3abbb8d485
3 changed files with 135 additions and 3 deletions
129
src/bin/cleanup_committee.rs
Normal file
129
src/bin/cleanup_committee.rs
Normal file
|
@ -0,0 +1,129 @@
|
|||
use serenity::all::{ChunkGuildFilter, GuildId};
|
||||
use serenity::{
|
||||
async_trait,
|
||||
client::{Context, EventHandler},
|
||||
model::gateway::{GatewayIntents, Ready},
|
||||
Client,
|
||||
};
|
||||
use skynet_discord_bot::common::database::{db_init, DataBase};
|
||||
|
||||
use skynet_discord_bot::common::set_roles::committee::{db_roles_get};
|
||||
use skynet_discord_bot::{get_config, Config};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
use std::{process, sync::Arc};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
/// Cleanup teh Committee server
|
||||
///
|
||||
/// This removes any invalid roles/channels which ay have been set up accidentally
|
||||
/// DO NOT run this locally unless you have a fresh copy of teh live database handy.
|
||||
#[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)
|
||||
.event_handler(Handler {})
|
||||
.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 {
|
||||
println!("Client error: {:?}", why);
|
||||
}
|
||||
}
|
||||
|
||||
struct Handler;
|
||||
#[async_trait]
|
||||
impl EventHandler for Handler {
|
||||
async fn cache_ready(&self, ctx: Context, guilds: Vec<GuildId>) {
|
||||
for guild in guilds {
|
||||
ctx.shard.chunk_guild(guild, Some(500), false, ChunkGuildFilter::None, None);
|
||||
}
|
||||
println!("Cache built successfully!");
|
||||
}
|
||||
|
||||
async fn ready(&self, ctx: Context, ready: Ready) {
|
||||
let ctx = Arc::new(ctx);
|
||||
println!("{} is connected!", ready.user.name);
|
||||
|
||||
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;
|
||||
|
||||
let config_lock = {
|
||||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
||||
};
|
||||
let config = config_lock.read().await;
|
||||
|
||||
cleanup(&db, &ctx, &config).await;
|
||||
// finish up
|
||||
process::exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
async fn cleanup(db: &Pool<Sqlite>, ctx: &Context, config: &Config) {
|
||||
let server = config.committee_server;
|
||||
let committees = db_roles_get(db).await;
|
||||
|
||||
if let Ok(channels) = server.channels(ctx).await {
|
||||
for (id, channel) in &channels {
|
||||
let name = &channel.name;
|
||||
let committee_tmp = committees.iter().filter(|x| &x.name_channel == name).collect::<Vec<_>>();
|
||||
let committee = match committee_tmp.first() {
|
||||
// if there are no committees which match then this is not a channelw e care about
|
||||
None => {
|
||||
continue;
|
||||
}
|
||||
Some(x) => x,
|
||||
};
|
||||
|
||||
// if the id of the channel does not match then remove it
|
||||
if id != &committee.id_channel {
|
||||
println!("Deleting Channel - ID: {} Name: {}", id, &channel.name);
|
||||
if let Err(e) = channel.delete(ctx).await {
|
||||
dbg!(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(mut roles) = server.roles(ctx).await {
|
||||
for (id, role) in &mut roles {
|
||||
let name = &role.name;
|
||||
let committee_tmp = committees.iter().filter(|x| &x.name_role == name).collect::<Vec<_>>();
|
||||
let committee = match committee_tmp.first() {
|
||||
// if there are no committees which match then this is not a channelw e care about
|
||||
None => {
|
||||
continue;
|
||||
}
|
||||
Some(x) => x,
|
||||
};
|
||||
|
||||
// if the id of the role does not match then remove it
|
||||
if id != &committee.id_role {
|
||||
println!("Deleting Role - ID: {} Name: {}", id, &role.name);
|
||||
if let Err(e) = role.delete(ctx).await {
|
||||
dbg!(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue