Merge branch 'main' into #17-automate-onboarding-mk-ii
# Conflicts: # Cargo.lock # src/commands/link_email.rs # src/lib.rs
This commit is contained in:
commit
c79113921d
15 changed files with 1013 additions and 141 deletions
|
@ -15,6 +15,7 @@ pub(crate) mod user {
|
|||
pub(crate) mod add {
|
||||
use super::*;
|
||||
use crate::commands::link_email::link::get_server_member_discord;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serenity::model::id::UserId;
|
||||
use skynet_discord_bot::common::database::Wolves;
|
||||
use skynet_discord_bot::common::minecraft::{whitelist_update, Minecraft};
|
||||
|
@ -22,13 +23,23 @@ pub(crate) mod user {
|
|||
use sqlx::Error;
|
||||
|
||||
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
|
||||
command.name("link_minecraft").description("Link your minecraft account").create_option(|option| {
|
||||
option
|
||||
.name("minecraft-username")
|
||||
.description("Your Minecraft username")
|
||||
.kind(CommandOptionType::String)
|
||||
.required(true)
|
||||
})
|
||||
command
|
||||
.name("link_minecraft")
|
||||
.description("Link your minecraft account")
|
||||
.create_option(|option| {
|
||||
option
|
||||
.name("minecraft_username")
|
||||
.description("Your Minecraft username")
|
||||
.kind(CommandOptionType::String)
|
||||
.required(true)
|
||||
})
|
||||
.create_option(|option| {
|
||||
option
|
||||
.name("bedrock_account")
|
||||
.description("Is this a Bedrock account?")
|
||||
.kind(CommandOptionType::Boolean)
|
||||
.required(false)
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
|
||||
|
@ -63,19 +74,48 @@ pub(crate) mod user {
|
|||
return "Please provide a valid username".to_string();
|
||||
};
|
||||
|
||||
// insert the username into the database
|
||||
match add_minecraft(&db, &command.user.id, username).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
dbg!("{:?}", e);
|
||||
return format!("Failure to minecraft username {:?}", username);
|
||||
// this is always true unless they state its not
|
||||
let mut java = true;
|
||||
if let Some(x) = command.data.options.get(1) {
|
||||
if let Some(CommandDataOptionValue::Boolean(z)) = x.to_owned().resolved {
|
||||
java = !z;
|
||||
}
|
||||
}
|
||||
|
||||
let username_mc;
|
||||
if java {
|
||||
// insert the username into the database
|
||||
match add_minecraft(&db, &command.user.id, username).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
dbg!("{:?}", e);
|
||||
return format!("Failure to minecraft username {:?}", username);
|
||||
}
|
||||
}
|
||||
username_mc = username.to_string();
|
||||
} else {
|
||||
match get_minecraft_bedrock(username, &config.minecraft_mcprofile).await {
|
||||
None => {
|
||||
return format!("No UID found for {:?}", username);
|
||||
}
|
||||
Some(x) => {
|
||||
match add_minecraft_bedrock(&db, &command.user.id, &x.floodgateuid).await {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
dbg!("{:?}", e);
|
||||
return format!("Failure to minecraft UID {:?}", &x.floodgateuid);
|
||||
}
|
||||
}
|
||||
|
||||
username_mc = x.floodgateuid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
whitelist_update(&vec![username.to_string()], &server.minecraft, &config.discord_token_minecraft).await;
|
||||
whitelist_update(&vec![(username_mc.to_owned(), java)], &server.minecraft, &config.discord_token_minecraft).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -96,6 +136,51 @@ pub(crate) mod user {
|
|||
.await
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
struct BedrockDetails {
|
||||
pub gamertag: String,
|
||||
pub xuid: String,
|
||||
pub floodgateuid: String,
|
||||
pub icon: String,
|
||||
pub gamescore: String,
|
||||
pub accounttier: String,
|
||||
pub textureid: String,
|
||||
pub skin: String,
|
||||
pub linked: bool,
|
||||
pub java_uuid: String,
|
||||
pub java_name: String,
|
||||
}
|
||||
|
||||
async fn get_minecraft_bedrock(username: &str, api_key: &str) -> Option<BedrockDetails> {
|
||||
let url = format!("https://mcprofile.io/api/v1/bedrock/gamertag/{username}/");
|
||||
match surf::get(url)
|
||||
.header("x-api-key", api_key)
|
||||
.header("User-Agent", "UL Computer Society")
|
||||
.recv_json()
|
||||
.await
|
||||
{
|
||||
Ok(res) => Some(res),
|
||||
Err(e) => {
|
||||
dbg!(e);
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn add_minecraft_bedrock(db: &Pool<Sqlite>, user: &UserId, minecraft: &str) -> Result<Option<Wolves>, Error> {
|
||||
sqlx::query_as::<_, Wolves>(
|
||||
"
|
||||
UPDATE wolves
|
||||
SET minecraft_uid = ?2
|
||||
WHERE discord = ?1;
|
||||
",
|
||||
)
|
||||
.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> {
|
||||
sqlx::query_as::<_, Minecraft>(
|
||||
"
|
||||
|
@ -249,7 +334,7 @@ pub(crate) mod server {
|
|||
ID: {id}
|
||||
Online: {online}
|
||||
Info: {description}
|
||||
Link: <http://panel.games.skynet.ie/server/{id}>
|
||||
Link: <https://panel.games.skynet.ie/server/{id}>
|
||||
"#,
|
||||
name = &x.attributes.name,
|
||||
online = !x.attributes.is_suspended,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue