no db pool

This commit is contained in:
2024-12-07 15:10:07 -05:00
parent 241709b3a0
commit 3bc51c858f
7 changed files with 31 additions and 23 deletions

View File

@@ -3,6 +3,10 @@ use crate::common::{Context, Error};
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
use serenity::Colour; use serenity::Colour;
// this code uses Member::permissions, which while it is a deprecated function, it doesn't actually matter
// since it is only used to display information to the user.
#[allow(deprecated)]
fn get_dox_output(ctx: &mut Context<'_>, fn get_dox_output(ctx: &mut Context<'_>,
user: &serenity::User, user: &serenity::User,
member: Option<&serenity::Member>, member: Option<&serenity::Member>,

View File

@@ -5,8 +5,11 @@ use poise::serenity_prelude as serenity;
#[poise::command(slash_command, prefix_command)] #[poise::command(slash_command, prefix_command)]
pub async fn balance(ctx: Context<'_>, user: Option<serenity::User>) -> Result<(), Error> { pub async fn balance(ctx: Context<'_>, user: Option<serenity::User>) -> Result<(), Error> {
let user = user.as_ref().unwrap_or(ctx.author()); let user = user.as_ref().unwrap_or(ctx.author());
let data = ctx.data();
let mut db = data.database.lock().await;
let db = db.as_mut();
let wealth = super::get_balance(user.id, &ctx.data()).await?; let wealth = super::get_balance(user.id, db).await?;
ctx.reply(format!("{} **{}** token(s).", ctx.reply(format!("{} **{}** token(s).",
if user.id == ctx.author().id { if user.id == ctx.author().id {

View File

@@ -10,17 +10,19 @@ pub async fn give(ctx: Context<'_>, user: serenity::User, #[min = 1] amount: i32
} }
let data = ctx.data(); let data = ctx.data();
let mut db = data.database.lock().await;
let db = db.as_mut();
let author_balance = super::get_balance(ctx.author().id, &data).await?; let author_balance = super::get_balance(ctx.author().id, db).await?;
if author_balance < amount { if author_balance < amount {
ctx.reply(format!("You do not have a high enough balance (**{author_balance}**) to complete this transaction.")).await?; ctx.reply(format!("You do not have a high enough balance (**{author_balance}**) to complete this transaction.")).await?;
} else { } else {
let author_new_balance = author_balance - amount; let author_new_balance = author_balance - amount;
let reciever_new_balance = super::get_balance(user.id, &data).await? + amount; let reciever_new_balance = super::get_balance(user.id, db).await? + amount;
super::change_balance(user.id, reciever_new_balance, &data).await?; super::change_balance(user.id, reciever_new_balance, db).await?;
super::change_balance(ctx.author().id, author_new_balance, data).await?; super::change_balance(ctx.author().id, author_new_balance, db).await?;
ctx.reply(format!("You've given **{}** **{}** tokens!", user.display_name(), amount)).await?; ctx.reply(format!("You've given **{}** **{}** tokens!", user.display_name(), amount)).await?;
} }

View File

@@ -3,13 +3,11 @@ pub mod balance;
pub mod give; pub mod give;
pub mod wager; pub mod wager;
use crate::common::{Data, Error}; use crate::common::Error;
use poise::serenity_prelude::UserId; use poise::serenity_prelude::UserId;
use sqlx::Row; use sqlx::{Row, PgConnection};
pub async fn get_balance(id: UserId, data: &Data) -> Result<i32, Error> {
let db = &data.database;
pub async fn get_balance(id: UserId, db: &mut PgConnection) -> Result<i32, Error> {
let row = sqlx::query("SELECT balance FROM bank WHERE id = $1") let row = sqlx::query("SELECT balance FROM bank WHERE id = $1")
.bind(id.get() as i64) .bind(id.get() as i64)
.fetch_one(db).await.ok(); .fetch_one(db).await.ok();
@@ -23,9 +21,7 @@ pub async fn get_balance(id: UserId, data: &Data) -> Result<i32, Error> {
Ok(balance) Ok(balance)
} }
pub async fn change_balance(id: UserId, balance: i32, data: &Data) -> Result<(), Error> { pub async fn change_balance(id: UserId, balance: i32, db: &mut PgConnection) -> Result<(), Error> {
let db = &data.database;
sqlx::query("INSERT INTO bank (id, balance) VALUES ($1, $2) ON CONFLICT (id) DO UPDATE SET balance = EXCLUDED.balance") sqlx::query("INSERT INTO bank (id, balance) VALUES ($1, $2) ON CONFLICT (id) DO UPDATE SET balance = EXCLUDED.balance")
.bind(id.get() as i64) .bind(id.get() as i64)
.bind(balance) .bind(balance)

View File

@@ -8,7 +8,10 @@ pub async fn wager(
amount: i32) -> Result<(), Error> amount: i32) -> Result<(), Error>
{ {
let data = ctx.data(); let data = ctx.data();
let mut wealth = super::get_balance(ctx.author().id, &data).await?; let mut db = data.database.lock().await;
let db = db.as_mut();
let mut wealth = super::get_balance(ctx.author().id, db).await?;
if wealth < amount { if wealth < amount {
ctx.reply(format!("You do not have enough tokens (**{}**) to wager this amount.", ctx.reply(format!("You do not have enough tokens (**{}**) to wager this amount.",
@@ -26,7 +29,7 @@ pub async fn wager(
amount, wealth)).await?; amount, wealth)).await?;
} }
super::change_balance(ctx.author().id, wealth, &data).await?; super::change_balance(ctx.author().id, wealth, db).await?;
Ok(()) Ok(())
} }

View File

@@ -2,10 +2,10 @@ use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use std::collections::HashMap; use std::collections::HashMap;
use poise::serenity_prelude::UserId; use poise::serenity_prelude::UserId;
use sqlx::{Pool, Postgres}; use sqlx::PgConnection;
pub struct Data { pub struct Data {
pub database: Pool<Postgres>, pub database: Arc<Mutex<PgConnection>>,
pub mentions: Arc<Mutex<HashMap<UserId, std::time::Instant>>>, pub mentions: Arc<Mutex<HashMap<UserId, std::time::Instant>>>,
} }

View File

@@ -13,7 +13,7 @@ use tokio::sync::Mutex;
use clap::Parser; use clap::Parser;
use sqlx::postgres::PgPoolOptions; use sqlx::{PgConnection, Connection};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
struct BotArgs { struct BotArgs {
@@ -68,9 +68,7 @@ async fn main() -> Result<(), Error> {
Box::pin(async move { Box::pin(async move {
poise::builtins::register_globally(ctx, &framework.options().commands).await?; poise::builtins::register_globally(ctx, &framework.options().commands).await?;
let database = PgPoolOptions::new() let mut database = PgConnection::connect(&database_url).await?;
.max_connections(4)
.connect(&database_url).await?;
sqlx::query( sqlx::query(
r#" r#"
@@ -79,10 +77,12 @@ async fn main() -> Result<(), Error> {
balance INT balance INT
) )
"#, "#,
).execute(&database).await?; ).execute(&mut database).await?;
println!("Bot is ready!");
Ok(Data { Ok(Data {
database, database: Arc::new(Mutex::new(database)),
mentions: Arc::new(Mutex::new(HashMap::new())), mentions: Arc::new(Mutex::new(HashMap::new())),
}) })
}) })