45 lines
1010 B
Rust
Raw Normal View History

2024-11-27 21:50:28 +11:00
use poise::serenity_prelude::Token;
use tokio::sync::Mutex;
2024-11-27 21:50:28 +11:00
use std::{
str::FromStr,
sync::LazyLock
};
use tokenservice_client::{
TokenService,
TokenServiceApi
};
pub struct TSClient(TokenService);
impl Default for TSClient {
fn default() -> Self {
Self::new()
}
}
impl TSClient {
pub fn new() -> Self {
let args: Vec<String> = std::env::args().collect();
let service = if args.len() > 1 { &args[1] } else { "pgbot" };
Self(TokenService::new(service))
}
pub async fn get(&self) -> Result<TokenServiceApi, Box<dyn std::error::Error + Send + Sync>> {
match self.0.connect().await {
Ok(api) => Ok(api),
Err(e) => Err(e)
}
}
}
static TSCLIENT: LazyLock<Mutex<TSClient>> = LazyLock::new(|| Mutex::new(TSClient::new()));
pub async fn token_path() -> TokenServiceApi {
TSCLIENT.lock().await.get().await.unwrap()
}
2024-11-27 21:50:28 +11:00
pub async fn discord_token() -> Token {
Token::from_str(&token_path().await.main)
.expect("Serenity couldn't parse the bot token!")
}