forked from Skynet/discord-bot
feat: added a delete server command
This commit is contained in:
parent
33cebe7782
commit
9481358068
2 changed files with 84 additions and 3 deletions
|
@ -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<Sqlite>, discord: &GuildId, minecraft: &str) -> Result<Option<Servers>, Error> {
|
||||
sqlx::query_as::<_, Servers>(
|
||||
async fn add_server(db: &Pool<Sqlite>, discord: &GuildId, minecraft: &str) -> Result<Option<Minecraft>, 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::<DataBase>().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<Sqlite>, discord: &GuildId, minecraft: &str) -> Result<Option<Minecraft>, 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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(),
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue