Skip to content

Commit

Permalink
feat: add shuttle.rs shared database
Browse files Browse the repository at this point in the history
  • Loading branch information
melothemarten committed Sep 6, 2024
1 parent 0e2ecec commit a28544e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 34 deletions.
63 changes: 36 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
shuttle-runtime = "0.46.0"
shuttle-serenity = "0.46.0"
shuttle-shared-db = { version = "0.46.0", features = ["postgres"] }
shuttle-runtime = "0.47.0"
shuttle-serenity = "0.47.0"
shuttle-shared-db = { version = "0.47.0", features = ["postgres", "sqlx"] }
poise = "0.6"
anyhow = "1.0"
tokio = "1.28"
Expand Down
Empty file added migrations/0001_init.sql
Empty file.
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,23 @@ pub mod helpers;
pub mod types;

#[shuttle_runtime::main]
async fn serenity(#[shuttle_runtime::Secrets] secret_store: SecretStore) -> ShuttleSerenity {
async fn serenity(
#[shuttle_runtime::Secrets] secret_store: SecretStore,
#[shuttle_shared_db::Postgres] pool: sqlx::PgPool,
) -> ShuttleSerenity {
let token = secret_store
.get("DISCORD_TOKEN")
.expect("Couldn't find your DISCORD_TOKEN!");
let intents = serenity::GatewayIntents::all();

sqlx::migrate!()
.run(&pool)
.await
.expect("Failed to run migrations");

let framework = poise::Framework::builder()
.setup(move |ctx, ready, framework| {
Box::pin(async move {
let data = Data::new(&secret_store)?;
let data = Data::new(&secret_store, pool)?;

debug!("Registering commands...");
poise::builtins::register_in_guild(
Expand Down Expand Up @@ -73,6 +80,7 @@ async fn serenity(#[shuttle_runtime::Secrets] secret_store: SecretStore) -> Shut
commands::playground::fmt(),
commands::playground::microbench(),
commands::playground::procmacro(),
commands::tags::tag(),
],
prefix_options: poise::PrefixFrameworkOptions {
prefix: Some("?".into()),
Expand Down Expand Up @@ -168,6 +176,8 @@ code here
})
.build();

let intents = serenity::GatewayIntents::all();

let client = serenity::ClientBuilder::new(token, intents)
.framework(framework)
.await
Expand Down
4 changes: 3 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::commands;

#[derive(Debug)]
pub struct Data {
pub database: sqlx::PgPool,
pub discord_guild_id: serenity::GuildId,
pub application_id: serenity::UserId,
pub mod_role_id: serenity::RoleId,
Expand All @@ -20,8 +21,9 @@ pub struct Data {
}

impl Data {
pub fn new(secret_store: &SecretStore) -> Result<Self> {
pub fn new(secret_store: &SecretStore, database: sqlx::PgPool) -> Result<Self> {
Ok(Self {
database,
discord_guild_id: secret_store
.get("DISCORD_GUILD")
.ok_or(anyhow!(
Expand Down

0 comments on commit a28544e

Please sign in to comment.