Merge branch 'main' into #17-automate-onboarding-mk-ii
# Conflicts: # .gitignore # src/commands/committee.rs
This commit is contained in:
commit
b2d8238c17
12 changed files with 447 additions and 47 deletions
|
@ -39,18 +39,60 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri
|
|||
.as_ref()
|
||||
.expect("Expected role object")
|
||||
{
|
||||
Some(role.id.to_owned())
|
||||
role.id.to_owned()
|
||||
} else {
|
||||
return "Please provide a valid role for ``Role Current``".to_string();
|
||||
};
|
||||
|
||||
let mut role_past = None;
|
||||
if let Some(x) = command.data.options.get(2) {
|
||||
if let Some(x) = command.data.options.get(5) {
|
||||
if let Some(CommandDataOptionValue::Role(role)) = &x.resolved {
|
||||
role_past = Some(role.id.to_owned());
|
||||
}
|
||||
};
|
||||
|
||||
let bot_channel_id = if let CommandDataOptionValue::Channel(channel) = command
|
||||
.data
|
||||
.options
|
||||
.get(2)
|
||||
.expect("Expected channel option")
|
||||
.resolved
|
||||
.as_ref()
|
||||
.expect("Expected channel object")
|
||||
{
|
||||
channel.id.to_owned()
|
||||
} else {
|
||||
return "Please provide a valid channel for ``Bot Channel``".to_string();
|
||||
};
|
||||
|
||||
let server_name = if let CommandDataOptionValue::String(name) = command
|
||||
.data
|
||||
.options
|
||||
.get(3)
|
||||
.expect("Expected Server Name option")
|
||||
.resolved
|
||||
.as_ref()
|
||||
.expect("Expected Server Name object")
|
||||
{
|
||||
name
|
||||
} else {
|
||||
&"UL Computer Society".to_string()
|
||||
};
|
||||
|
||||
let wolves_link = if let CommandDataOptionValue::String(wolves) = command
|
||||
.data
|
||||
.options
|
||||
.get(4)
|
||||
.expect("Expected Wolves Link option")
|
||||
.resolved
|
||||
.as_ref()
|
||||
.expect("Expected Server Name object")
|
||||
{
|
||||
wolves
|
||||
} else {
|
||||
&"https://ulwolves.ie/society/computer".to_string()
|
||||
};
|
||||
|
||||
let db_lock = {
|
||||
let data_read = ctx.data.read().await;
|
||||
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
|
||||
|
@ -64,6 +106,9 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri
|
|||
role_current,
|
||||
member_past: 0,
|
||||
member_current: 0,
|
||||
bot_channel_id,
|
||||
server_name: server_name.to_owned(),
|
||||
wolves_link: wolves_link.to_string(),
|
||||
};
|
||||
|
||||
match add_server(&db, ctx, &server_data).await {
|
||||
|
@ -95,6 +140,27 @@ pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicatio
|
|||
.kind(CommandOptionType::Role)
|
||||
.required(true)
|
||||
})
|
||||
.create_option(|option| {
|
||||
option
|
||||
.name("bot_channel")
|
||||
.description("Safe space for folks to use the bot commands.")
|
||||
.kind(CommandOptionType::Channel)
|
||||
.required(true)
|
||||
})
|
||||
.create_option(|option| {
|
||||
option
|
||||
.name("server_name")
|
||||
.description("Name of the Discord Server.")
|
||||
.kind(CommandOptionType::String)
|
||||
.required(true)
|
||||
})
|
||||
.create_option(|option| {
|
||||
option
|
||||
.name("wolves_link")
|
||||
.description("Link to the Club/Society on UL Wolves.")
|
||||
.kind(CommandOptionType::String)
|
||||
.required(true)
|
||||
})
|
||||
.create_option(|option| {
|
||||
option
|
||||
.name("role_past")
|
||||
|
@ -107,18 +173,20 @@ pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicatio
|
|||
async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Result<Option<Servers>, Error> {
|
||||
let existing = get_server_config(db, &server.server).await;
|
||||
let role_past = server.role_past.map(|x| *x.as_u64() as i64);
|
||||
let role_current = server.role_current.map(|x| *x.as_u64() as i64);
|
||||
|
||||
let insert = sqlx::query_as::<_, Servers>(
|
||||
"
|
||||
INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current)
|
||||
VALUES (?1, ?2, ?3, ?4)
|
||||
INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current, bot_channel_id, server_name, wolves_link)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
|
||||
",
|
||||
)
|
||||
.bind(*server.server.as_u64() as i64)
|
||||
.bind(&server.wolves_api)
|
||||
.bind(role_past)
|
||||
.bind(role_current)
|
||||
.bind(*server.role_current.as_u64() as i64)
|
||||
.bind(*server.bot_channel_id.as_u64() as i64)
|
||||
.bind(&server.server_name)
|
||||
.bind(&server.wolves_link)
|
||||
.fetch_optional(db)
|
||||
.await;
|
||||
|
||||
|
@ -133,7 +201,7 @@ async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Resul
|
|||
if x.role_current != server.role_current {
|
||||
result.0 = true;
|
||||
result.1 = true;
|
||||
result.2 = x.role_current;
|
||||
result.2 = Some(x.role_current);
|
||||
}
|
||||
if x.role_past != server.role_past {
|
||||
result.0 = true;
|
||||
|
|
|
@ -59,7 +59,7 @@ pub mod link {
|
|||
// check if email exists
|
||||
let details = match get_server_member_email(&db, email).await {
|
||||
None => {
|
||||
return "Please check it is your preferred contact on https://ulwolves.ie/memberships/profile and that you are fully paid up.".to_string()
|
||||
return "Please check it matches (including case) your preferred contact on https://ulwolves.ie/memberships/profile and that you are fully paid up.".to_string()
|
||||
}
|
||||
Some(x) => x,
|
||||
};
|
||||
|
@ -184,7 +184,7 @@ pub mod link {
|
|||
let creds = Credentials::new(config.mail_user.clone(), config.mail_pass.clone());
|
||||
|
||||
// Open a remote connection to gmail using STARTTLS
|
||||
let mailer = SmtpTransport::starttls_relay(&config.mail_smtp).unwrap().credentials(creds).build();
|
||||
let mailer = SmtpTransport::starttls_relay(&config.mail_smtp)?.credentials(creds).build();
|
||||
|
||||
// Send the email
|
||||
mailer.send(&email)
|
||||
|
@ -352,10 +352,8 @@ pub mod verify {
|
|||
}
|
||||
}
|
||||
|
||||
if let Some(role) = &role_current {
|
||||
if !member.roles.contains(role) {
|
||||
roles.push(role.to_owned());
|
||||
}
|
||||
if !member.roles.contains(&role_current) {
|
||||
roles.push(role_current.to_owned());
|
||||
}
|
||||
|
||||
if let Err(e) = member.add_roles(&ctx, &roles).await {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
pub mod add_server;
|
||||
pub mod link_email;
|
||||
pub mod minecraft;
|
||||
pub mod role_adder;
|
||||
|
|
238
src/commands/role_adder.rs
Normal file
238
src/commands/role_adder.rs
Normal file
|
@ -0,0 +1,238 @@
|
|||
use serenity::{
|
||||
builder::CreateApplicationCommand,
|
||||
client::Context,
|
||||
model::{
|
||||
application::interaction::application_command::ApplicationCommandInteraction,
|
||||
prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue},
|
||||
},
|
||||
};
|
||||
|
||||
use skynet_discord_bot::{is_admin, DataBase, RoleAdder};
|
||||
use sqlx::{Error, Pool, Sqlite};
|
||||
|
||||
pub mod edit {
|
||||
use super::*;
|
||||
|
||||
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
||||
// check if user has high enough permisssions
|
||||
if let Some(msg) = is_admin(command, ctx).await {
|
||||
return msg;
|
||||
}
|
||||
|
||||
let role_a = if let CommandDataOptionValue::Role(role) = command
|
||||
.data
|
||||
.options
|
||||
.first()
|
||||
.expect("Expected role option")
|
||||
.resolved
|
||||
.as_ref()
|
||||
.expect("Expected role object")
|
||||
{
|
||||
role.id.to_owned()
|
||||
} else {
|
||||
return "Please provide a valid role for ``Role Current``".to_string();
|
||||
};
|
||||
|
||||
let role_b = if let CommandDataOptionValue::Role(role) = command
|
||||
.data
|
||||
.options
|
||||
.get(1)
|
||||
.expect("Expected role option")
|
||||
.resolved
|
||||
.as_ref()
|
||||
.expect("Expected role object")
|
||||
{
|
||||
role.id.to_owned()
|
||||
} else {
|
||||
return "Please provide a valid role for ``Role Current``".to_string();
|
||||
};
|
||||
|
||||
let role_c = if let CommandDataOptionValue::Role(role) = command
|
||||
.data
|
||||
.options
|
||||
.get(2)
|
||||
.expect("Expected role option")
|
||||
.resolved
|
||||
.as_ref()
|
||||
.expect("Expected role object")
|
||||
{
|
||||
role.id.to_owned()
|
||||
} else {
|
||||
return "Please provide a valid role for ``Role Current``".to_string();
|
||||
};
|
||||
|
||||
if role_a == role_b {
|
||||
return "Roles A and B must be different".to_string();
|
||||
}
|
||||
|
||||
if (role_c == role_a) || (role_c == role_b) {
|
||||
return "Role C cannot be same as A or B".to_string();
|
||||
}
|
||||
|
||||
let mut delete = false;
|
||||
|
||||
if let Some(x) = command.data.options.get(3) {
|
||||
let tmp = x.to_owned();
|
||||
if let Some(CommandDataOptionValue::Boolean(z)) = tmp.resolved {
|
||||
delete = z;
|
||||
}
|
||||
}
|
||||
|
||||
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 server = command.guild_id.unwrap_or_default();
|
||||
let server_data = RoleAdder {
|
||||
server,
|
||||
role_a,
|
||||
role_b,
|
||||
role_c,
|
||||
};
|
||||
|
||||
match add_server(&db, &server_data, delete).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("{:?}", e);
|
||||
return format!("Failure to insert into Servers {:?}", server_data);
|
||||
}
|
||||
}
|
||||
|
||||
let mut role_a_name = String::new();
|
||||
let mut role_b_name = String::new();
|
||||
let mut role_c_name = String::new();
|
||||
|
||||
if let Ok(x) = server.roles(&ctx).await {
|
||||
if let Some(y) = x.get(&role_a) {
|
||||
role_a_name = y.to_owned().name;
|
||||
}
|
||||
if let Some(y) = x.get(&role_b) {
|
||||
role_b_name = y.to_owned().name;
|
||||
}
|
||||
if let Some(y) = x.get(&role_b) {
|
||||
role_c_name = y.to_owned().name;
|
||||
}
|
||||
}
|
||||
|
||||
if delete {
|
||||
format!("Removed {} + {} = {}", role_a_name, role_b_name, role_c_name)
|
||||
} else {
|
||||
format!("Added {} + {} = {}", role_a_name, role_b_name, role_c_name)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||
command
|
||||
.name("roles_adder")
|
||||
.description("Combine roles together to an new one")
|
||||
.create_option(|option| {
|
||||
option
|
||||
.name("role_a")
|
||||
.description("A role you want to add to Role B")
|
||||
.kind(CommandOptionType::Role)
|
||||
.required(true)
|
||||
})
|
||||
.create_option(|option| {
|
||||
option
|
||||
.name("role_b")
|
||||
.description("A role you want to add to Role A")
|
||||
.kind(CommandOptionType::Role)
|
||||
.required(true)
|
||||
})
|
||||
.create_option(|option| option.name("role_c").description("Sum of A and B").kind(CommandOptionType::Role).required(true))
|
||||
.create_option(|option| {
|
||||
option
|
||||
.name("delete")
|
||||
.description("Delete this entry.")
|
||||
.kind(CommandOptionType::Boolean)
|
||||
.required(false)
|
||||
})
|
||||
}
|
||||
|
||||
async fn add_server(db: &Pool<Sqlite>, server: &RoleAdder, delete: bool) -> Result<Option<RoleAdder>, Error> {
|
||||
if delete {
|
||||
sqlx::query_as::<_, RoleAdder>(
|
||||
"
|
||||
DELETE FROM roles_adder
|
||||
WHERE server = ?1 AND role_a = ?2 AND role_b = ?3 AND role_c = ?4
|
||||
",
|
||||
)
|
||||
.bind(*server.server.as_u64() as i64)
|
||||
.bind(*server.role_a.as_u64() as i64)
|
||||
.bind(*server.role_b.as_u64() as i64)
|
||||
.bind(*server.role_c.as_u64() as i64)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
} else {
|
||||
sqlx::query_as::<_, RoleAdder>(
|
||||
"
|
||||
INSERT OR REPLACE INTO roles_adder (server, role_a, role_b, role_c)
|
||||
VALUES (?1, ?2, ?3, ?4)
|
||||
",
|
||||
)
|
||||
.bind(*server.server.as_u64() as i64)
|
||||
.bind(*server.role_a.as_u64() as i64)
|
||||
.bind(*server.role_b.as_u64() as i64)
|
||||
.bind(*server.role_c.as_u64() as i64)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
pub mod list {}
|
||||
|
||||
pub mod tools {
|
||||
use serenity::client::Context;
|
||||
use serenity::model::guild::Member;
|
||||
use skynet_discord_bot::RoleAdder;
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
pub async fn on_role_change(db: &Pool<Sqlite>, ctx: &Context, mut new_data: Member) {
|
||||
// check if the role changed is part of the oens for this server
|
||||
if let Ok(role_adders) = sqlx::query_as::<_, RoleAdder>(
|
||||
r#"
|
||||
SELECT *
|
||||
FROM roles_adder
|
||||
WHERE server = ?
|
||||
"#,
|
||||
)
|
||||
.bind(*new_data.guild_id.as_u64() as i64)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
{
|
||||
let mut roles_add = vec![];
|
||||
let mut roles_remove = vec![];
|
||||
|
||||
for role_adder in role_adders {
|
||||
// if the user has both A dnd B give them C
|
||||
if new_data.roles.contains(&role_adder.role_a) && new_data.roles.contains(&role_adder.role_b) && !new_data.roles.contains(&role_adder.role_c)
|
||||
{
|
||||
roles_add.push(role_adder.role_c);
|
||||
}
|
||||
|
||||
// If the suer has C but not A or B remove C
|
||||
if new_data.roles.contains(&role_adder.role_c)
|
||||
&& (!new_data.roles.contains(&role_adder.role_a) || !new_data.roles.contains(&role_adder.role_b))
|
||||
{
|
||||
roles_remove.push(role_adder.role_c);
|
||||
}
|
||||
}
|
||||
|
||||
if !roles_add.is_empty() {
|
||||
if let Err(e) = new_data.add_roles(&ctx, &roles_add).await {
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
if !roles_remove.is_empty() {
|
||||
if let Err(e) = new_data.remove_roles(&ctx, &roles_remove).await {
|
||||
println!("{:?}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue