From f09280260ea7e036fbd11722fda97a61dc6c3970 Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Tue, 10 Dec 2024 15:41:49 -0500 Subject: [PATCH 1/4] some better transaction usage --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/commands/self_roles/color.rs | 7 ++++--- src/commands/self_roles/name.rs | 8 ++++---- src/commands/self_roles/register.rs | 9 +++++---- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b35a14..2c43a77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -202,7 +202,7 @@ dependencies = [ [[package]] name = "bot" -version = "0.1.0" +version = "0.1.1" dependencies = [ "clap", "dotenv", diff --git a/Cargo.toml b/Cargo.toml index dbfd274..92f95f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bot" -version = "0.1.0" +version = "0.1.1" edition = "2021" [dependencies] diff --git a/src/commands/self_roles/color.rs b/src/commands/self_roles/color.rs index 4bfbb5a..44410ab 100644 --- a/src/commands/self_roles/color.rs +++ b/src/commands/self_roles/color.rs @@ -59,9 +59,9 @@ pub async fn color(ctx: Context<'_>, #[autocomplete = "autocomplete_colors"] col Color::from_rgb(rgb.r, rgb.g, rgb.b) }; - let mut tx = ctx.data().database.begin().await?; - if let Some(guild) = ctx.guild_id() { + let mut tx = ctx.data().database.begin().await?; + match super::get_user_role(ctx.author().id, guild, &mut *tx).await? { Some(role) => { guild.edit_role(ctx, role, EditRole::new().colour(color)).await?; @@ -82,13 +82,14 @@ pub async fn color(ctx: Context<'_>, #[autocomplete = "autocomplete_colors"] col .bind(role.id.get() as i64) .bind(guild.get() as i64) .execute(&mut *tx).await?; + + tx.commit().await?; let member = guild.member(ctx, ctx.author().id).await?; member.add_role(ctx, role.clone()).await?; ctx.reply(format!("You have been given the {} role!", role)).await?; - tx.commit().await?; return Ok(()); } } diff --git a/src/commands/self_roles/name.rs b/src/commands/self_roles/name.rs index 59389e5..b9fa4a5 100644 --- a/src/commands/self_roles/name.rs +++ b/src/commands/self_roles/name.rs @@ -6,9 +6,9 @@ use poise::serenity_prelude::EditRole; /// Change the name of your personal role #[poise::command(slash_command, prefix_command)] pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> { - let mut tx = ctx.data().database.begin().await?; - if let Some(guild) = ctx.guild_id() { + let mut tx = ctx.data().database.begin().await?; + let role = match super::get_user_role(ctx.author().id, guild, &mut *tx).await? { Some(role) => role, None => { @@ -20,11 +20,11 @@ pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> { .bind(guild.get() as i64) .execute(&mut *tx).await?; + tx.commit().await?; + let member = guild.member(ctx, ctx.author().id).await?; member.add_role(ctx, role.clone()).await?; - tx.commit().await?; - ctx.reply(format!("You've been given the {} role!", role)).await?; return Ok(()); diff --git a/src/commands/self_roles/register.rs b/src/commands/self_roles/register.rs index 40d432c..2538db1 100644 --- a/src/commands/self_roles/register.rs +++ b/src/commands/self_roles/register.rs @@ -5,10 +5,10 @@ use poise::serenity_prelude as serenity; /// Register an existing role as a user's custom role #[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")] -pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> { - let mut tx = ctx.data().database.begin().await?; - +pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> { if let Some(guild) = ctx.guild_id() { + let mut tx = ctx.data().database.begin().await?; + match super::get_user_role(user.id, guild, &mut *tx).await? { Some(role) => { let role = guild.role(ctx, role).await?; @@ -20,6 +20,8 @@ pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Ro .bind(guild.get() as i64) .bind(role.id.get() as i64) .execute(&mut *tx).await?; + + tx.commit().await?; let member = guild.member(ctx, user.id).await?; member.add_role(ctx, role.id).await?; @@ -31,6 +33,5 @@ pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Ro ctx.reply("This command can only be run in a guild!").await?; } - tx.commit().await?; Ok(()) } From 23bdcffc8c2d3b8097749e211e9836950d568508 Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Tue, 10 Dec 2024 17:59:30 -0500 Subject: [PATCH 2/4] add help for daily commands --- src/commands/gambling/daily.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/commands/gambling/daily.rs b/src/commands/gambling/daily.rs index 8b47246..3024228 100644 --- a/src/commands/gambling/daily.rs +++ b/src/commands/gambling/daily.rs @@ -57,6 +57,7 @@ where Ok(()) } +/// Tells you what your current daily streak is #[poise::command(slash_command, prefix_command)] pub async fn streak(ctx: Context<'_>) -> Result<(), Error> { let db = &ctx.data().database; @@ -120,6 +121,7 @@ async fn do_claim(ctx: Context<'_>) -> Result<(), Error> { Ok(()) } +// Redeem daily tokens. #[poise::command(slash_command, prefix_command)] pub async fn claim(ctx: Context<'_>) -> Result<(), Error> { do_claim(ctx).await From d23fc1d7e76155832a1e14ecad2d9a8d085f3f8f Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Wed, 11 Dec 2024 00:37:44 -0500 Subject: [PATCH 3/4] fix: stealing other players' items --- src/inventory.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/inventory.rs b/src/inventory.rs index 9ce7b9d..ba9cc9f 100644 --- a/src/inventory.rs +++ b/src/inventory.rs @@ -53,9 +53,9 @@ impl Inventory { let x = sqlx::query_as( r#" SELECT id, name, game, item, data FROM items - where item = $1 + where item = $1 AND owner = $2 "# - ).bind(item as i64).fetch_one(db).await.ok(); + ).bind(item as i64).bind(self.user.get() as i64).fetch_one(db).await.ok(); Ok(x) } @@ -67,9 +67,9 @@ impl Inventory { let x = sqlx::query_as( r#" SELECT id, name, game, item, data FROM items - where name = $1 + where name = $1 AND user = $2 "# - ).bind(name).fetch_one(db).await.ok(); + ).bind(name).bind(self.user.get() as i64).fetch_one(db).await.ok(); Ok(x) } From 7bdb63ff6c36e03524c2e39a1ebf553910412cca Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Wed, 11 Dec 2024 16:13:50 -0500 Subject: [PATCH 4/4] take advantage of discord timestamps to display time until next daily --- src/commands/gambling/daily.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/commands/gambling/daily.rs b/src/commands/gambling/daily.rs index 3024228..c076736 100644 --- a/src/commands/gambling/daily.rs +++ b/src/commands/gambling/daily.rs @@ -1,7 +1,7 @@ use crate::{Context, Error}; use poise::serenity_prelude::UserId; -use sqlx::{types::chrono::{DateTime, Local, TimeZone}, PgExecutor, Row}; +use sqlx::{types::chrono::{DateTime, Utc, TimeZone}, PgExecutor, Row}; use std::time::Duration; @@ -31,7 +31,7 @@ where Ok(()) } -async fn get_last<'a, E>(db: E, user: UserId) -> Result>, Error> +async fn get_last<'a, E>(db: E, user: UserId) -> Result>, Error> where E: PgExecutor<'a>, { @@ -45,7 +45,7 @@ where } } -async fn set_last<'a, E>(db: E, user: UserId, last: DateTime) -> Result<(), Error> +async fn set_last<'a, E>(db: E, user: UserId, last: DateTime) -> Result<(), Error> where E: PgExecutor<'a>, { @@ -73,9 +73,9 @@ async fn do_claim(ctx: Context<'_>) -> Result<(), Error> { let last = get_last(&mut *tx, user).await?; let existed = last.is_some(); - let last = last.unwrap_or(Local.timestamp_opt(0, 0).unwrap()); + let last = last.unwrap_or(Utc.timestamp_opt(0, 0).unwrap()); - let now = Local::now(); + let now = Utc::now(); let next_daily = last + Duration::from_secs(24 * 60 * 60); let time_to_redeem = next_daily + Duration::from_secs(24 * 60 * 60); @@ -104,7 +104,7 @@ async fn do_claim(ctx: Context<'_>) -> Result<(), Error> { } let payout = 50 + 10 * streak.min(7); - + let balance = super::get_balance(user, &mut *tx).await?; super::change_balance(user, balance + payout, &mut *tx).await?; @@ -115,7 +115,7 @@ async fn do_claim(ctx: Context<'_>) -> Result<(), Error> { ctx.reply(format!("{begin}**{payout}** tokens were added to your balance.{end}")).await?; } else { - ctx.reply(format!("Your next daily is not available! It will be available on {}.", next_daily.to_rfc2822())).await?; + ctx.reply(format!("Your next daily is not available! It will be available .", next_daily.timestamp())).await?; } Ok(())