Compare commits

...

3 commits

2 changed files with 64 additions and 1 deletions

View file

@ -135,6 +135,7 @@ pub mod committee {
use crate::common::wolves::committees::Committees; use crate::common::wolves::committees::Committees;
use crate::Config; use crate::Config;
use serenity::client::Context; use serenity::client::Context;
use serenity::model::channel::ChannelType;
use serenity::model::guild::Member; use serenity::model::guild::Member;
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
use std::collections::HashMap; use std::collections::HashMap;
@ -172,6 +173,7 @@ pub mod committee {
// information about the server // information about the server
let roles = server.roles(&ctx).await.unwrap_or_default(); let roles = server.roles(&ctx).await.unwrap_or_default();
let channels = server.channels(&ctx).await.unwrap_or_default();
// make a hashmap of the nameof roles to quickly get them out again // make a hashmap of the nameof roles to quickly get them out again
let mut roles_name = HashMap::new(); let mut roles_name = HashMap::new();
@ -179,6 +181,16 @@ pub mod committee {
roles_name.insert(role.name.to_owned(), role.to_owned()); roles_name.insert(role.name.to_owned(), role.to_owned());
} }
let mut channels_name = HashMap::new();
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());
}
}
}
// a map of users and the roles they are goign to be getting // a map of users and the roles they are goign to be getting
let mut users_roles = HashMap::new(); let mut users_roles = HashMap::new();
@ -198,6 +210,24 @@ pub mod committee {
} }
}; };
// create teh channel if it does nto exist
if !channels_name.contains_key(&committee.name) {
match server
.create_channel(&ctx, |c| c.name(&committee.name).kind(ChannelType::Text).category(config.committee_category))
.await
{
Ok(x) => {
// update teh channels name list
channels_name.insert(x.name.to_owned(), x.to_owned());
println!("Created channel: {}", &committee.name);
}
Err(x) => {
dbg!("Unable to create channel: ", x);
}
}
};
// so if the role exists // so if the role exists
if let Some(r) = role { if let Some(r) = role {
committee_roles.push(r.id); committee_roles.push(r.id);
@ -244,6 +274,32 @@ pub mod committee {
member.add_roles(&ctx, &roles_required).await.unwrap_or_default(); member.add_roles(&ctx, &roles_required).await.unwrap_or_default();
} }
} }
// finally re-order teh channels to make them visually apealing
let mut channel_names = channels_name.clone().into_keys().collect::<Vec<String>>();
channel_names.sort();
// get a list of all teh new positions
let mut new_positions = vec![];
for (i, name) in channel_names.iter().enumerate() {
if let Some(channel) = channels_name.get_mut(name) {
let position_new = i as u64;
if position_new != channel.position as u64 {
new_positions.push((channel.id.to_owned(), position_new));
}
}
}
if !new_positions.is_empty() {
match server.reorder_channels(&ctx, new_positions).await {
Ok(_) => {
println!("Successfully re-orderd the committee category");
}
Err(e) => {
dbg!("Failed to re-order ", e);
}
}
}
} }
async fn get_committees(db: &Pool<Sqlite>) -> Vec<Committees> { async fn get_committees(db: &Pool<Sqlite>) -> Vec<Committees> {

View file

@ -4,7 +4,7 @@ use chrono::{Datelike, SecondsFormat, Utc};
use dotenvy::dotenv; use dotenvy::dotenv;
use rand::{distributions::Alphanumeric, thread_rng, Rng}; use rand::{distributions::Alphanumeric, thread_rng, Rng};
use serenity::client::Context; use serenity::client::Context;
use serenity::model::id::{GuildId, RoleId}; use serenity::model::id::{ChannelId, GuildId, RoleId};
use serenity::model::prelude::application_command::ApplicationCommandInteraction; use serenity::model::prelude::application_command::ApplicationCommandInteraction;
use serenity::prelude::TypeMapKey; use serenity::prelude::TypeMapKey;
use std::{env, sync::Arc}; use std::{env, sync::Arc};
@ -31,6 +31,7 @@ pub struct Config {
// discord server for committee // discord server for committee
pub committee_server: GuildId, pub committee_server: GuildId,
pub committee_role: RoleId, pub committee_role: RoleId,
pub committee_category: ChannelId,
} }
impl TypeMapKey for Config { impl TypeMapKey for Config {
type Value = Arc<RwLock<Config>>; type Value = Arc<RwLock<Config>>;
@ -54,6 +55,7 @@ pub fn get_config() -> Config {
wolves_api: "".to_string(), wolves_api: "".to_string(),
committee_server: GuildId(0), committee_server: GuildId(0),
committee_role: RoleId(0), committee_role: RoleId(0),
committee_category: ChannelId(0),
}; };
if let Ok(x) = env::var("DATABASE_HOME") { if let Ok(x) = env::var("DATABASE_HOME") {
@ -98,6 +100,11 @@ pub fn get_config() -> Config {
config.committee_role = RoleId(x); config.committee_role = RoleId(x);
} }
} }
if let Ok(x) = env::var("COMMITTEE_CATEGORY") {
if let Ok(x) = x.trim().parse::<u64>() {
config.committee_category = ChannelId(x);
}
}
config config
} }