feat: now have a member count across all threads
This commit is contained in:
parent
89e81b575c
commit
d66f7895bc
1 changed files with 43 additions and 1 deletions
42
src/main.rs
42
src/main.rs
|
@ -1,5 +1,6 @@
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use serenity::async_trait;
|
use serenity::async_trait;
|
||||||
use serenity::model::channel::Message;
|
use serenity::model::channel::Message;
|
||||||
|
@ -46,8 +47,22 @@ struct Config {
|
||||||
member_role_past: RoleId,
|
member_role_past: RoleId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct Members {
|
||||||
|
members: i32,
|
||||||
|
members_current: i32,
|
||||||
|
}
|
||||||
|
struct MemberCounter;
|
||||||
|
impl TypeMapKey for MemberCounter {
|
||||||
|
type Value = Arc<RwLock<Members>>;
|
||||||
|
}
|
||||||
|
|
||||||
async fn bulk_check(ctx: &Context, config: &Config){
|
async fn bulk_check(ctx: &Context, config: &Config){
|
||||||
let mut roles_set = [0,0,0];
|
let mut roles_set = [0,0,0];
|
||||||
|
let mut res = Members {
|
||||||
|
members: 0,
|
||||||
|
members_current: 0,
|
||||||
|
};
|
||||||
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 config.members.contains(&member.user.name) {
|
if config.members.contains(&member.user.name) {
|
||||||
|
@ -72,10 +87,31 @@ async fn bulk_check(ctx: &Context, config: &Config){
|
||||||
println!("{:?}", e);
|
println!("{:?}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if member.roles.contains(&config.member_role_past){
|
||||||
|
res.members += 1;
|
||||||
|
}
|
||||||
|
if member.roles.contains(&config.member_role_current){
|
||||||
|
res.members_current += 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 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()
|
||||||
|
};
|
||||||
|
{
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -92,6 +128,12 @@ async fn main() {
|
||||||
.await
|
.await
|
||||||
.expect("Error creating client");
|
.expect("Error creating client");
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut data = client.data.write().await;
|
||||||
|
data.insert::<MemberCounter>(Arc::new(RwLock::new(Members::default())));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Finally, start a single shard, and start listening to events.
|
// Finally, start a single shard, and start listening to events.
|
||||||
//
|
//
|
||||||
// Shards will automatically attempt to reconnect, and will perform
|
// Shards will automatically attempt to reconnect, and will perform
|
||||||
|
|
Loading…
Reference in a new issue