2024-06-27 21:22:44 +02:00
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use poise::serenity_prelude::UserId;
|
2024-08-08 15:59:36 +02:00
|
|
|
use sqlx::SqlitePool;
|
2024-06-27 21:22:44 +02:00
|
|
|
|
2024-06-22 16:40:56 +02:00
|
|
|
use crate::context::{Context, Error};
|
|
|
|
|
2024-06-22 23:07:11 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2024-06-22 16:40:56 +02:00
|
|
|
pub struct User {
|
|
|
|
pub id: u32,
|
|
|
|
pub discord_id: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl User {
|
|
|
|
pub async fn from_context(ctx: &Context<'_>) -> Result<Self, Error> {
|
|
|
|
let id = ctx.author().id.get().to_string();
|
2024-06-22 23:07:11 +02:00
|
|
|
let user = sqlx::query!("SELECT * FROM users WHERE discord_id = ?", id)
|
2024-06-22 16:40:56 +02:00
|
|
|
.fetch_one(&ctx.data().db)
|
2024-08-10 03:08:38 +02:00
|
|
|
.await
|
|
|
|
.map_err(|_| "You are not an user in my database, sowwy ^~^")?;
|
2024-06-22 16:40:56 +02:00
|
|
|
|
2024-06-22 23:07:11 +02:00
|
|
|
Ok(User {
|
|
|
|
id: user.id as u32,
|
|
|
|
discord_id: user.discord_id,
|
|
|
|
})
|
2024-06-22 16:40:56 +02:00
|
|
|
}
|
2024-08-08 15:59:36 +02:00
|
|
|
|
|
|
|
pub async fn by_id(db: &SqlitePool, id: u32) -> Result<Self, Error> {
|
|
|
|
let user = sqlx::query!("SELECT * FROM users WHERE id = ?", id)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(User {
|
|
|
|
id: user.id as u32,
|
|
|
|
discord_id: user.discord_id,
|
|
|
|
})
|
|
|
|
}
|
2024-06-22 16:40:56 +02:00
|
|
|
}
|
2024-06-27 21:22:44 +02:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub async fn discord_it_to_discord_user(
|
|
|
|
&ctx: &Context<'_>,
|
|
|
|
discord_id: &str,
|
|
|
|
) -> Result<poise::serenity_prelude::User, Error> {
|
|
|
|
UserId::from_str(discord_id)?
|
|
|
|
.to_user(ctx.http())
|
|
|
|
.await
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
}
|