feat: add a channel as a logging channel

Closes #1, fixes #10
This commit is contained in:
2023-11-23 02:09:45 +01:00
parent 47dd2b02c4
commit c7bf7ec9e6
7 changed files with 114 additions and 73 deletions

62
src/db/mod.rs Normal file
View 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(|_| ())
}
}