feat: new command to count the server info, available on the computer soc server
This commit is contained in:
parent
934842cbc9
commit
7d7afcd00c
5 changed files with 180 additions and 86 deletions
|
@ -1,80 +0,0 @@
|
|||
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)),
|
||||
)
|
||||
}
|
||||
}
|
156
src/commands/count.rs
Normal file
156
src/commands/count.rs
Normal file
|
@ -0,0 +1,156 @@
|
|||
pub mod committee {
|
||||
|
||||
// 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 - 3;
|
||||
|
||||
let mut response = vec!["```".to_string()];
|
||||
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 + 3) {
|
||||
response.push(line);
|
||||
limit -= length;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
response.push("```".to_string());
|
||||
|
||||
response.join("\n")
|
||||
}
|
||||
|
||||
pub fn register() -> CreateCommand {
|
||||
CreateCommand::new("count")
|
||||
.description("Commands related to teh committees")
|
||||
//.default_member_permissions(serenity::model::Permissions::MANAGE_GUILD)
|
||||
.add_option(
|
||||
CreateCommandOption::new(CommandOptionType::SubCommand, "committee", "List out the Committee Roles Numbers")
|
||||
.add_sub_option(CreateCommandOption::new(CommandOptionType::Boolean, "all", "List out all the Committee Roles Numbers").required(false)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub mod servers {
|
||||
// get the list of all the current clubs/socs
|
||||
use serenity::all::{CommandInteraction, CommandOptionType, Context, CreateCommand, CreateCommandOption};
|
||||
use skynet_discord_bot::common::database::{get_server_config_bulk, DataBase};
|
||||
use skynet_discord_bot::common::set_roles::committee::get_committees;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub async fn run(_command: &CommandInteraction, ctx: &Context) -> String {
|
||||
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 committees = HashMap::new();
|
||||
for committee in get_committees(&db).await {
|
||||
committees.insert(committee.id, committee.to_owned());
|
||||
}
|
||||
|
||||
let mut cs = vec![];
|
||||
// pull it from a DB
|
||||
for server_config in get_server_config_bulk(&db).await {
|
||||
if let Some(x) = committees.get(&server_config.wolves_id) {
|
||||
cs.push((server_config.member_current, server_config.member_past, x.name_full.to_owned()));
|
||||
}
|
||||
}
|
||||
|
||||
cs.sort_by_key(|(count, _, _)| *count);
|
||||
cs.reverse();
|
||||
|
||||
// msg can be a max 2000 chars long
|
||||
let mut limit = 2000 - 3;
|
||||
|
||||
let mut response = vec!["```".to_string()];
|
||||
for (current, past, name) in cs {
|
||||
let current_leading = if current < 10 {
|
||||
" "
|
||||
} else if current < 100 {
|
||||
" "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
let past_leading = if past < 10 {
|
||||
" "
|
||||
} else if past < 100 {
|
||||
" "
|
||||
} else {
|
||||
""
|
||||
};
|
||||
|
||||
let line = format!("{}{} {}{} {}", current_leading, current, past_leading, past, name);
|
||||
|
||||
let length = line.len() + 1;
|
||||
|
||||
// +3 is to account for the closing fense
|
||||
if length < (limit + 3) {
|
||||
response.push(line);
|
||||
limit -= length;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
response.push("```".to_string());
|
||||
|
||||
response.join("\n")
|
||||
}
|
||||
|
||||
pub fn register() -> CreateCommand {
|
||||
CreateCommand::new("count")
|
||||
.description("Commands related to all servers")
|
||||
.default_member_permissions(serenity::model::Permissions::MANAGE_GUILD)
|
||||
.add_option(CreateCommandOption::new(CommandOptionType::SubCommand, "servers", "List out all servers using the skynet bot"))
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
pub mod add_server;
|
||||
pub mod committee;
|
||||
pub mod count;
|
||||
pub mod link_email;
|
||||
pub mod minecraft;
|
||||
pub mod role_adder;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue