From 4b03762390d019359794dff0503b39faabe8c437 Mon Sep 17 00:00:00 2001 From: Stachelbeere1248 Date: Sat, 28 Dec 2024 03:57:52 +0100 Subject: [PATCH] minor error message improvements --- src/commands/accountv2.rs | 82 ++++++++++++++++++++++----------------- src/error.rs | 6 +-- 2 files changed, 50 insertions(+), 38 deletions(-) diff --git a/src/commands/accountv2.rs b/src/commands/accountv2.rs index fc6d27e..3e5ae2d 100644 --- a/src/commands/accountv2.rs +++ b/src/commands/accountv2.rs @@ -1,30 +1,29 @@ -use std::ops::Add; use poise::CreateReply; use reqwest::{Client, Response}; use serde::Deserialize; -use serenity::{ - all::{ChannelId, CreateActionRow, CreateAllowedMentions, CreateButton, CreateMessage, ReactionType, User}, -}; -use serenity::all::{ButtonStyle}; -use sqlx::{Pool, query_as, Sqlite}; +use serenity::all::ButtonStyle; +use serenity::all::{ChannelId, CreateActionRow, CreateAllowedMentions, CreateButton, CreateMessage, ReactionType, User}; +use sqlx::{query_as, Pool, Sqlite}; +use std::ops::Add; use crate::commands::command_helper::cooldown; -use crate::Context; use crate::error::Error; +use crate::error::Error::Other; +use crate::Context; #[derive(Deserialize)] struct Links { #[serde(rename = "DISCORD")] - pub discord: String, + pub discord: Option, } #[derive(Deserialize)] struct SocialMedia { - pub links: Links, + pub links: Option, } #[derive(Deserialize)] struct HypixelPlayer { #[serde(rename = "socialMedia")] - pub social_media: SocialMedia, + pub social_media: Option, } #[derive(Deserialize)] struct HypixelResponse { @@ -49,9 +48,7 @@ impl Uuid { let url: String = format!("https://api.mojang.com/users/profiles/minecraft/{ign}"); let response_400: Response = reqwest::get(url).await?.error_for_status()?; let deserialized = response_400.json::().await?; - let uuid = Uuid { - uuid: deserialized.id, - }; + let uuid = Uuid { uuid: deserialized.id }; Ok(uuid) } @@ -71,7 +68,18 @@ impl Uuid { let url: String = format!("https://api.hypixel.net/v2/player?uuid={}", self.uuid); let response_400: Response = client.get(url).send().await?.error_for_status()?; let deserialized = response_400.json::().await?; - let matches = deserialized.player.social_media.links.discord == user.name; + let matches = deserialized + .player + .social_media + .map(|sm| sm.links) + .flatten() + .map(|l| l.discord) + .flatten() + .ok_or(Other(format!( + "The Hypixel profile has no Discord account linked. Please follow the steps in {}", + ChannelId::new(1256219552568840263_u64) + )))? + == user.name; Ok(matches) } } @@ -127,7 +135,7 @@ pub(crate) async fn account(_ctx: Context<'_>) -> Result<(), Error> { slash_command, install_context = "User|Guild", interaction_context = "Guild|BotDm|PrivateChannel", - ephemeral = "false", + ephemeral = "false" )] /// Verify a Minecraft account on the Zombies MultiPlayer Discord. pub(crate) async fn add<'a>( @@ -140,10 +148,15 @@ pub(crate) async fn add<'a>( #[description = "admin-only"] force: Option, ) -> Result<(), Error> { ctx.defer().await?; + let force: bool = force.unwrap_or(false) && ctx.framework().options.owners.contains(&ctx.author().id) && { + let _ = user.as_ref().ok_or(Other( + "Warning: attempted to run forced account add without specifying a target Discord account.".to_string(), + ))?; + true + }; let user: User = user.unwrap_or(ctx.author().clone()); let uuid: Uuid = Uuid::for_ign(ign.as_str()).await?; - let force: bool = force.unwrap_or(false) && ctx.framework().options.owners.contains(&ctx.author().id); - match uuid.has_discord_user(&user, &ctx.data().hypixel_api_client).await? || force { + match force || uuid.has_discord_user(&user, &ctx.data().hypixel_api_client).await? { true => { let pool = &ctx.data().sqlite_pool; let status: &str = match link_id_from_minecraft(pool, uuid.get()).await { @@ -176,23 +189,21 @@ pub(crate) async fn add<'a>( sqlx::query( format!( "UPDATE minecraft_links SET link_id = {} WHERE link_id = {};", - mc_id.inner, - dc_id.inner + mc_id.inner, dc_id.inner ) - .as_str(), + .as_str(), ) - .execute(pool) - .await?; + .execute(pool) + .await?; sqlx::query( format!( "UPDATE discord_links SET link_id = {} WHERE link_id = {};", - mc_id.inner, - dc_id.inner + mc_id.inner, dc_id.inner ) - .as_str(), + .as_str(), ) - .execute(pool) - .await?; + .execute(pool) + .await?; "Both your Discord and Minecraft account had linked accounts. Merged all account links." } }, @@ -233,7 +244,7 @@ pub(crate) async fn add<'a>( install_context = "User|Guild", interaction_context = "Guild|BotDm|PrivateChannel", ephemeral = "true", - context_menu_command="Account list" + context_menu_command = "Account list" )] /// List a users linked minecraft Accounts. pub(crate) async fn list(ctx: Context<'_>, user: User) -> Result<(), Error> { @@ -244,8 +255,9 @@ pub(crate) async fn list(ctx: Context<'_>, user: User) -> Result<(), Error> { ctx.send( CreateReply::default() .content(s) - .allowed_mentions(CreateAllowedMentions::new().empty_roles().empty_users()) - ).await?; + .allowed_mentions(CreateAllowedMentions::new().empty_roles().empty_users()), + ) + .await?; Ok(()) } @@ -288,11 +300,11 @@ async fn link_id_from_discord(pool: &Pool, snowflake: u64) -> Option for LinkId { fn from(unsigned: u16) -> Self { Self { - inner: unsigned.cast_signed() + inner: unsigned.cast_signed(), } } } diff --git a/src/error.rs b/src/error.rs index 8eda55c..65553d6 100644 --- a/src/error.rs +++ b/src/error.rs @@ -14,7 +14,7 @@ macro_rules! reply_fail_handler { #[derive(Debug)] pub enum Error { SqlxError(sqlx::Error), - HttpsError(reqwest::Error), + ApiError(reqwest::Error), SerenityError(serenity::Error), OnCooldown(std::time::Duration), Other(String), @@ -24,7 +24,7 @@ impl Display for Error { fn fmt(&self, f: &mut Formatter) -> FmtResult { match self { Error::SqlxError(e) => write!(f, "SQLx Error: {}", e), - Error::HttpsError(e) => write!(f, "HTTPS Error (Hypixel / Mojang API):\n{}", e), + Error::ApiError(e) => write!(f, "HTTPS Error (Hypixel / Mojang API):\n{}", e), Error::SerenityError(e) => write!(f, "Discord Error:\n {}", e), Error::OnCooldown(d) => write!(f, "This command is on cooldown. {}s remaining.", d.as_secs()), Error::Other(s) => write!(f, "{}", s), @@ -40,7 +40,7 @@ impl From for Error { impl From for Error { fn from(error: reqwest::Error) -> Self { - Error::HttpsError(error) + Error::ApiError(error) } }