no db pool
This commit is contained in:
@@ -3,6 +3,10 @@ use crate::common::{Context, Error};
|
||||
use poise::serenity_prelude as serenity;
|
||||
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<'_>,
|
||||
user: &serenity::User,
|
||||
member: Option<&serenity::Member>,
|
||||
|
||||
@@ -5,8 +5,11 @@ use poise::serenity_prelude as serenity;
|
||||
#[poise::command(slash_command, prefix_command)]
|
||||
pub async fn balance(ctx: Context<'_>, user: Option<serenity::User>) -> Result<(), Error> {
|
||||
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).",
|
||||
if user.id == ctx.author().id {
|
||||
|
||||
@@ -10,17 +10,19 @@ pub async fn give(ctx: Context<'_>, user: serenity::User, #[min = 1] amount: i32
|
||||
}
|
||||
|
||||
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 {
|
||||
ctx.reply(format!("You do not have a high enough balance (**{author_balance}**) to complete this transaction.")).await?;
|
||||
} else {
|
||||
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(ctx.author().id, author_new_balance, data).await?;
|
||||
super::change_balance(user.id, reciever_new_balance, db).await?;
|
||||
super::change_balance(ctx.author().id, author_new_balance, db).await?;
|
||||
|
||||
ctx.reply(format!("You've given **{}** **{}** tokens!", user.display_name(), amount)).await?;
|
||||
}
|
||||
|
||||
@@ -3,13 +3,11 @@ pub mod balance;
|
||||
pub mod give;
|
||||
pub mod wager;
|
||||
|
||||
use crate::common::{Data, Error};
|
||||
use crate::common::Error;
|
||||
use poise::serenity_prelude::UserId;
|
||||
use sqlx::Row;
|
||||
|
||||
pub async fn get_balance(id: UserId, data: &Data) -> Result<i32, Error> {
|
||||
let db = &data.database;
|
||||
use sqlx::{Row, PgConnection};
|
||||
|
||||
pub async fn get_balance(id: UserId, db: &mut PgConnection) -> Result<i32, Error> {
|
||||
let row = sqlx::query("SELECT balance FROM bank WHERE id = $1")
|
||||
.bind(id.get() as i64)
|
||||
.fetch_one(db).await.ok();
|
||||
@@ -23,9 +21,7 @@ pub async fn get_balance(id: UserId, data: &Data) -> Result<i32, Error> {
|
||||
Ok(balance)
|
||||
}
|
||||
|
||||
pub async fn change_balance(id: UserId, balance: i32, data: &Data) -> Result<(), Error> {
|
||||
let db = &data.database;
|
||||
|
||||
pub async fn change_balance(id: UserId, balance: i32, db: &mut PgConnection) -> Result<(), Error> {
|
||||
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(balance)
|
||||
|
||||
@@ -8,7 +8,10 @@ pub async fn wager(
|
||||
amount: i32) -> Result<(), Error>
|
||||
{
|
||||
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 {
|
||||
ctx.reply(format!("You do not have enough tokens (**{}**) to wager this amount.",
|
||||
@@ -26,7 +29,7 @@ pub async fn wager(
|
||||
amount, wealth)).await?;
|
||||
}
|
||||
|
||||
super::change_balance(ctx.author().id, wealth, &data).await?;
|
||||
super::change_balance(ctx.author().id, wealth, db).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -2,10 +2,10 @@ use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use std::collections::HashMap;
|
||||
use poise::serenity_prelude::UserId;
|
||||
use sqlx::{Pool, Postgres};
|
||||
use sqlx::PgConnection;
|
||||
|
||||
pub struct Data {
|
||||
pub database: Pool<Postgres>,
|
||||
pub database: Arc<Mutex<PgConnection>>,
|
||||
pub mentions: Arc<Mutex<HashMap<UserId, std::time::Instant>>>,
|
||||
}
|
||||
|
||||
|
||||
12
src/main.rs
12
src/main.rs
@@ -13,7 +13,7 @@ use tokio::sync::Mutex;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
use sqlx::postgres::PgPoolOptions;
|
||||
use sqlx::{PgConnection, Connection};
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
struct BotArgs {
|
||||
@@ -68,9 +68,7 @@ async fn main() -> Result<(), Error> {
|
||||
Box::pin(async move {
|
||||
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
||||
|
||||
let database = PgPoolOptions::new()
|
||||
.max_connections(4)
|
||||
.connect(&database_url).await?;
|
||||
let mut database = PgConnection::connect(&database_url).await?;
|
||||
|
||||
sqlx::query(
|
||||
r#"
|
||||
@@ -79,10 +77,12 @@ async fn main() -> Result<(), Error> {
|
||||
balance INT
|
||||
)
|
||||
"#,
|
||||
).execute(&database).await?;
|
||||
).execute(&mut database).await?;
|
||||
|
||||
println!("Bot is ready!");
|
||||
|
||||
Ok(Data {
|
||||
database,
|
||||
database: Arc::new(Mutex::new(database)),
|
||||
mentions: Arc::new(Mutex::new(HashMap::new())),
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user