fmt: clippy and fmt

This commit is contained in:
silver 2024-06-03 04:06:47 +01:00
parent 9481358068
commit 55b2e534d4
5 changed files with 67 additions and 73 deletions

View file

@ -1,5 +1,5 @@
use std::collections::HashSet;
use skynet_discord_bot::{db_init, get_config, get_minecraft_config, update_server, whitelist_wipe}; use skynet_discord_bot::{db_init, get_config, get_minecraft_config, update_server, whitelist_wipe};
use std::collections::HashSet;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {

View file

@ -63,7 +63,7 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri
role_past, role_past,
role_current, role_current,
member_past: 0, member_past: 0,
member_current: 0 member_current: 0,
}; };
match add_server(&db, ctx, &server_data).await { match add_server(&db, ctx, &server_data).await {

View file

@ -7,7 +7,7 @@ use serenity::{
}, },
}; };
use skynet_discord_bot::{get_server_config, DataBase, Servers}; use skynet_discord_bot::DataBase;
use sqlx::{Pool, Sqlite}; use sqlx::{Pool, Sqlite};
pub(crate) mod user { pub(crate) mod user {
@ -16,7 +16,7 @@ pub(crate) mod user {
use super::*; use super::*;
use crate::commands::link_email::link::get_server_member_discord; use crate::commands::link_email::link::get_server_member_discord;
use serenity::model::id::UserId; 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; use sqlx::Error;
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
@ -88,10 +88,10 @@ pub(crate) mod user {
WHERE discord = ?1; WHERE discord = ?1;
", ",
) )
.bind(*user.as_u64() as i64) .bind(*user.as_u64() as i64)
.bind(minecraft) .bind(minecraft)
.fetch_optional(db) .fetch_optional(db)
.await .await
} }
async fn get_servers(db: &Pool<Sqlite>, discord: &UserId) -> Result<Vec<Minecraft>, Error> { async fn get_servers(db: &Pool<Sqlite>, discord: &UserId) -> Result<Vec<Minecraft>, Error> {
@ -107,9 +107,9 @@ pub(crate) mod user {
) sub on minecraft.server_discord = sub.server ) sub on minecraft.server_discord = sub.server
", ",
) )
.bind(*discord.as_u64() as i64) .bind(*discord.as_u64() as i64)
.fetch_all(db) .fetch_all(db)
.await .await
} }
} }
} }
@ -125,16 +125,13 @@ pub(crate) mod server {
use skynet_discord_bot::{is_admin, update_server, Config, Minecraft}; use skynet_discord_bot::{is_admin, update_server, Config, Minecraft};
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand { pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command command.name("minecraft_add").description("Add a minecraft server").create_option(|option| {
.name("minecraft_add") option
.description("Add a minecraft server") .name("server_id")
.create_option(|option| { .description("ID of the Minecraft server hosted by the Computer Society")
option .kind(CommandOptionType::String)
.name("server_id") .required(true)
.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 { pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
@ -193,10 +190,10 @@ pub(crate) mod server {
VALUES (?1, ?2) VALUES (?1, ?2)
", ",
) )
.bind(*discord.as_u64() as i64) .bind(*discord.as_u64() as i64)
.bind(minecraft) .bind(minecraft)
.fetch_optional(db) .fetch_optional(db)
.await .await
} }
} }
@ -204,12 +201,10 @@ pub(crate) mod server {
use serenity::builder::CreateApplicationCommand; use serenity::builder::CreateApplicationCommand;
use serenity::client::Context; use serenity::client::Context;
use serenity::model::prelude::application_command::ApplicationCommandInteraction; 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 { pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command command.name("minecraft_list").description("List your minecraft servers")
.name("minecraft_list")
.description("List your minecraft servers")
} }
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String { 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(), None => return "Not in a server".to_string(),
Some(x) => x, Some(x) => x,
}; };
let db_lock = { let db_lock = {
let data_read = ctx.data.read().await; let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone() data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
}; };
let db = db_lock.read().await; let db = db_lock.read().await;
let servers = get_minecraft_config_server(&db, g_id).await; let servers = get_minecraft_config_server(&db, g_id).await;
if servers.is_empty() { if servers.is_empty() {
return "No minecraft servers, use /minecraft_add to add one".to_string(); return "No minecraft servers, use /minecraft_add to add one".to_string();
} }
@ -238,14 +233,17 @@ pub(crate) mod server {
data_read.get::<Config>().expect("Expected Config in TypeMap.").clone() data_read.get::<Config>().expect("Expected Config in TypeMap.").clone()
}; };
let config = config_lock.read().await; let config = config_lock.read().await;
let mut result = "|ID|Online|Name|Description|\n|:---|:---|:---|:---|".to_string(); 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 { 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::application::command::CommandOptionType;
use serenity::model::id::GuildId; use serenity::model::id::GuildId;
use serenity::model::prelude::application_command::{ApplicationCommandInteraction, CommandDataOptionValue}; use serenity::model::prelude::application_command::{ApplicationCommandInteraction, CommandDataOptionValue};
use skynet_discord_bot::{is_admin, DataBase, Minecraft};
use sqlx::{Error, Pool, Sqlite}; 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 { pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command command.name("minecraft_delete").description("Delete a minecraft server").create_option(|option| {
.name("minecraft_delete") option
.description("Delete a minecraft server") .name("server_id")
.create_option(|option| { .description("ID of the Minecraft server hosted by the Computer Society")
option .kind(CommandOptionType::String)
.name("server_id") .required(true)
.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 { 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); 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 // no need to clear teh whitelist as it will be reset within 24hr anyways
"Removed minecraft_server info".to_string() "Removed minecraft_server info".to_string()
@ -321,10 +316,10 @@ pub(crate) mod server {
WHERE server_discord = ?1 AND server_minecraft = ?2 WHERE server_discord = ?1 AND server_minecraft = ?2
", ",
) )
.bind(*discord.as_u64() as i64) .bind(*discord.as_u64() as i64)
.bind(minecraft) .bind(minecraft)
.fetch_optional(db) .fetch_optional(db)
.await .await
} }
} }
} }

View file

@ -1,3 +1,3 @@
pub mod minecraft;
pub mod add_server; pub mod add_server;
pub mod link_email; pub mod link_email;
pub mod minecraft;

View file

@ -11,6 +11,7 @@ use serenity::{
use crate::set_roles::get_server_member_bulk; use crate::set_roles::get_server_member_bulk;
use chrono::{Datelike, SecondsFormat, Utc}; use chrono::{Datelike, SecondsFormat, Utc};
use rand::{distributions::Alphanumeric, thread_rng, Rng}; use rand::{distributions::Alphanumeric, thread_rng, Rng};
use serde::de::DeserializeOwned;
use serenity::client::Context; use serenity::client::Context;
use serenity::model::id::UserId; use serenity::model::id::UserId;
use serenity::model::prelude::application_command::ApplicationCommandInteraction; use serenity::model::prelude::application_command::ApplicationCommandInteraction;
@ -264,7 +265,6 @@ impl<'r> FromRow<'r, SqliteRow> for Servers {
} }
} }
#[derive(Debug, Clone, Deserialize, Serialize)] #[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Minecraft { pub struct Minecraft {
pub discord: GuildId, pub discord: GuildId,
@ -274,7 +274,7 @@ impl<'r> FromRow<'r, SqliteRow> for Minecraft {
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> { fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
let server_tmp: i64 = row.try_get("server_discord")?; let server_tmp: i64 = row.try_get("server_discord")?;
let discord = GuildId::from(server_tmp as u64); let discord = GuildId::from(server_tmp as u64);
Ok(Self { Ok(Self {
discord, discord,
minecraft: row.try_get("server_minecraft")?, 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 get a list of folks with mc accounts that are members
and a list that arent members and a list that arent members
*/ */
pub async fn update_server(server_id: &String, db: &Pool<Sqlite>, g_id: &GuildId, config: &Config) { pub async fn update_server(server_id: &str, db: &Pool<Sqlite>, g_id: &GuildId, config: &Config) {
let mut usernames = vec![]; let mut usernames = vec![];
for member in get_server_member_bulk(db, g_id).await { for member in get_server_member_bulk(db, g_id).await {
if let Some(x) = member.minecraft { if let Some(x) = member.minecraft {
@ -730,25 +730,25 @@ pub struct ServerDetailsResSub {
pub identifier: String, pub identifier: String,
pub name: String, pub name: String,
pub description: String, pub description: String,
pub is_suspended: String, pub is_suspended: bool,
} }
#[derive(Deserialize, Serialize, Debug)] #[derive(Deserialize, Serialize, Debug)]
pub struct ServerDetailsRes { pub struct ServerDetailsRes {
pub attributes: ServerDetailsResSub, pub attributes: ServerDetailsResSub,
} }
async fn get<T: Serialize>(url: &str, bearer: &str) -> Option<T> { async fn get<T: Serialize + DeserializeOwned>(url: &str, bearer: &str) -> Option<T> {
match surf::get(url) match surf::get(url)
.header("Authorization", bearer) .header("Authorization", bearer)
.header("Content-Type", "application/json") .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) => { Ok(res) => Some(res),
Some(res)
}
Err(e) => { Err(e) => {
dbg!(e); dbg!(e);
None None
} }
} }
@ -804,7 +804,6 @@ pub async fn server_information(server: &str, token: &str) -> Option<ServerDetai
get::<ServerDetailsRes>(&format!("{url_base}/"), &bearer).await get::<ServerDetailsRes>(&format!("{url_base}/"), &bearer).await
} }
pub async fn get_minecraft_config(db: &Pool<Sqlite>) -> Vec<Minecraft> { pub async fn get_minecraft_config(db: &Pool<Sqlite>) -> Vec<Minecraft> {
sqlx::query_as::<_, Minecraft>( sqlx::query_as::<_, Minecraft>(
r#" r#"
@ -812,12 +811,12 @@ pub async fn get_minecraft_config(db: &Pool<Sqlite>) -> Vec<Minecraft> {
FROM minecraft FROM minecraft
"#, "#,
) )
.fetch_all(db) .fetch_all(db)
.await .await
.unwrap_or_default() .unwrap_or_default()
} }
pub async fn get_minecraft_config_server(db: &Pool<Sqlite>, g_id: GuildId) -> Vec<Minecraft> { pub async fn get_minecraft_config_server(db: &Pool<Sqlite>, g_id: GuildId) -> Vec<Minecraft> {
sqlx::query_as::<_, Minecraft>( sqlx::query_as::<_, Minecraft>(
r#" r#"
SELECT * SELECT *
@ -825,8 +824,8 @@ pub async fn get_minecraft_config_server(db: &Pool<Sqlite>, g_id: GuildId) -> V
WHERE server_discord = ?1 WHERE server_discord = ?1
"#, "#,
) )
.bind(*g_id.as_u64() as i64) .bind(*g_id.as_u64() as i64)
.fetch_all(db) .fetch_all(db)
.await .await
.unwrap_or_default() .unwrap_or_default()
} }