62
src/db/mod.rs
Normal file
62
src/db/mod.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use std::env;
|
||||
|
||||
use poise::serenity_prelude::{ChannelId, GuildId};
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
pub type Result<T> = ::std::result::Result<T, sqlx::Error>;
|
||||
|
||||
pub struct Database {
|
||||
pool: SqlitePool,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub async fn new() -> Result<Self> {
|
||||
Ok(Self {
|
||||
pool: SqlitePool::connect(
|
||||
&env::var("DATABASE_URL")
|
||||
.expect("Missing enviroment variable DATABASE_URL"),
|
||||
)
|
||||
.await?,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_logging_channel(
|
||||
&self,
|
||||
guild_id: u64,
|
||||
) -> Result<Vec<u64>> {
|
||||
let guild_id = guild_id as i64;
|
||||
let channels = sqlx::query!(
|
||||
r#"
|
||||
SELECT channel_id
|
||||
FROM guild_log_channels
|
||||
WHERE guild_id = ?1
|
||||
"#,
|
||||
guild_id
|
||||
)
|
||||
.fetch_all(&self.pool)
|
||||
.await?;
|
||||
Ok(channels.iter().map(|id| id.channel_id as u64).collect())
|
||||
}
|
||||
|
||||
pub async fn set_logging_channel(
|
||||
&self,
|
||||
guild_id: GuildId,
|
||||
channel_id: ChannelId,
|
||||
) -> Result<()> {
|
||||
let guild_id = guild_id.0 as i64;
|
||||
let channel_id = channel_id.0 as i64;
|
||||
let mut conn = self.pool.acquire().await?;
|
||||
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO guild_log_channels (guild_id, channel_id)
|
||||
VALUES ( ?1, ?2 )
|
||||
"#,
|
||||
guild_id,
|
||||
channel_id
|
||||
)
|
||||
.execute(&mut *conn)
|
||||
.await
|
||||
.map(|_| ())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user