Initial v2.2.0 push
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
pub enum Colors {
|
||||
Info = 0x0773D6,
|
||||
Success = 0x3BD65D,
|
||||
Warning = 0xF0D932,
|
||||
Error = 0xFC1F28,
|
||||
None = 0,
|
||||
}
|
||||
|
||||
impl From<Colors> for poise::serenity_prelude::Colour {
|
||||
fn from(value: Colors) -> Self {
|
||||
Self(value as u32)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn escape(text: impl Into<String>) -> String {
|
||||
let text: String = text.into();
|
||||
|
||||
text.replace('\\', "\\\\")
|
||||
.replace('/', "\\/")
|
||||
.replace('*', "\\*")
|
||||
.replace('_', "\\_")
|
||||
.replace('~', "\\~")
|
||||
.replace('`', "\\`")
|
||||
// Prevent markdown links
|
||||
.replace('[', "\\[")
|
||||
.replace(']', "\\]")
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
pub mod discord;
|
||||
pub mod spotify;
|
||||
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
pub fn get_time() -> u128 {
|
||||
let now = SystemTime::now();
|
||||
let since_the_epoch = now.duration_since(UNIX_EPOCH).expect("Time went backwards");
|
||||
|
||||
since_the_epoch.as_millis()
|
||||
}
|
||||
|
||||
pub fn time_to_string(time: u32) -> String {
|
||||
let hour = 3600;
|
||||
let min = 60;
|
||||
|
||||
if time / hour >= 1 {
|
||||
format!(
|
||||
"{}h{}m{}s",
|
||||
time / hour,
|
||||
(time % hour) / min,
|
||||
(time % hour) % min
|
||||
)
|
||||
} else if time / min >= 1 {
|
||||
format!("{}m{}s", time / min, time % min)
|
||||
} else {
|
||||
format!("{}s", time)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
use anyhow::Result;
|
||||
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
|
||||
use librespot::{
|
||||
core::{connection::AuthenticationError, Session, SessionConfig},
|
||||
discovery::Credentials,
|
||||
protocol::{authentication::AuthenticationType, keyexchange::ErrorCode},
|
||||
};
|
||||
use log::trace;
|
||||
|
||||
pub async fn validate_token(
|
||||
username: impl Into<String>,
|
||||
token: impl Into<String>,
|
||||
) -> Result<Option<String>> {
|
||||
let auth_data = BASE64.decode(token.into())?;
|
||||
|
||||
let credentials = Credentials {
|
||||
username: username.into(),
|
||||
auth_type: AuthenticationType::AUTHENTICATION_STORED_SPOTIFY_CREDENTIALS,
|
||||
auth_data,
|
||||
};
|
||||
|
||||
trace!("Validating session token for {}", credentials.username);
|
||||
|
||||
let new_credentials = request_session_token(credentials.clone()).await?;
|
||||
|
||||
if credentials.auth_data != new_credentials.auth_data {
|
||||
trace!("New session token retrieved for {}", credentials.username);
|
||||
|
||||
return Ok(Some(BASE64.encode(new_credentials.auth_data)));
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
pub async fn request_session_token(credentials: Credentials) -> Result<Credentials> {
|
||||
trace!("Requesting session token for {}", credentials.username);
|
||||
|
||||
let session = Session::new(SessionConfig::default(), None);
|
||||
let mut tries = 0;
|
||||
|
||||
Ok(loop {
|
||||
let (host, port) = session.apresolver().resolve("accesspoint").await?;
|
||||
let mut transport = librespot::core::connection::connect(&host, port, None).await?;
|
||||
|
||||
match librespot::core::connection::authenticate(
|
||||
&mut transport,
|
||||
credentials.clone(),
|
||||
&session.config().device_id,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(creds) => break creds,
|
||||
Err(e) => {
|
||||
if let Some(AuthenticationError::LoginFailed(ErrorCode::TryAnotherAP)) =
|
||||
e.error.downcast_ref::<AuthenticationError>()
|
||||
{
|
||||
tries += 1;
|
||||
if tries > 3 {
|
||||
return Err(e.into());
|
||||
}
|
||||
|
||||
continue;
|
||||
} else {
|
||||
return Err(e.into());
|
||||
}
|
||||
}
|
||||
};
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user