60 lines
1.3 KiB
Rust
60 lines
1.3 KiB
Rust
use std::env;
|
|
|
|
use sqlx::SqlitePool;
|
|
|
|
pub struct Database {
|
|
pool: SqlitePool,
|
|
}
|
|
|
|
impl Database {
|
|
pub async fn new() -> color_eyre::Result<Self> {
|
|
Ok(Self {
|
|
pool: SqlitePool::connect(&env::var("DATABASE_URL")?).await?,
|
|
})
|
|
}
|
|
|
|
pub async fn get_logging_channel(
|
|
&self,
|
|
guild_id: u64,
|
|
) -> color_eyre::Result<Vec<u64>> {
|
|
let guild_str = guild_id.to_string();
|
|
let channels = sqlx::query!(
|
|
r#"
|
|
SELECT channel_id
|
|
FROM guild_log_channels
|
|
WHERE guild_id = ?1
|
|
"#,
|
|
guild_str
|
|
)
|
|
.fetch_all(&self.pool)
|
|
.await?;
|
|
Ok(channels
|
|
.iter()
|
|
.map(|id| id.channel_id.parse::<u64>().unwrap())
|
|
.collect())
|
|
}
|
|
|
|
pub async fn set_logging_channel(
|
|
&self,
|
|
guild_id: u64,
|
|
channel_id: u64,
|
|
) -> color_eyre::Result<()> {
|
|
let guild_str = guild_id.to_string();
|
|
let channel_str = channel_id.to_string();
|
|
let mut conn = self.pool.acquire().await?;
|
|
|
|
sqlx::query!(
|
|
r#"
|
|
INSERT INTO guild_log_channels (guild_id, channel_id)
|
|
VALUES ( ?1, ?2 )
|
|
"#,
|
|
guild_str,
|
|
channel_str
|
|
)
|
|
.execute(&mut *conn)
|
|
.await?
|
|
.last_insert_rowid();
|
|
Ok(())
|
|
}
|
|
}
|