diff --git a/src/commands/add_server.rs b/src/commands/add_server.rs new file mode 100644 index 0000000..0f112a1 --- /dev/null +++ b/src/commands/add_server.rs @@ -0,0 +1,149 @@ +use serenity::{ + builder::CreateApplicationCommand, + client::Context, + model::{ + application::interaction::application_command::ApplicationCommandInteraction, + prelude::{command::CommandOptionType, interaction::application_command::CommandDataOptionValue}, + }, +}; +use skynet_discord_bot::{DataBase, Servers, Wolves}; +use sqlx::{Error, Pool, Sqlite}; + +pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { + // check if user has high enough permisssions + let mut admin = false; + + let g_id = match command.guild_id { + None => return "Not in a server".to_string(), + Some(x) => x, + }; + + let roles_server = g_id.roles(&ctx.http).await.unwrap_or_default(); + + if let Ok(member) = g_id.member(&ctx.http, command.user.id).await { + if let Some(permissions) = member.permissions { + if permissions.administrator() { + admin = true; + } + } + + for role_id in member.roles { + if admin { + break; + } + if let Some(role) = roles_server.get(&role_id) { + if role.permissions.administrator() { + admin = true; + } + } + } + } + if !admin { + return "Administrator permission required".to_string(); + } + + let api_key = if let CommandDataOptionValue::String(key) = command + .data + .options + .get(0) + .expect("Expected user option") + .resolved + .as_ref() + .expect("Expected user object") + { + key + } else { + return "Please provide a wolves API key".to_string(); + }; + + let role_current = if let CommandDataOptionValue::Role(role) = command + .data + .options + .get(1) + .expect("Expected role option") + .resolved + .as_ref() + .expect("Expected role object") + { + Some(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(CommandDataOptionValue::Role(role)) = &x.resolved { + role_past = Some(role.id.to_owned()); + } + }; + + let db_lock = { + let data_read = ctx.data.read().await; + data_read.get::().expect("Expected Databse in TypeMap.").clone() + }; + let db = db_lock.read().await; + + let server_data = Servers { + server: command.guild_id.unwrap_or_default(), + wolves_api: api_key.to_owned(), + role_past, + role_current, + member_past: 0, + member_current: 0, + }; + + match add_server(&db, &server_data).await { + Ok(_) => {} + Err(e) => { + println!("{:?}", e); + return format!("Failure to insert into Servers {:?}", server_data); + } + } + + "Added/Updated server info".to_string() +} + +pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { + command + .name("add") + .description("Enable the bot for this discord") + .create_option(|option| { + option + .name("api_key") + .description("UL Wolves API Key") + .kind(CommandOptionType::String) + .required(true) + }) + .create_option(|option| { + option + .name("role_current") + .description("Role for Current members") + .kind(CommandOptionType::Role) + .required(true) + }) + .create_option(|option| { + option + .name("role_past") + .description("Role for Past members") + .kind(CommandOptionType::Role) + .required(false) + }) +} + +async fn add_server(db: &Pool, server: &Servers) -> Result, Error> { + 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); + + sqlx::query_as::<_, Wolves>( + " + INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current) + VALUES (?1, ?2, ?3, ?4) + ", + ) + .bind(*server.server.as_u64() as i64) + .bind(&server.wolves_api) + .bind(role_past) + .bind(role_current) + .fetch_optional(db) + .await +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 1ab5243..4be8b3d 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1 +1,2 @@ +pub mod add_server; pub mod link_email; diff --git a/src/lib.rs b/src/lib.rs index 863a777..21ce390 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -239,7 +239,7 @@ pub async fn db_init(config: &Config) -> Result, Error> { sqlx::query( "CREATE TABLE IF NOT EXISTS servers ( - server integer KEY, + server integer PRIMARY KEY, wolves_api text not null, role_past integer, role_current integer, diff --git a/src/main.rs b/src/main.rs index 28382ec..be2e4b7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,13 +55,19 @@ impl EventHandler for Handler { async fn ready(&self, ctx: Context, ready: Ready) { println!("[Main] {} is connected!", ready.user.name); - Command::set_global_application_commands(&ctx.http, |commands| { + match Command::set_global_application_commands(&ctx.http, |commands| { commands + .create_application_command(|command| commands::add_server::register(command)) .create_application_command(|command| commands::link_email::link::register(command)) .create_application_command(|command| commands::link_email::verify::register(command)) }) .await - .ok(); + { + Ok(_) => {} + Err(e) => { + println!("{:?}", e) + } + } } async fn interaction_create(&self, ctx: Context, interaction: Interaction) { @@ -70,6 +76,7 @@ impl EventHandler for Handler { //println!("Received command interaction: {:#?}", command); let content = match command.data.name.as_str() { + "add" => commands::add_server::run(&command, &ctx).await, "link" => commands::link_email::link::run(&command, &ctx).await, "verify" => commands::link_email::verify::run(&command, &ctx).await, _ => "not implemented :(".to_string(),