forked from Skynet/discord-bot
fmt: clippy and fmt
This commit is contained in:
parent
9481358068
commit
55b2e534d4
5 changed files with 67 additions and 73 deletions
|
@ -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() {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<Sqlite>, discord: &UserId) -> Result<Vec<Minecraft>, 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::<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();
|
||||
}
|
||||
|
@ -238,14 +233,17 @@ pub(crate) mod server {
|
|||
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 {
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
pub mod minecraft;
|
||||
pub mod add_server;
|
||||
pub mod link_email;
|
||||
pub mod minecraft;
|
||||
|
|
39
src/lib.rs
39
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<Self, Error> {
|
||||
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<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![];
|
||||
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<T: Serialize>(url: &str, bearer: &str) -> Option<T> {
|
||||
async fn get<T: Serialize + DeserializeOwned>(url: &str, bearer: &str) -> Option<T> {
|
||||
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<ServerDetai
|
|||
get::<ServerDetailsRes>(&format!("{url_base}/"), &bearer).await
|
||||
}
|
||||
|
||||
|
||||
pub async fn get_minecraft_config(db: &Pool<Sqlite>) -> Vec<Minecraft> {
|
||||
sqlx::query_as::<_, Minecraft>(
|
||||
r#"
|
||||
|
@ -812,12 +811,12 @@ pub async fn get_minecraft_config(db: &Pool<Sqlite>) -> Vec<Minecraft> {
|
|||
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<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>(
|
||||
r#"
|
||||
SELECT *
|
||||
|
@ -825,8 +824,8 @@ pub async fn get_minecraft_config_server(db: &Pool<Sqlite>, g_id: GuildId) -> V
|
|||
WHERE server_discord = ?1
|
||||
"#,
|
||||
)
|
||||
.bind(*g_id.as_u64() as i64)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap_or_default()
|
||||
}
|
||||
.bind(*g_id.as_u64() as i64)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue