feat; can now handle bedrock in the command

For #26
This commit is contained in:
silver 2024-11-30 00:44:36 +00:00
parent b55650b221
commit ee0c8f0987
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
4 changed files with 124 additions and 37 deletions

View file

@ -15,18 +15,29 @@ 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::{whitelist_update, Config, Minecraft, Wolves};
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("java-account")
.description("Is this a Java account?")
.kind(CommandOptionType::Boolean)
.required(false)
})
}
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
@ -61,12 +72,35 @@ 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;
}
}
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);
}
}
} 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);
}
},
}
}
@ -94,6 +128,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>(
"

View file

@ -31,6 +31,7 @@ pub struct Config {
// tokens for discord and other API's
pub discord_token: String,
pub discord_token_minecraft: String,
pub minecraft_mcprofile: String,
// email settings
pub mail_smtp: String,
@ -58,6 +59,7 @@ pub fn get_config() -> Config {
let mut config = Config {
discord_token: "".to_string(),
discord_token_minecraft: "".to_string(),
minecraft_mcprofile: "".to_string(),
home: ".".to_string(),
database: "database.db".to_string(),
@ -82,6 +84,9 @@ pub fn get_config() -> Config {
if let Ok(x) = env::var("DISCORD_TOKEN_MINECRAFT") {
config.discord_token_minecraft = x.trim().to_string();
}
if let Ok(x) = env::var("MINECRAFT_MCPROFILE_KEY") {
config.minecraft_mcprofile = x.trim().to_string();
}
if let Ok(x) = env::var("EMAIL_SMTP") {
config.mail_smtp = x.trim().to_string();