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
|
@ -14,7 +14,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};
|
||||
use skynet_discord_bot::{whitelist_update, Config, Wolves, get_minecraft_config_server, Minecraft};
|
||||
use sqlx::Error;
|
||||
|
||||
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
|
||||
if let Ok(servers) = get_servers(&db, &command.user.id).await {
|
||||
for server in servers {
|
||||
if let Some(server_minecraft) = server.server_minecraft {
|
||||
// activate the user on all linked servers
|
||||
whitelist_update(&vec![username.to_string()], &server_minecraft, &config.discord_minecraft).await;
|
||||
}
|
||||
whitelist_update(&vec![username.to_string()], &server.minecraft, &config.discord_minecraft).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -95,18 +92,17 @@ pub(crate) mod user {
|
|||
.await
|
||||
}
|
||||
|
||||
async fn get_servers(db: &Pool<Sqlite>, discord: &UserId) -> Result<Vec<Servers>, Error> {
|
||||
sqlx::query_as::<_, Servers>(
|
||||
async fn get_servers(db: &Pool<Sqlite>, discord: &UserId) -> Result<Vec<Minecraft>, Error> {
|
||||
sqlx::query_as::<_, Minecraft>(
|
||||
"
|
||||
SELECT servers.*
|
||||
FROM servers
|
||||
SELECT minecraft.*
|
||||
FROM minecraft
|
||||
JOIN (
|
||||
SELECT server
|
||||
FROM server_members
|
||||
JOIN wolves USING (id_wolves)
|
||||
WHERE discord = ?1
|
||||
) USING (server)
|
||||
WHERE server_minecraft IS NOT NULL
|
||||
) sub on minecraft.server_discord = sub.server
|
||||
",
|
||||
)
|
||||
.bind(*discord.as_u64() as i64)
|
||||
|
@ -153,9 +149,9 @@ pub(crate) mod server {
|
|||
.as_ref()
|
||||
.expect("Expected server_id object")
|
||||
{
|
||||
Some(id.to_owned())
|
||||
id.to_owned()
|
||||
} else {
|
||||
None
|
||||
return String::from("Expected Server ID");
|
||||
};
|
||||
|
||||
let db_lock = {
|
||||
|
@ -168,17 +164,16 @@ pub(crate) mod server {
|
|||
None => {
|
||||
return "No existing server config, have you used ``/add``?".to_string();
|
||||
}
|
||||
Some(mut x) => {
|
||||
x.server_minecraft.clone_from(&server_minecraft);
|
||||
Some(x) => {
|
||||
x
|
||||
}
|
||||
};
|
||||
|
||||
match add_server(&db, &server_data).await {
|
||||
match add_server(&db, *server_data.server.as_u64() as i64, &server_minecraft).await {
|
||||
Ok(_) => {}
|
||||
Err(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()
|
||||
}
|
||||
|
||||
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>(
|
||||
"
|
||||
UPDATE servers
|
||||
SET server_minecraft = ?2
|
||||
WHERE server = ?1;
|
||||
INSERT OR REPLACE INTO minecraft (server_discord, server_minecraft)
|
||||
VALUES (?1, ?2)
|
||||
",
|
||||
)
|
||||
.bind(*server.server.as_u64() as i64)
|
||||
.bind(&server.server_minecraft)
|
||||
.bind(discord)
|
||||
.bind(minecraft)
|
||||
.fetch_optional(db)
|
||||
.await
|
||||
}
|
||||
|
|
|
@ -63,9 +63,7 @@ pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> Stri
|
|||
role_past,
|
||||
role_current,
|
||||
member_past: 0,
|
||||
member_current: 0,
|
||||
// this gets added later
|
||||
server_minecraft: None,
|
||||
member_current: 0
|
||||
};
|
||||
|
||||
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_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>(
|
||||
"
|
||||
INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current, server_minecraft)
|
||||
VALUES (?1, ?2, ?3, ?4, ?5)
|
||||
INSERT OR REPLACE INTO servers (server, wolves_api, role_past, role_current)
|
||||
VALUES (?1, ?2, ?3, ?4)
|
||||
",
|
||||
)
|
||||
.bind(*server.server.as_u64() as i64)
|
||||
.bind(&server.wolves_api)
|
||||
.bind(role_past)
|
||||
.bind(role_current)
|
||||
.bind(server_minecraft)
|
||||
.fetch_optional(db)
|
||||
.await;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue