feat: added support for multiple minecraft servers per discord server
Closes #9
This commit is contained in:
parent
c446c10f2d
commit
0f774258a1
4 changed files with 66 additions and 53 deletions
|
@ -1,4 +1,5 @@
|
||||||
use skynet_discord_bot::{db_init, get_config, get_server_config_bulk, update_server, whitelist_wipe};
|
use std::collections::HashSet;
|
||||||
|
use skynet_discord_bot::{db_init, get_config, get_minecraft_config, update_server, whitelist_wipe};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
@ -8,16 +9,17 @@ async fn main() {
|
||||||
Err(_) => return,
|
Err(_) => return,
|
||||||
};
|
};
|
||||||
|
|
||||||
let servers = get_server_config_bulk(&db).await;
|
let servers = get_minecraft_config(&db).await;
|
||||||
|
let mut wiped = HashSet::new();
|
||||||
|
|
||||||
|
for server in &servers {
|
||||||
// wipe whitelist first
|
// wipe whitelist first
|
||||||
for server_config in &servers {
|
if !wiped.contains(&server.minecraft) {
|
||||||
if let Some(server_id) = &server_config.server_minecraft {
|
whitelist_wipe(&server.minecraft, &config.discord_minecraft).await;
|
||||||
whitelist_wipe(server_id, &config.discord_minecraft).await;
|
// add it to teh done list so its not done again
|
||||||
}
|
wiped.insert(&server.minecraft);
|
||||||
}
|
}
|
||||||
|
|
||||||
for server_config in &servers {
|
update_server(&server.minecraft, &db, &server.discord, &config).await;
|
||||||
update_server(&server_config.server_minecraft, &db, &server_config.server, &config).await;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,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};
|
use skynet_discord_bot::{whitelist_update, Config, Wolves, get_minecraft_config_server, Minecraft};
|
||||||
use sqlx::Error;
|
use sqlx::Error;
|
||||||
|
|
||||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||||
|
@ -71,10 +71,7 @@ pub(crate) mod user {
|
||||||
// get a list of servers that the user is a member of
|
// get a list of servers that the user is a member of
|
||||||
if let Ok(servers) = get_servers(&db, &command.user.id).await {
|
if let Ok(servers) = get_servers(&db, &command.user.id).await {
|
||||||
for server in servers {
|
for server in servers {
|
||||||
if let Some(server_minecraft) = server.server_minecraft {
|
whitelist_update(&vec![username.to_string()], &server.minecraft, &config.discord_minecraft).await;
|
||||||
// activate the user on all linked servers
|
|
||||||
whitelist_update(&vec![username.to_string()], &server_minecraft, &config.discord_minecraft).await;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,18 +92,17 @@ pub(crate) mod user {
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_servers(db: &Pool<Sqlite>, discord: &UserId) -> Result<Vec<Servers>, Error> {
|
async fn get_servers(db: &Pool<Sqlite>, discord: &UserId) -> Result<Vec<Minecraft>, Error> {
|
||||||
sqlx::query_as::<_, Servers>(
|
sqlx::query_as::<_, Minecraft>(
|
||||||
"
|
"
|
||||||
SELECT servers.*
|
SELECT minecraft.*
|
||||||
FROM servers
|
FROM minecraft
|
||||||
JOIN (
|
JOIN (
|
||||||
SELECT server
|
SELECT server
|
||||||
FROM server_members
|
FROM server_members
|
||||||
JOIN wolves USING (id_wolves)
|
JOIN wolves USING (id_wolves)
|
||||||
WHERE discord = ?1
|
WHERE discord = ?1
|
||||||
) USING (server)
|
) sub on minecraft.server_discord = sub.server
|
||||||
WHERE server_minecraft IS NOT NULL
|
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(*discord.as_u64() as i64)
|
.bind(*discord.as_u64() as i64)
|
||||||
|
@ -153,9 +149,9 @@ pub(crate) mod server {
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.expect("Expected server_id object")
|
.expect("Expected server_id object")
|
||||||
{
|
{
|
||||||
Some(id.to_owned())
|
id.to_owned()
|
||||||
} else {
|
} else {
|
||||||
None
|
return String::from("Expected Server ID");
|
||||||
};
|
};
|
||||||
|
|
||||||
let db_lock = {
|
let db_lock = {
|
||||||
|
@ -168,17 +164,16 @@ pub(crate) mod server {
|
||||||
None => {
|
None => {
|
||||||
return "No existing server config, have you used ``/add``?".to_string();
|
return "No existing server config, have you used ``/add``?".to_string();
|
||||||
}
|
}
|
||||||
Some(mut x) => {
|
Some(x) => {
|
||||||
x.server_minecraft.clone_from(&server_minecraft);
|
|
||||||
x
|
x
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match add_server(&db, &server_data).await {
|
match add_server(&db, *server_data.server.as_u64() as i64, &server_minecraft).await {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("{:?}", e);
|
println!("{:?}", e);
|
||||||
return format!("Failure to insert into Servers {:?}", server_data);
|
return format!("Failure to insert into Minecraft {} {}", *server_data.server.as_u64(), &server_minecraft);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,16 +188,15 @@ pub(crate) mod server {
|
||||||
"Added/Updated minecraft_server info".to_string()
|
"Added/Updated minecraft_server info".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn add_server(db: &Pool<Sqlite>, server: &Servers) -> Result<Option<Servers>, Error> {
|
async fn add_server(db: &Pool<Sqlite>, discord: i64, minecraft: &str ) -> Result<Option<Servers>, Error> {
|
||||||
sqlx::query_as::<_, Servers>(
|
sqlx::query_as::<_, Servers>(
|
||||||
"
|
"
|
||||||
UPDATE servers
|
INSERT OR REPLACE INTO minecraft (server_discord, server_minecraft)
|
||||||
SET server_minecraft = ?2
|
VALUES (?1, ?2)
|
||||||
WHERE server = ?1;
|
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(*server.server.as_u64() as i64)
|
.bind(discord)
|
||||||
.bind(&server.server_minecraft)
|
.bind(minecraft)
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,9 +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
|
||||||
// this gets added later
|
|
||||||
server_minecraft: None,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
match add_server(&db, ctx, &server_data).await {
|
match add_server(&db, ctx, &server_data).await {
|
||||||
|
@ -111,22 +109,16 @@ async fn add_server(db: &Pool<Sqlite>, ctx: &Context, server: &Servers) -> Resul
|
||||||
let role_past = server.role_past.map(|x| *x.as_u64() as i64);
|
let role_past = server.role_past.map(|x| *x.as_u64() as i64);
|
||||||
let role_current = server.role_current.map(|x| *x.as_u64() as i64);
|
let role_current = server.role_current.map(|x| *x.as_u64() as i64);
|
||||||
|
|
||||||
let server_minecraft = match get_server_config(db, &server.server).await {
|
|
||||||
None => None,
|
|
||||||
Some(x) => x.server_minecraft,
|
|
||||||
};
|
|
||||||
|
|
||||||
let insert = sqlx::query_as::<_, Servers>(
|
let insert = sqlx::query_as::<_, Servers>(
|
||||||
"
|
"
|
||||||
INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current, server_minecraft)
|
INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current)
|
||||||
VALUES (?1, ?2, ?3, ?4, ?5)
|
VALUES (?1, ?2, ?3, ?4)
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(*server.server.as_u64() as i64)
|
.bind(*server.server.as_u64() as i64)
|
||||||
.bind(&server.wolves_api)
|
.bind(&server.wolves_api)
|
||||||
.bind(role_past)
|
.bind(role_past)
|
||||||
.bind(role_current)
|
.bind(role_current)
|
||||||
.bind(server_minecraft)
|
|
||||||
.fetch_optional(db)
|
.fetch_optional(db)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
|
|
31
src/lib.rs
31
src/lib.rs
|
@ -697,8 +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_minecraft: &Option<String>, db: &Pool<Sqlite>, g_id: &GuildId, config: &Config) {
|
pub async fn update_server(server_id: &String, db: &Pool<Sqlite>, g_id: &GuildId, config: &Config) {
|
||||||
if let Some(server_id) = server_minecraft {
|
|
||||||
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 {
|
||||||
|
@ -709,7 +708,6 @@ pub async fn update_server(server_minecraft: &Option<String>, db: &Pool<Sqlite>,
|
||||||
whitelist_update(&usernames, server_id, &config.discord_minecraft).await;
|
whitelist_update(&usernames, server_id, &config.discord_minecraft).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async fn post<T: Serialize>(url: &str, bearer: &str, data: &T) {
|
async fn post<T: Serialize>(url: &str, bearer: &str, data: &T) {
|
||||||
match surf::post(url)
|
match surf::post(url)
|
||||||
|
@ -770,3 +768,30 @@ pub async fn whitelist_wipe(server: &str, token: &str) {
|
||||||
};
|
};
|
||||||
post(&format!("{url_base}/command"), &bearer, &data).await;
|
post(&format!("{url_base}/command"), &bearer, &data).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub async fn get_minecraft_config(db: &Pool<Sqlite>) -> Vec<Minecraft> {
|
||||||
|
sqlx::query_as::<_, Minecraft>(
|
||||||
|
r#"
|
||||||
|
SELECT *
|
||||||
|
FROM minecraft
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.fetch_all(db)
|
||||||
|
.await
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_minecraft_config_server(db: &Pool<Sqlite>, g_id: GuildId) -> Option<Minecraft> {
|
||||||
|
sqlx::query_as::<_, Minecraft>(
|
||||||
|
r#"
|
||||||
|
SELECT *
|
||||||
|
FROM minecraft
|
||||||
|
WHERE server_discord = ?1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(*g_id.as_u64() as i64)
|
||||||
|
.fetch_optional(db)
|
||||||
|
.await
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
Loading…
Reference in a new issue