feat: add a command to list minecraft servers

This commit is contained in:
silver 2024-06-03 03:04:12 +01:00
parent 2970549eb0
commit c2a6407ef0
3 changed files with 88 additions and 2 deletions

View file

@ -207,4 +207,53 @@ pub(crate) mod server {
.await
}
}
pub(crate) mod list {
use serenity::builder::CreateApplicationCommand;
use serenity::client::Context;
use serenity::model::prelude::application_command::ApplicationCommandInteraction;
use skynet_discord_bot::{Config, DataBase, get_minecraft_config_server, is_admin, server_information};
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command
.name("minecraft_list")
.description("List your minecraft servers")
}
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
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 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 servers = get_minecraft_config_server(&db, g_id).await;
if servers.is_empty() {
return "No minecraft servers, use /minecraft_add to add one".to_string();
}
let config_lock = {
let data_read = ctx.data.read().await;
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
};
let config = config_lock.read().await;
let mut result = "|ID|Online|Name|Description|\n|:---|:---|:---|:---|".to_string();
for server in get_minecraft_config_server(&db, g_id).await {
if let Some(x) = server_information(&server.minecraft, &config.discord_minecraft).await {
write!(result, "\n|{}|{}|{}|{}|", &x.attributes.identifier, !x.attributes.is_suspended, &x.attributes.name, &x.attributes.description).unwrap();
}
}
result
}
}
}