feat: now using proper types for the guild and user ID's
This commit is contained in:
parent
ca6ae993c5
commit
6b08f82e2c
4 changed files with 85 additions and 45 deletions
82
src/lib.rs
82
src/lib.rs
|
@ -1,15 +1,18 @@
|
|||
use dotenvy::dotenv;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serenity::{
|
||||
model::{guild, id::GuildId},
|
||||
model::{
|
||||
guild,
|
||||
id::{GuildId, RoleId},
|
||||
},
|
||||
prelude::TypeMapKey,
|
||||
};
|
||||
|
||||
use sqlx::{
|
||||
sqlite::{SqliteConnectOptions, SqlitePoolOptions},
|
||||
Error, Pool, Sqlite,
|
||||
sqlite::{SqliteConnectOptions, SqlitePoolOptions, SqliteRow},
|
||||
Error, FromRow, Pool, Row, Sqlite,
|
||||
};
|
||||
use std::str::FromStr;
|
||||
use std::{env, sync::Arc};
|
||||
use std::{env, str::FromStr, sync::Arc};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
pub struct Config {
|
||||
|
@ -99,9 +102,9 @@ fn str_to_num<T: FromStr + Default>(x: &str) -> T {
|
|||
x.trim().parse::<T>().unwrap_or_default()
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Accounts {
|
||||
pub server: i64,
|
||||
pub server: GuildId,
|
||||
pub wolves_id: String,
|
||||
pub email: String,
|
||||
pub expiry: String,
|
||||
|
@ -109,15 +112,60 @@ pub struct Accounts {
|
|||
pub minecraft: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, sqlx::FromRow)]
|
||||
impl<'r> FromRow<'r, SqliteRow> for Accounts {
|
||||
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
|
||||
let server_tmp: i64 = row.try_get("server")?;
|
||||
let server = GuildId::from(server_tmp as u64);
|
||||
|
||||
Ok(Self {
|
||||
server,
|
||||
wolves_id: row.try_get("wolves_api")?,
|
||||
email: row.try_get("email")?,
|
||||
expiry: row.try_get("expiry")?,
|
||||
discord: row.try_get("discord")?,
|
||||
minecraft: row.try_get("minecraft")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Servers {
|
||||
pub server: i64,
|
||||
pub server: GuildId,
|
||||
pub wolves_api: String,
|
||||
pub role_past: Option<i64>,
|
||||
pub role_current: Option<i64>,
|
||||
pub role_past: Option<RoleId>,
|
||||
pub role_current: Option<RoleId>,
|
||||
pub member_past: i64,
|
||||
pub member_current: i64,
|
||||
}
|
||||
impl<'r> FromRow<'r, SqliteRow> for Servers {
|
||||
fn from_row(row: &'r SqliteRow) -> Result<Self, Error> {
|
||||
let server_tmp: i64 = row.try_get("server")?;
|
||||
let server = GuildId::from(server_tmp as u64);
|
||||
let role_past = match row.try_get("role_past") {
|
||||
Ok(x) => {
|
||||
let tmp: i64 = x;
|
||||
Some(RoleId::from(tmp as u64))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
let role_current = match row.try_get("role_current") {
|
||||
Ok(x) => {
|
||||
let tmp: i64 = x;
|
||||
Some(RoleId::from(tmp as u64))
|
||||
}
|
||||
_ => None,
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
server,
|
||||
wolves_api: row.try_get("wolves_api")?,
|
||||
role_past,
|
||||
role_current,
|
||||
member_past: row.try_get("member_past")?,
|
||||
member_current: row.try_get("member_current")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
||||
let database = format!("{}/{}", &config.home, &config.database);
|
||||
|
@ -190,3 +238,15 @@ pub async fn get_server_member(db: &Pool<Sqlite>, server: &GuildId, member: &gui
|
|||
.await
|
||||
.ok()
|
||||
}
|
||||
|
||||
pub async fn get_server_config_bulk(db: &Pool<Sqlite>) -> Vec<Servers> {
|
||||
sqlx::query_as::<_, Servers>(
|
||||
r#"
|
||||
SELECT *
|
||||
FROM servers
|
||||
"#,
|
||||
)
|
||||
.fetch_all(db)
|
||||
.await
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue