Added bot api

+ Right now can just play voices
+ Added commands to add api keys
+ clippy + fmt
This commit is contained in:
2023-01-21 15:36:14 -07:00
parent 6896ee610c
commit 41213db8d4
10 changed files with 396 additions and 39 deletions
+70
View File
@@ -0,0 +1,70 @@
use crate::error::Error;
use base64::{engine::general_purpose, Engine as _};
use j_db::database::Database;
use j_db::model::JdbModel;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
use serenity::model::id::UserId;
use sha3::digest::FixedOutput;
use sha3::{Digest, Sha3_256};
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Apikey {
id: Option<u64>,
pub name: String,
pub hash: String,
pub user_id: Option<UserId>,
}
impl JdbModel for Apikey {
fn id(&self) -> Option<u64> {
self.id
}
fn set_id(&mut self, id: u64) {
self.id = Some(id)
}
fn tree() -> String {
"ApiKey".to_string()
}
}
impl Apikey {
pub fn new(name: &str, user_id: Option<UserId>) -> (Self, String) {
let secret: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(64)
.map(char::from)
.collect();
let hash = Self::hash_secret(&secret);
(
Self {
id: None,
name: name.to_string(),
hash,
user_id,
},
secret,
)
}
fn hash_secret(secret: &str) -> String {
let mut hasher = Sha3_256::default();
hasher.update(secret);
let hash = hasher.finalize_fixed().to_vec();
general_purpose::STANDARD_NO_PAD.encode(hash)
}
pub fn find_key_from_secret(db: &Database, secret: &str) -> Result<Option<Self>, Error> {
let hash = Self::hash_secret(secret);
Ok(db.filter(move |_, key: &Self| key.hash == hash)?.next())
}
}
+1
View File
@@ -1,4 +1,5 @@
pub mod album;
pub mod api_key;
pub mod insult_compliment;
pub mod motivation;
pub mod random;