feat: now properly sets and removes roles for committee members

This commit is contained in:
silver 2025-02-18 21:14:05 +00:00
parent 4eeb7f2135
commit 1aef86ad01
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
9 changed files with 137 additions and 89 deletions

View file

@ -137,6 +137,8 @@ pub mod committee {
use serenity::client::Context;
use serenity::model::channel::ChannelType;
use serenity::model::guild::Member;
use serenity::model::id::ChannelId;
use serenity::model::prelude::RoleId;
use sqlx::{Pool, Sqlite};
use std::collections::HashMap;
use std::sync::Arc;
@ -170,6 +172,7 @@ pub mod committee {
let server = config.committee_server;
let committee_member = config.committee_role;
let committees = get_committees(db).await;
let categories = vec![config.committee_category, ChannelId(1341457244973305927), ChannelId(1341457509717639279)];
// information about the server
let roles = server.roles(&ctx).await.unwrap_or_default();
@ -185,8 +188,10 @@ pub mod committee {
for channel in channels.values() {
// we only care about teh channels in teh category
if let Some(x) = channel.parent_id {
if x.eq(&config.committee_category) {
channels_name.insert(channel.name.to_owned(), channel.to_owned());
for category in &categories {
if x.eq(category) {
channels_name.insert(channel.name.to_owned(), channel.to_owned());
}
}
}
}
@ -197,13 +202,21 @@ pub mod committee {
// a list of all the roles that can be removed from folks who should have them
let mut committee_roles = vec![committee_member];
for committee in &committees {
let mut category_index = 0;
let mut i = 0;
loop {
if i >= committees.len() {
break;
}
let committee = &committees[i];
// get the role for this committee/club/soc
let role = match roles_name.get(&committee.name_profile) {
let role = match roles_name.get(&committee.name_full) {
Some(x) => Some(x.to_owned()),
None => {
// create teh role if it does not exist
match server.create_role(&ctx, |r| r.hoist(false).mentionable(true).name(&committee.name_profile)).await {
match server.create_role(&ctx, |r| r.hoist(false).mentionable(true).name(&committee.name_full)).await {
Ok(x) => Some(x),
Err(_) => None,
}
@ -213,7 +226,7 @@ pub mod committee {
// create teh channel if it does nto exist
if !channels_name.contains_key(&committee.name_profile) {
match server
.create_channel(&ctx, |c| c.name(&committee.name_profile).kind(ChannelType::Text).category(config.committee_category))
.create_channel(&ctx, |c| c.name(&committee.name_profile).kind(ChannelType::Text).category(categories[category_index]))
.await
{
Ok(x) => {
@ -223,7 +236,14 @@ pub mod committee {
println!("Created channel: {}", &committee.name_profile);
}
Err(x) => {
dbg!("Unable to create channel: ", x);
let tmp = x.to_string();
if x.to_string().contains("Maximum number of channels in category reached (50)") {
category_index += 1;
continue;
}
dbg!("Unable to create channel: ", &tmp, &tmp.contains("Maximum number of channels in category reached (50)"));
}
}
};
@ -236,42 +256,60 @@ pub mod committee {
// ID in this is the wolves ID, so we need to get a matching discord ID (if one exists)
if let Some(x) = get_server_member_discord(db, id_wolves).await {
if let Some(member_tmp) = x.discord {
let values = users_roles.entry(member_tmp).or_insert(vec![]);
let values = users_roles.entry(member_tmp).or_insert(vec![committee_member]);
values.push(r.id);
}
}
}
}
i += 1;
}
// now we have a map of all users that should get roles time to go through all the folks on teh server
for member in members {
let roles_current = member.roles(ctx).unwrap_or_default();
let roles_required = match users_roles.get(&member.user.id) {
None => {
vec![]
}
Some(x) => {
let mut combined = x.to_owned();
// this is the main role, since it provides access to everything.
combined.push(committee_member);
combined
let mut tmp = x.to_owned();
if !tmp.is_empty() {
tmp.push(RoleId(1226602779968274573));
}
tmp
}
};
// get a list of all the roles to remove from someone
let mut roles_rem = vec![];
for role in &committee_roles {
if !roles_required.contains(role) {
roles_rem.push(role.to_owned());
let mut roles_add = vec![];
// get a list of all the roles to remove from someone
let mut roles_current_id = vec![];
for role in &roles_current {
roles_current_id.push(role.id.to_owned());
if !roles_required.contains(&role.id) {
roles_rem.push(role.id.to_owned());
}
}
for role in &roles_required {
if !roles_current_id.contains(role) {
roles_add.push(role.to_owned());
}
}
if !roles_rem.is_empty() {
member.remove_roles(&ctx, &roles_rem).await.unwrap_or_default();
}
if !roles_required.is_empty() {
if !roles_add.is_empty() {
// these roles are flavor roles, only there to make folks mentionable
member.add_roles(&ctx, &roles_required).await.unwrap_or_default();
member.add_roles(&ctx, &roles_add).await.unwrap_or_default();
} else {
member.remove_roles(&ctx, &[RoleId(1226602779968274573)]).await.unwrap_or_default();
}
}
@ -303,7 +341,7 @@ pub mod committee {
}
async fn get_committees(db: &Pool<Sqlite>) -> Vec<Committees> {
sqlx::query_as::<_, Committees>(
match sqlx::query_as::<_, Committees>(
r#"
SELECT *
FROM committees
@ -311,7 +349,13 @@ pub mod committee {
)
.fetch_all(db)
.await
.unwrap_or_default()
{
Ok(a) => a,
Err(e) => {
dbg!(e);
vec![]
}
}
}
async fn get_server_member_discord(db: &Pool<Sqlite>, user: &i64) -> Option<Wolves> {