2023-08-26 15:26:39 +00:00
|
|
|
use std::sync::Arc;
|
2023-08-27 14:39:09 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::time::Duration;
|
2023-08-25 22:36:46 +00:00
|
|
|
|
|
|
|
use serenity::async_trait;
|
2023-08-27 14:58:03 +00:00
|
|
|
use serenity::model::gateway::{GatewayIntents, Ready};
|
|
|
|
use serenity::model::guild::Member;
|
2023-08-26 14:55:07 +00:00
|
|
|
use serenity::model::id::GuildId;
|
2023-08-25 22:36:46 +00:00
|
|
|
use serenity::prelude::*;
|
2023-08-26 14:55:07 +00:00
|
|
|
use serenity::model::prelude::RoleId;
|
2023-08-25 22:36:46 +00:00
|
|
|
|
2023-08-27 14:39:09 +00:00
|
|
|
struct Handler {
|
|
|
|
is_loop_running: AtomicBool,
|
|
|
|
}
|
|
|
|
|
2023-08-25 22:36:46 +00:00
|
|
|
#[async_trait]
|
|
|
|
impl EventHandler for Handler {
|
2023-08-27 14:58:03 +00:00
|
|
|
async fn guild_member_addition(&self, ctx: Context, mut new_member: Member) {
|
2023-08-27 15:51:55 +00:00
|
|
|
let config_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
2023-08-27 14:58:03 +00:00
|
|
|
};
|
2023-08-27 15:51:55 +00:00
|
|
|
let config = config_lock.read().await;
|
2023-08-26 14:55:07 +00:00
|
|
|
|
2023-08-27 14:58:03 +00:00
|
|
|
let members_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<Members>().expect("Expected Members in TypeMap.").clone()
|
|
|
|
};
|
|
|
|
let members = members_lock.read().await;
|
2023-08-27 15:51:55 +00:00
|
|
|
|
2023-08-27 14:58:03 +00:00
|
|
|
if members.contains(&new_member.user.name) {
|
|
|
|
let mut roles = vec![];
|
|
|
|
|
|
|
|
if !new_member.roles.contains(&config.member_role_past){
|
|
|
|
roles.push(config.member_role_past);
|
|
|
|
}
|
|
|
|
if !new_member.roles.contains(&config.member_role_current){
|
|
|
|
roles.push(config.member_role_current);
|
|
|
|
}
|
2023-08-25 22:36:46 +00:00
|
|
|
|
2023-08-27 14:58:03 +00:00
|
|
|
if let Err(e) = new_member.add_roles(&ctx, &roles).await {
|
|
|
|
println!("{:?}", e);
|
|
|
|
}
|
|
|
|
}
|
2023-08-25 22:36:46 +00:00
|
|
|
}
|
|
|
|
|
2023-08-26 14:55:07 +00:00
|
|
|
async fn ready(&self, ctx: Context, ready: Ready) {
|
2023-08-27 14:39:09 +00:00
|
|
|
let ctx = Arc::new(ctx);
|
2023-08-25 22:36:46 +00:00
|
|
|
println!("{} is connected!", ready.user.name);
|
2023-08-26 14:55:07 +00:00
|
|
|
|
2023-08-27 15:51:55 +00:00
|
|
|
let config_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
2023-08-26 14:55:07 +00:00
|
|
|
};
|
2023-08-27 15:51:55 +00:00
|
|
|
let config = config_lock.read().await;
|
|
|
|
let timing_update = config.timing_update;
|
|
|
|
let timing_fetch = config.timing_fetch;
|
2023-08-27 14:39:09 +00:00
|
|
|
|
|
|
|
if !self.is_loop_running.load(Ordering::Relaxed) {
|
|
|
|
// We have to clone the Arc, as it gets moved into the new thread.
|
|
|
|
let ctx1 = Arc::clone(&ctx);
|
|
|
|
// tokio::spawn creates a new green thread that can run in parallel with the rest of
|
|
|
|
// the application.
|
|
|
|
tokio::spawn(async move {
|
|
|
|
loop {
|
|
|
|
// We clone Context again here, because Arc is owned, so it moves to the
|
|
|
|
// new function.
|
2023-08-27 15:51:55 +00:00
|
|
|
bulk_check(Arc::clone(&ctx1)).await;
|
|
|
|
tokio::time::sleep(Duration::from_secs(timing_update)).await;
|
2023-08-27 14:39:09 +00:00
|
|
|
}
|
|
|
|
});
|
2023-08-27 15:51:55 +00:00
|
|
|
|
2023-08-27 14:39:09 +00:00
|
|
|
let ctx2 = Arc::clone(&ctx);
|
|
|
|
tokio::spawn(async move {
|
|
|
|
loop {
|
2023-08-27 15:51:55 +00:00
|
|
|
fetch_accounts(Arc::clone(&ctx2)).await;
|
|
|
|
tokio::time::sleep(Duration::from_secs(timing_fetch)).await;
|
2023-08-27 14:39:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Now that the loop is running, we set the bool to true
|
|
|
|
self.is_loop_running.swap(true, Ordering::Relaxed);
|
|
|
|
}
|
2023-08-26 14:55:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Config {
|
|
|
|
server: GuildId,
|
|
|
|
member_role_current: RoleId,
|
|
|
|
member_role_past: RoleId,
|
2023-08-27 15:51:55 +00:00
|
|
|
auth: String,
|
|
|
|
timing_update: u64,
|
|
|
|
timing_fetch: u64,
|
|
|
|
}
|
|
|
|
impl TypeMapKey for Config {
|
|
|
|
type Value = Arc<RwLock<Config>>;
|
2023-08-26 14:55:07 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 14:39:09 +00:00
|
|
|
#[derive(Default, Debug)]
|
|
|
|
struct MembersCount {
|
2023-08-26 15:26:39 +00:00
|
|
|
members: i32,
|
|
|
|
members_current: i32,
|
|
|
|
}
|
|
|
|
struct MemberCounter;
|
|
|
|
impl TypeMapKey for MemberCounter {
|
2023-08-27 14:39:09 +00:00
|
|
|
type Value = Arc<RwLock<MembersCount>>;
|
2023-08-26 15:26:39 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 14:39:09 +00:00
|
|
|
struct Members;
|
|
|
|
impl TypeMapKey for Members {
|
|
|
|
type Value = Arc<RwLock<Vec<String>>>;
|
|
|
|
}
|
2023-08-27 15:51:55 +00:00
|
|
|
async fn bulk_check(ctx: Arc<Context>){
|
|
|
|
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;
|
|
|
|
|
2023-08-27 14:39:09 +00:00
|
|
|
let members_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<Members>().expect("Expected Members in TypeMap.").clone()
|
|
|
|
};
|
2023-08-27 15:51:55 +00:00
|
|
|
let members = members_lock.read().await;
|
|
|
|
|
2023-08-26 14:55:07 +00:00
|
|
|
let mut roles_set = [0,0,0];
|
2023-08-27 14:39:09 +00:00
|
|
|
let mut res = MembersCount {
|
2023-08-26 15:26:39 +00:00
|
|
|
members: 0,
|
|
|
|
members_current: 0,
|
|
|
|
};
|
2023-08-27 14:39:09 +00:00
|
|
|
if let Ok(x) = config.server.members(&ctx, None, None).await {
|
2023-08-26 14:55:07 +00:00
|
|
|
for mut member in x {
|
2023-08-27 15:51:55 +00:00
|
|
|
if members.contains(&member.user.name) {
|
2023-08-26 14:55:07 +00:00
|
|
|
let mut roles = vec![];
|
|
|
|
|
|
|
|
if !member.roles.contains(&config.member_role_past){
|
|
|
|
roles_set[0] += 1;
|
|
|
|
roles.push(config.member_role_past);
|
|
|
|
}
|
|
|
|
if !member.roles.contains(&config.member_role_current){
|
|
|
|
roles_set[1] += 1;
|
|
|
|
roles.push(config.member_role_current);
|
|
|
|
}
|
2023-08-27 14:39:09 +00:00
|
|
|
|
2023-08-26 14:55:07 +00:00
|
|
|
if let Err(e) = member.add_roles(&ctx, &roles).await {
|
|
|
|
println!("{:?}", e);
|
|
|
|
}
|
|
|
|
} else if member.roles.contains(&config.member_role_current) {
|
|
|
|
roles_set[2] += 1;
|
|
|
|
// if theya re not a current member and have the role then remove it
|
|
|
|
if let Err(e) = member.remove_role(&ctx, &config.member_role_current).await {
|
|
|
|
println!("{:?}", e);
|
|
|
|
}
|
|
|
|
}
|
2023-08-27 14:39:09 +00:00
|
|
|
|
2023-08-26 15:26:39 +00:00
|
|
|
if member.roles.contains(&config.member_role_past){
|
|
|
|
res.members += 1;
|
|
|
|
}
|
|
|
|
if member.roles.contains(&config.member_role_current){
|
|
|
|
res.members_current += 1;
|
|
|
|
}
|
2023-08-26 14:55:07 +00:00
|
|
|
}
|
2023-08-25 22:36:46 +00:00
|
|
|
}
|
2023-08-26 14:55:07 +00:00
|
|
|
// small bit of logging to note changes over time
|
|
|
|
println!("Changes: New: +{}, Current: +{}/-{}", roles_set[0], roles_set[1], roles_set[2]);
|
2023-08-26 15:26:39 +00:00
|
|
|
|
|
|
|
{
|
2023-08-27 15:51:55 +00:00
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
let counter_lock = data_read.get::<MemberCounter>().expect("Expected MemberCounter in TypeMap.").clone();
|
2023-08-26 15:26:39 +00:00
|
|
|
// The HashMap of CommandCounter is wrapped in an RwLock; since we want to write to it, we will
|
|
|
|
// open the lock in write mode.
|
|
|
|
let mut counter = counter_lock.write().await;
|
|
|
|
|
|
|
|
// And we write the amount of times the command has been called to it.
|
|
|
|
counter.members_current = res.members_current;
|
|
|
|
counter.members = res.members;
|
2023-08-27 15:51:55 +00:00
|
|
|
};
|
2023-08-25 22:36:46 +00:00
|
|
|
}
|
|
|
|
|
2023-08-27 14:39:09 +00:00
|
|
|
async fn fetch_accounts(ctx: Arc<Context>){
|
2023-08-27 15:51:55 +00:00
|
|
|
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;
|
|
|
|
let auth = &config.auth;
|
2023-08-27 14:39:09 +00:00
|
|
|
let url = format!("http://127.0.0.1:8087/ldap/discord?auth={}", auth);
|
|
|
|
if let Ok(result) = surf::get(url).recv_json::<Vec<String>>().await {
|
|
|
|
let members_lock = {
|
|
|
|
let data_read = ctx.data.read().await;
|
|
|
|
data_read.get::<Members>().expect("Expected Members in TypeMap.").clone()
|
|
|
|
};
|
|
|
|
let mut accounts = members_lock.write().await;
|
|
|
|
*accounts = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 22:36:46 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
// Configure the client with your Discord bot token in the environment.
|
|
|
|
//let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment");
|
|
|
|
let token = String::from("");
|
|
|
|
|
|
|
|
// 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(token, intents)
|
2023-08-27 14:39:09 +00:00
|
|
|
.event_handler(Handler { is_loop_running: AtomicBool::new(false)})
|
2023-08-25 22:36:46 +00:00
|
|
|
.await
|
|
|
|
.expect("Error creating client");
|
2023-08-26 15:26:39 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut data = client.data.write().await;
|
2023-08-27 14:39:09 +00:00
|
|
|
|
|
|
|
// will keep track of how many past and current members we have
|
|
|
|
data.insert::<MemberCounter>(Arc::new(RwLock::new(MembersCount::default())));
|
|
|
|
|
|
|
|
// a list of all current members
|
|
|
|
data.insert::<Members>(Arc::new(RwLock::new(vec![])));
|
2023-08-27 15:51:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
let config = Config {
|
|
|
|
server: GuildId(957961810147938334),
|
|
|
|
member_role_current: RoleId::from(1144760670995370094),
|
|
|
|
member_role_past: RoleId::from(1144760548072886353),
|
|
|
|
auth: String::from("abcdef"),
|
|
|
|
timing_update: 30,
|
|
|
|
timing_fetch: 50,
|
|
|
|
};
|
|
|
|
data.insert::<Config>(Arc::new(RwLock::new(config)));
|
2023-08-26 15:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-25 22:36:46 +00:00
|
|
|
// Finally, start a single shard, and start listening to events.
|
|
|
|
//
|
|
|
|
// Shards will automatically attempt to reconnect, and will perform
|
|
|
|
// exponential backoff until it reconnects.
|
|
|
|
if let Err(why) = client.start().await {
|
|
|
|
println!("Client error: {:?}", why);
|
|
|
|
}
|
|
|
|
}
|