feat(poll api): improve error handling (#52)

This commit is contained in:
Ax333l 2023-01-09 21:34:56 +01:00 committed by GitHub
parent 741b3e7347
commit 5229cd6654
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 21 deletions

View File

@ -40,7 +40,7 @@ impl Api {
*req.headers_mut() = headers.clone(); *req.headers_mut() = headers.clone();
} }
client.execute(req).await?.json::<T>().await client.execute(req).await?.error_for_status()?.json::<T>().await
} }
pub async fn authenticate( pub async fn authenticate(

View File

@ -1,7 +1,8 @@
use poise::serenity_prelude::{ButtonStyle, ReactionType, Timestamp}; use poise::serenity_prelude::{ButtonStyle, ReactionType, Timestamp};
use reqwest::StatusCode;
use sha3::{Digest, Sha3_256}; use sha3::{Digest, Sha3_256};
use tracing::log::{error, info, trace}; use tracing::log::{error, trace};
use super::bot::get_data_lock; use super::bot::get_data_lock;
use super::*; use super::*;
@ -22,28 +23,32 @@ pub async fn handle_poll(
let member = component.member.as_ref().unwrap(); let member = component.member.as_ref().unwrap();
let eligible = member.joined_at.unwrap() <= min_join_date; let eligible = member.joined_at.unwrap() <= min_join_date;
let auth_token = if eligible { let result = if eligible {
let mut hasher = Sha3_256::new(); let mut hasher = Sha3_256::new();
hasher.update(&member.user.id.to_string()); hasher.update(&member.user.id.to_string());
let result = data match data
.api .api
// We cannot use the entire hash because Discord rejects URLs with more than 512 characters. // We cannot use the entire hash because Discord rejects URLs with more than 512 characters.
.authenticate(&hex::encode(hasher.finalize())[..2^5]) .authenticate(&hex::encode(hasher.finalize())[..2^5])
.await .await
.map(|auth| auth.access_token); {
Ok(auth) => Ok(auth.access_token),
if let Err(ref e) = result { Err(err) => match err.status() {
error!("API Request error: {}", e) Some(StatusCode::PRECONDITION_FAILED) => Err("You can only vote once."),
_ => {
error!("API Request error: {:?}", err);
Err("API Request failed. Please try again later.")
},
},
} }
result.ok()
} else { } else {
None Err("You are not eligible to vote on this poll.")
}; };
component component
.create_interaction_response(&ctx.http, |r| { .create_interaction_response(&ctx.http, |r| {
r.interaction_response_data(|m| { r.interaction_response_data(|m| {
if let Some(token) = auth_token.as_deref() { if let Ok(token) = result.as_deref() {
let url = format!("https://revanced.app/polling#{}", token); let url = format!("https://revanced.app/polling#{}", token);
m.components(|c| { m.components(|c| {
c.create_action_row(|r| { c.create_action_row(|r| {
@ -60,16 +65,11 @@ pub async fn handle_poll(
} }
.ephemeral(true) .ephemeral(true)
.embed(|e| { .embed(|e| {
if auth_token.is_some() { match result {
e.title("Cast your vote") Ok(_) => e
.description("You can now vote on the poll.") .title("Cast your vote")
} else if !eligible { .description("You can now vote on the poll."),
info!("Member {} failed to vote.", member.display_name()); Err(msg) => e.title("Error").description(msg),
e.title("You can not vote")
.description("You are not eligible to vote on this poll.")
} else {
e.title("Error")
.description("An error has occured. Please try again later.")
} }
.color(data.configuration.general.embed_color) .color(data.configuration.general.embed_color)
.thumbnail(member.user.face()) .thumbnail(member.user.face())