initial commit

This commit is contained in:
2023-11-22 21:38:21 +01:00
commit 47dd2b02c4
15 changed files with 3534 additions and 0 deletions

59
src/db.rs Normal file
View File

@@ -0,0 +1,59 @@
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(())
}
}

19
src/discord/commands.rs Normal file
View File

@@ -0,0 +1,19 @@
use super::{Context, Error};
use super::utils::serenity;
#[poise::command(slash_command)]
pub async fn add_logging_channel(
ctx: Context<'_>,
#[description = "Selected channel"] channel: Option<serenity::Channel>,
) -> Result<(), Error> {
let response = match channel {
None => "No channel selected. Please select one.".to_owned(),
Some(chan) => {
let channel_id = chan.id();
format!("Selected channel <#{channel_id}>")
}
};
ctx.say(response).await?;
Ok(())
}

0
src/discord/events.rs Normal file
View File

31
src/discord/mod.rs Normal file
View File

@@ -0,0 +1,31 @@
mod commands;
mod events;
pub mod utils;
use poise::FrameworkBuilder;
use utils::serenity;
use commands::add_logging_channel;
use utils::{BotData, Context, Error};
pub async fn make_bot() -> color_eyre::Result<FrameworkBuilder<BotData, Error>>
{
let framework = poise::Framework::builder()
.options(poise::FrameworkOptions {
commands: vec![add_logging_channel()],
..Default::default()
})
.token(std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN"))
.intents(serenity::GatewayIntents::non_privileged())
.setup(|ctx, _ready, framework| {
Box::pin(async move {
poise::builtins::register_globally(
ctx,
&framework.options().commands,
)
.await?;
Ok(BotData::new().await?)
})
});
Ok(framework)
}

17
src/discord/utils.rs Normal file
View File

@@ -0,0 +1,17 @@
use crate::db::Database;
pub use poise::serenity_prelude as serenity;
pub struct BotData {
database: Database,
}
impl BotData {
pub async fn new() -> color_eyre::Result<Self> {
Ok(Self {
database: Database::new().await?,
})
}
}
pub type Error = Box<dyn std::error::Error + Send + Sync>;
pub type Context<'a> = poise::Context<'a, BotData, Error>;

17
src/main.rs Normal file
View File

@@ -0,0 +1,17 @@
mod utils;
mod db;
mod discord;
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
dotenvy::dotenv()?;
color_eyre::install()?;
utils::setup_logging();
let bot = discord::make_bot().await?;
bot.run().await?;
Ok(())
}

10
src/utils.rs Normal file
View File

@@ -0,0 +1,10 @@
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
pub fn setup_logging() {
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::DEBUG)
.finish();
tracing::subscriber::set_global_default(subscriber)
.expect("Setting default subscriber failed");
}