feat: new command to see how many of each club/soc are on teh server
This commit is contained in:
parent
0bedf96da5
commit
143483d3b3
4 changed files with 110 additions and 9 deletions
80
src/commands/committee.rs
Normal file
80
src/commands/committee.rs
Normal file
|
@ -0,0 +1,80 @@
|
|||
pub mod count {
|
||||
|
||||
// get the list of all the current clubs/socs members
|
||||
|
||||
use serenity::all::{
|
||||
CommandDataOption, CommandDataOptionValue, CommandInteraction, CommandOptionType, Context, CreateCommand, CreateCommandOption,
|
||||
};
|
||||
use skynet_discord_bot::common::database::DataBase;
|
||||
use skynet_discord_bot::common::set_roles::committee::db_roles_get;
|
||||
|
||||
pub async fn run(command: &CommandInteraction, ctx: &Context) -> String {
|
||||
let sub_options = if let Some(CommandDataOption {
|
||||
value: CommandDataOptionValue::SubCommand(key),
|
||||
..
|
||||
}) = command.data.options.first()
|
||||
{
|
||||
key
|
||||
} else {
|
||||
return "Please provide a wolves API key".to_string();
|
||||
};
|
||||
|
||||
let all = if let Some(x) = sub_options.first() {
|
||||
match x.value {
|
||||
CommandDataOptionValue::Boolean(y) => y,
|
||||
_ => false,
|
||||
}
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let db_lock = {
|
||||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
|
||||
};
|
||||
let db = db_lock.read().await;
|
||||
|
||||
let mut cs = vec![];
|
||||
// pull it from a DB
|
||||
for committee in db_roles_get(&db).await {
|
||||
if !all && committee.count == 0 {
|
||||
continue;
|
||||
}
|
||||
cs.push((committee.count, committee.name_role.to_owned()));
|
||||
}
|
||||
|
||||
cs.sort_by_key(|(count, _)| *count);
|
||||
cs.reverse();
|
||||
|
||||
// msg can be a max 2000 chars long
|
||||
let mut limit = 2000;
|
||||
|
||||
let mut response = vec![];
|
||||
for (count, name) in cs {
|
||||
let leading = if count < 10 { " " } else { "" };
|
||||
|
||||
let line = format!("{}{} {}", leading, count, name);
|
||||
|
||||
let length = line.len() + 1;
|
||||
|
||||
if length < limit {
|
||||
response.push(line);
|
||||
limit -= length;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
response.join("\n")
|
||||
}
|
||||
|
||||
pub fn register() -> CreateCommand {
|
||||
CreateCommand::new("committee")
|
||||
.description("Commands related to teh committees")
|
||||
//.default_member_permissions(serenity::model::Permissions::MANAGE_GUILD)
|
||||
.add_option(
|
||||
CreateCommandOption::new(CommandOptionType::SubCommand, "list", "List out the Committee Roles Numbers")
|
||||
.add_sub_option(CreateCommandOption::new(CommandOptionType::Boolean, "all", "List out all the Committee Roles Numbers").required(false)),
|
||||
)
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
pub mod add_server;
|
||||
pub mod committee;
|
||||
pub mod link_email;
|
||||
pub mod minecraft;
|
||||
pub mod role_adder;
|
||||
|
|
|
@ -293,6 +293,7 @@ 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 {
|
||||
if server.member(ctx, &member_tmp).await.is_ok() {
|
||||
let values = users_roles.entry(member_tmp).or_insert(vec![]);
|
||||
values.push(r);
|
||||
|
||||
|
@ -303,6 +304,7 @@ pub mod committee {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
|
@ -399,13 +401,13 @@ pub mod committee {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
struct CommitteeRoles {
|
||||
pub struct CommitteeRoles {
|
||||
id_wolves: i64,
|
||||
id_role: RoleId,
|
||||
id_channel: ChannelId,
|
||||
name_role: String,
|
||||
pub name_role: String,
|
||||
name_channel: String,
|
||||
count: i64,
|
||||
pub count: i64,
|
||||
}
|
||||
|
||||
impl<'r> FromRow<'r, SqliteRow> for CommitteeRoles {
|
||||
|
@ -447,7 +449,7 @@ pub mod committee {
|
|||
}
|
||||
}
|
||||
|
||||
async fn db_roles_get(db: &Pool<Sqlite>) -> Vec<CommitteeRoles> {
|
||||
pub async fn db_roles_get(db: &Pool<Sqlite>) -> Vec<CommitteeRoles> {
|
||||
// expiry
|
||||
sqlx::query_as::<_, CommitteeRoles>(
|
||||
"
|
||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -130,6 +130,23 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
|
|||
println!("{:?}", e)
|
||||
}
|
||||
}
|
||||
|
||||
let config_lock = {
|
||||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
|
||||
};
|
||||
let config_global = config_lock.read().await;
|
||||
|
||||
match &config_global
|
||||
.committee_server
|
||||
.set_commands(&ctx.http, vec![commands::committee::count::register()])
|
||||
.await
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("{:?}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||
|
@ -148,6 +165,7 @@ Sign up on [UL Wolves]({}) and go to https://discord.com/channels/{}/{} and use
|
|||
"minecraft_add" => commands::minecraft::server::add::run(&command, &ctx).await,
|
||||
"minecraft_list" => commands::minecraft::server::list::run(&command, &ctx).await,
|
||||
"minecraft_delete" => commands::minecraft::server::delete::run(&command, &ctx).await,
|
||||
"committee" => commands::committee::count::run(&command, &ctx).await,
|
||||
_ => "not implemented :(".to_string(),
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue