feat: config is now shared
This commit is contained in:
parent
58def2e01e
commit
1694c4eebf
1 changed files with 52 additions and 58 deletions
108
src/main.rs
108
src/main.rs
|
@ -16,18 +16,18 @@ struct Handler {
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl EventHandler for Handler {
|
impl EventHandler for Handler {
|
||||||
async fn guild_member_addition(&self, ctx: Context, mut new_member: Member) {
|
async fn guild_member_addition(&self, ctx: Context, mut new_member: Member) {
|
||||||
let config = Config {
|
let config_lock = {
|
||||||
server: GuildId(957961810147938334),
|
let data_read = ctx.data.read().await;
|
||||||
member_role_current: RoleId::from(1144760670995370094),
|
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
||||||
member_role_past: RoleId::from(1144760548072886353),
|
|
||||||
};
|
};
|
||||||
|
let config = config_lock.read().await;
|
||||||
|
|
||||||
let members_lock = {
|
let members_lock = {
|
||||||
let data_read = ctx.data.read().await;
|
let data_read = ctx.data.read().await;
|
||||||
data_read.get::<Members>().expect("Expected Members in TypeMap.").clone()
|
data_read.get::<Members>().expect("Expected Members in TypeMap.").clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
let members = members_lock.read().await;
|
let members = members_lock.read().await;
|
||||||
|
|
||||||
if members.contains(&new_member.user.name) {
|
if members.contains(&new_member.user.name) {
|
||||||
let mut roles = vec![];
|
let mut roles = vec![];
|
||||||
|
|
||||||
|
@ -48,11 +48,13 @@ impl EventHandler for Handler {
|
||||||
let ctx = Arc::new(ctx);
|
let ctx = Arc::new(ctx);
|
||||||
println!("{} is connected!", ready.user.name);
|
println!("{} is connected!", ready.user.name);
|
||||||
|
|
||||||
let config = Config {
|
let config_lock = {
|
||||||
server: GuildId(957961810147938334),
|
let data_read = ctx.data.read().await;
|
||||||
member_role_current: RoleId::from(1144760670995370094),
|
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
||||||
member_role_past: RoleId::from(1144760548072886353),
|
|
||||||
};
|
};
|
||||||
|
let config = config_lock.read().await;
|
||||||
|
let timing_update = config.timing_update;
|
||||||
|
let timing_fetch = config.timing_fetch;
|
||||||
|
|
||||||
if !self.is_loop_running.load(Ordering::Relaxed) {
|
if !self.is_loop_running.load(Ordering::Relaxed) {
|
||||||
// We have to clone the Arc, as it gets moved into the new thread.
|
// We have to clone the Arc, as it gets moved into the new thread.
|
||||||
|
@ -63,29 +65,19 @@ impl EventHandler for Handler {
|
||||||
loop {
|
loop {
|
||||||
// We clone Context again here, because Arc is owned, so it moves to the
|
// We clone Context again here, because Arc is owned, so it moves to the
|
||||||
// new function.
|
// new function.
|
||||||
bulk_check(Arc::clone(&ctx1), &config).await;
|
bulk_check(Arc::clone(&ctx1)).await;
|
||||||
tokio::time::sleep(Duration::from_secs(30)).await;
|
tokio::time::sleep(Duration::from_secs(timing_update)).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// And of course, we can run more than one thread at different timings.
|
|
||||||
let ctx2 = Arc::clone(&ctx);
|
let ctx2 = Arc::clone(&ctx);
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
bulk_check2(Arc::clone(&ctx2)).await;
|
fetch_accounts(Arc::clone(&ctx2)).await;
|
||||||
tokio::time::sleep(Duration::from_secs(15)).await;
|
tokio::time::sleep(Duration::from_secs(timing_fetch)).await;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let ctx3 = Arc::clone(&ctx);
|
|
||||||
tokio::spawn(async move {
|
|
||||||
loop {
|
|
||||||
fetch_accounts(Arc::clone(&ctx3)).await;
|
|
||||||
tokio::time::sleep(Duration::from_secs(50)).await;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
// fetch_accounts
|
|
||||||
|
|
||||||
// Now that the loop is running, we set the bool to true
|
// Now that the loop is running, we set the bool to true
|
||||||
self.is_loop_running.swap(true, Ordering::Relaxed);
|
self.is_loop_running.swap(true, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
@ -96,6 +88,12 @@ struct Config {
|
||||||
server: GuildId,
|
server: GuildId,
|
||||||
member_role_current: RoleId,
|
member_role_current: RoleId,
|
||||||
member_role_past: RoleId,
|
member_role_past: RoleId,
|
||||||
|
auth: String,
|
||||||
|
timing_update: u64,
|
||||||
|
timing_fetch: u64,
|
||||||
|
}
|
||||||
|
impl TypeMapKey for Config {
|
||||||
|
type Value = Arc<RwLock<Config>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug)]
|
#[derive(Default, Debug)]
|
||||||
|
@ -112,11 +110,19 @@ struct Members;
|
||||||
impl TypeMapKey for Members {
|
impl TypeMapKey for Members {
|
||||||
type Value = Arc<RwLock<Vec<String>>>;
|
type Value = Arc<RwLock<Vec<String>>>;
|
||||||
}
|
}
|
||||||
async fn bulk_check(ctx: Arc<Context>, config: &Config){
|
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;
|
||||||
|
|
||||||
let members_lock = {
|
let members_lock = {
|
||||||
let data_read = ctx.data.read().await;
|
let data_read = ctx.data.read().await;
|
||||||
data_read.get::<Members>().expect("Expected Members in TypeMap.").clone()
|
data_read.get::<Members>().expect("Expected Members in TypeMap.").clone()
|
||||||
};
|
};
|
||||||
|
let members = members_lock.read().await;
|
||||||
|
|
||||||
let mut roles_set = [0,0,0];
|
let mut roles_set = [0,0,0];
|
||||||
let mut res = MembersCount {
|
let mut res = MembersCount {
|
||||||
members: 0,
|
members: 0,
|
||||||
|
@ -124,7 +130,7 @@ async fn bulk_check(ctx: Arc<Context>, config: &Config){
|
||||||
};
|
};
|
||||||
if let Ok(x) = config.server.members(&ctx, None, None).await {
|
if let Ok(x) = config.server.members(&ctx, None, None).await {
|
||||||
for mut member in x {
|
for mut member in x {
|
||||||
if members_lock.read().await.contains(&member.user.name) {
|
if members.contains(&member.user.name) {
|
||||||
let mut roles = vec![];
|
let mut roles = vec![];
|
||||||
|
|
||||||
if !member.roles.contains(&config.member_role_past){
|
if !member.roles.contains(&config.member_role_past){
|
||||||
|
@ -158,11 +164,9 @@ async fn bulk_check(ctx: Arc<Context>, config: &Config){
|
||||||
// small bit of logging to note changes over time
|
// small bit of logging to note changes over time
|
||||||
println!("Changes: New: +{}, Current: +{}/-{}", roles_set[0], roles_set[1], roles_set[2]);
|
println!("Changes: New: +{}, Current: +{}/-{}", roles_set[0], roles_set[1], roles_set[2]);
|
||||||
|
|
||||||
let counter_lock = {
|
|
||||||
let data_read = ctx.data.read().await;
|
|
||||||
data_read.get::<MemberCounter>().expect("Expected MemberCounter in TypeMap.").clone()
|
|
||||||
};
|
|
||||||
{
|
{
|
||||||
|
let data_read = ctx.data.read().await;
|
||||||
|
let counter_lock = data_read.get::<MemberCounter>().expect("Expected MemberCounter in TypeMap.").clone();
|
||||||
// The HashMap of CommandCounter is wrapped in an RwLock; since we want to write to it, we will
|
// The HashMap of CommandCounter is wrapped in an RwLock; since we want to write to it, we will
|
||||||
// open the lock in write mode.
|
// open the lock in write mode.
|
||||||
let mut counter = counter_lock.write().await;
|
let mut counter = counter_lock.write().await;
|
||||||
|
@ -170,11 +174,16 @@ async fn bulk_check(ctx: Arc<Context>, config: &Config){
|
||||||
// And we write the amount of times the command has been called to it.
|
// And we write the amount of times the command has been called to it.
|
||||||
counter.members_current = res.members_current;
|
counter.members_current = res.members_current;
|
||||||
counter.members = res.members;
|
counter.members = res.members;
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn fetch_accounts(ctx: Arc<Context>){
|
async fn fetch_accounts(ctx: Arc<Context>){
|
||||||
let auth = "abcdef";
|
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;
|
||||||
let url = format!("http://127.0.0.1:8087/ldap/discord?auth={}", auth);
|
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 {
|
if let Ok(result) = surf::get(url).recv_json::<Vec<String>>().await {
|
||||||
let members_lock = {
|
let members_lock = {
|
||||||
|
@ -186,32 +195,6 @@ async fn fetch_accounts(ctx: Arc<Context>){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn bulk_check2(ctx: Arc<Context>){
|
|
||||||
let counter_lock = {
|
|
||||||
let data_read = ctx.data.read().await;
|
|
||||||
data_read.get::<MemberCounter>().expect("Expected MemberCounter in TypeMap.").clone()
|
|
||||||
};
|
|
||||||
{
|
|
||||||
// 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 counter = counter_lock.read().await;
|
|
||||||
println!("Members: {:?}", counter);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
let members_lock = {
|
|
||||||
let data_read = ctx.data.read().await;
|
|
||||||
data_read.get::<Members>().expect("Expected Members in TypeMap.").clone()
|
|
||||||
};
|
|
||||||
{
|
|
||||||
// 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 counter = members_lock.read().await;
|
|
||||||
println!("Members: {:?}", counter);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Configure the client with your Discord bot token in the environment.
|
// Configure the client with your Discord bot token in the environment.
|
||||||
|
@ -234,6 +217,17 @@ async fn main() {
|
||||||
|
|
||||||
// a list of all current members
|
// a list of all current members
|
||||||
data.insert::<Members>(Arc::new(RwLock::new(vec![])));
|
data.insert::<Members>(Arc::new(RwLock::new(vec![])));
|
||||||
|
|
||||||
|
|
||||||
|
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)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue