From 94813580684eb6e75d321189a37d34fcd08ce7e5 Mon Sep 17 00:00:00 2001 From: Brendan Golden Date: Mon, 3 Jun 2024 03:56:45 +0100 Subject: [PATCH] feat: added a delete server command --- src/commands/minecraft.rs | 85 +++++++++++++++++++++++++++++++++++++-- src/main.rs | 2 + 2 files changed, 84 insertions(+), 3 deletions(-) diff --git a/src/commands/minecraft.rs b/src/commands/minecraft.rs index 0f3e363..c2164d8 100644 --- a/src/commands/minecraft.rs +++ b/src/commands/minecraft.rs @@ -122,7 +122,7 @@ pub(crate) mod server { use sqlx::Error; // this is to managfe the server side of commands related to minecraft use super::*; - use skynet_discord_bot::{is_admin, update_server, Config}; + use skynet_discord_bot::{is_admin, update_server, Config, Minecraft}; pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { command @@ -186,8 +186,8 @@ pub(crate) mod server { "Added/Updated minecraft_server info".to_string() } - async fn add_server(db: &Pool, discord: &GuildId, minecraft: &str) -> Result, Error> { - sqlx::query_as::<_, Servers>( + async fn add_server(db: &Pool, discord: &GuildId, minecraft: &str) -> Result, Error> { + sqlx::query_as::<_, Minecraft>( " INSERT OR REPLACE INTO minecraft (server_discord, server_minecraft) VALUES (?1, ?2) @@ -248,4 +248,83 @@ pub(crate) mod server { result } } + + pub(crate) mod delete { + use serenity::builder::CreateApplicationCommand; + use serenity::client::Context; + use serenity::model::application::command::CommandOptionType; + use serenity::model::id::GuildId; + use serenity::model::prelude::application_command::{ApplicationCommandInteraction, CommandDataOptionValue}; + use sqlx::{Error, Pool, Sqlite}; + use skynet_discord_bot::{Config, DataBase, get_minecraft_config_server, get_server_config, is_admin, Minecraft, server_information, Servers, update_server}; + + pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { + command + .name("minecraft_delete") + .description("Delete a minecraft server") + .create_option(|option| { + option + .name("server_id") + .description("ID of the Minecraft server hosted by the Computer Society") + .kind(CommandOptionType::String) + .required(true) + }) + } + + 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 g_id = match command.guild_id { + None => return "Not in a server".to_string(), + Some(x) => x, + }; + + let server_minecraft = if let CommandDataOptionValue::String(id) = command + .data + .options + .first() + .expect("Expected server_id option") + .resolved + .as_ref() + .expect("Expected server_id object") + { + id.to_owned() + } else { + return String::from("Expected Server ID"); + }; + + 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; + + match server_remove(&db, &g_id, &server_minecraft).await { + Ok(_) => {} + Err(e) => { + println!("{:?}", e); + return format!("Failure to insert into Minecraft {} {}", &g_id, &server_minecraft); + } + } + + // no need to clear teh whitelist as it will be reset within 24hr anyways + + "Removed minecraft_server info".to_string() + } + + async fn server_remove(db: &Pool, discord: &GuildId, minecraft: &str) -> Result, Error> { + sqlx::query_as::<_, Minecraft>( + " + DELETE FROM minecraft + WHERE server_discord = ?1 AND server_minecraft = ?2 + ", + ) + .bind(*discord.as_u64() as i64) + .bind(minecraft) + .fetch_optional(db) + .await + } + } } diff --git a/src/main.rs b/src/main.rs index c972037..d1ebf2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,6 +62,7 @@ impl EventHandler for Handler { .create_application_command(|command| commands::link_email::verify::register(command)) .create_application_command(|command| commands::minecraft::server::add::register(command)) .create_application_command(|command| commands::minecraft::server::list::register(command)) + .create_application_command(|command| commands::minecraft::server::delete::register(command)) .create_application_command(|command| commands::minecraft::user::add::register(command)) }) .await @@ -87,6 +88,7 @@ impl EventHandler for Handler { "add" => commands::add_server::run(&command, &ctx).await, "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, _ => "not implemented :(".to_string(), };