diff --git a/src/bin/update_minecraft.rs b/src/bin/update_minecraft.rs index 05e8831..016de39 100644 --- a/src/bin/update_minecraft.rs +++ b/src/bin/update_minecraft.rs @@ -1,5 +1,5 @@ -use std::collections::HashSet; use skynet_discord_bot::{db_init, get_config, get_minecraft_config, update_server, whitelist_wipe}; +use std::collections::HashSet; #[tokio::main] async fn main() { diff --git a/src/commands/add_server.rs b/src/commands/add_server.rs index fd192d2..e1e7ca4 100644 --- a/src/commands/add_server.rs +++ b/src/commands/add_server.rs @@ -63,7 +63,7 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri role_past, role_current, member_past: 0, - member_current: 0 + member_current: 0, }; match add_server(&db, ctx, &server_data).await { diff --git a/src/commands/minecraft.rs b/src/commands/minecraft.rs index c2164d8..ae80ff3 100644 --- a/src/commands/minecraft.rs +++ b/src/commands/minecraft.rs @@ -7,7 +7,7 @@ use serenity::{ }, }; -use skynet_discord_bot::{get_server_config, DataBase, Servers}; +use skynet_discord_bot::DataBase; use sqlx::{Pool, Sqlite}; pub(crate) mod user { @@ -16,7 +16,7 @@ pub(crate) mod user { use super::*; use crate::commands::link_email::link::get_server_member_discord; use serenity::model::id::UserId; - use skynet_discord_bot::{whitelist_update, Config, Wolves, Minecraft}; + use skynet_discord_bot::{whitelist_update, Config, Minecraft, Wolves}; use sqlx::Error; pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { @@ -88,10 +88,10 @@ pub(crate) mod user { WHERE discord = ?1; ", ) - .bind(*user.as_u64() as i64) - .bind(minecraft) - .fetch_optional(db) - .await + .bind(*user.as_u64() as i64) + .bind(minecraft) + .fetch_optional(db) + .await } async fn get_servers(db: &Pool, discord: &UserId) -> Result, Error> { @@ -107,9 +107,9 @@ pub(crate) mod user { ) sub on minecraft.server_discord = sub.server ", ) - .bind(*discord.as_u64() as i64) - .fetch_all(db) - .await + .bind(*discord.as_u64() as i64) + .fetch_all(db) + .await } } } @@ -125,16 +125,13 @@ pub(crate) mod server { use skynet_discord_bot::{is_admin, update_server, Config, Minecraft}; pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { - command - .name("minecraft_add") - .description("Add 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) - }) + command.name("minecraft_add").description("Add 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 { @@ -193,10 +190,10 @@ pub(crate) mod server { VALUES (?1, ?2) ", ) - .bind(*discord.as_u64() as i64) - .bind(minecraft) - .fetch_optional(db) - .await + .bind(*discord.as_u64() as i64) + .bind(minecraft) + .fetch_optional(db) + .await } } @@ -204,12 +201,10 @@ pub(crate) mod server { 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}; + use skynet_discord_bot::{get_minecraft_config_server, is_admin, server_information, Config, DataBase}; pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { - command - .name("minecraft_list") - .description("List your minecraft servers") + command.name("minecraft_list").description("List your minecraft servers") } pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { @@ -220,15 +215,15 @@ pub(crate) mod server { None => return "Not in a server".to_string(), Some(x) => x, }; - + 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 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(); } @@ -238,14 +233,17 @@ pub(crate) mod server { data_read.get::().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 { + 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.push_str(&format!( + "\n|{}|{}|{}|{}|", + &x.attributes.identifier, !x.attributes.is_suspended, &x.attributes.name, &x.attributes.description + )); } } - result + result.to_string() } } @@ -255,20 +253,17 @@ pub(crate) mod server { use serenity::model::application::command::CommandOptionType; use serenity::model::id::GuildId; use serenity::model::prelude::application_command::{ApplicationCommandInteraction, CommandDataOptionValue}; + use skynet_discord_bot::{is_admin, DataBase, Minecraft}; 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) - }) + 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 { @@ -308,7 +303,7 @@ pub(crate) mod server { 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() @@ -321,10 +316,10 @@ pub(crate) mod server { WHERE server_discord = ?1 AND server_minecraft = ?2 ", ) - .bind(*discord.as_u64() as i64) - .bind(minecraft) - .fetch_optional(db) - .await + .bind(*discord.as_u64() as i64) + .bind(minecraft) + .fetch_optional(db) + .await } } } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 0a1d475..9d3bea5 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,3 +1,3 @@ -pub mod minecraft; pub mod add_server; pub mod link_email; +pub mod minecraft; diff --git a/src/lib.rs b/src/lib.rs index c5de643..f05e58b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ use serenity::{ use crate::set_roles::get_server_member_bulk; use chrono::{Datelike, SecondsFormat, Utc}; use rand::{distributions::Alphanumeric, thread_rng, Rng}; +use serde::de::DeserializeOwned; use serenity::client::Context; use serenity::model::id::UserId; use serenity::model::prelude::application_command::ApplicationCommandInteraction; @@ -264,7 +265,6 @@ impl<'r> FromRow<'r, SqliteRow> for Servers { } } - #[derive(Debug, Clone, Deserialize, Serialize)] pub struct Minecraft { pub discord: GuildId, @@ -274,7 +274,7 @@ impl<'r> FromRow<'r, SqliteRow> for Minecraft { fn from_row(row: &'r SqliteRow) -> Result { let server_tmp: i64 = row.try_get("server_discord")?; let discord = GuildId::from(server_tmp as u64); - + Ok(Self { discord, minecraft: row.try_get("server_minecraft")?, @@ -697,7 +697,7 @@ loop through all members of server get a list of folks with mc accounts that are members and a list that arent members */ -pub async fn update_server(server_id: &String, db: &Pool, g_id: &GuildId, config: &Config) { +pub async fn update_server(server_id: &str, db: &Pool, g_id: &GuildId, config: &Config) { let mut usernames = vec![]; for member in get_server_member_bulk(db, g_id).await { if let Some(x) = member.minecraft { @@ -730,25 +730,25 @@ pub struct ServerDetailsResSub { pub identifier: String, pub name: String, pub description: String, - pub is_suspended: String, + pub is_suspended: bool, } #[derive(Deserialize, Serialize, Debug)] pub struct ServerDetailsRes { pub attributes: ServerDetailsResSub, } -async fn get(url: &str, bearer: &str) -> Option { +async fn get(url: &str, bearer: &str) -> Option { match surf::get(url) .header("Authorization", bearer) .header("Content-Type", "application/json") - .header("Accept", "Application/vnd.pterodactyl.v1+json").recv_json().await + .header("Accept", "Application/vnd.pterodactyl.v1+json") + .recv_json() + .await { - Ok(res) => { - Some(res) - } + Ok(res) => Some(res), Err(e) => { dbg!(e); - + None } } @@ -804,7 +804,6 @@ pub async fn server_information(server: &str, token: &str) -> Option(&format!("{url_base}/"), &bearer).await } - pub async fn get_minecraft_config(db: &Pool) -> Vec { sqlx::query_as::<_, Minecraft>( r#" @@ -812,12 +811,12 @@ pub async fn get_minecraft_config(db: &Pool) -> Vec { FROM minecraft "#, ) - .fetch_all(db) - .await - .unwrap_or_default() + .fetch_all(db) + .await + .unwrap_or_default() } -pub async fn get_minecraft_config_server(db: &Pool, g_id: GuildId) -> Vec { +pub async fn get_minecraft_config_server(db: &Pool, g_id: GuildId) -> Vec { sqlx::query_as::<_, Minecraft>( r#" SELECT * @@ -825,8 +824,8 @@ pub async fn get_minecraft_config_server(db: &Pool, g_id: GuildId) -> V WHERE server_discord = ?1 "#, ) - .bind(*g_id.as_u64() as i64) - .fetch_all(db) - .await - .unwrap_or_default() -} \ No newline at end of file + .bind(*g_id.as_u64() as i64) + .fetch_all(db) + .await + .unwrap_or_default() +}