From 2df9e08bc495611ed13b80b76f3bd48e70a654bf Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Sun, 23 Feb 2025 01:30:15 -0500 Subject: [PATCH] use a &mut PgConnection --- src/commands/self_roles/color.rs | 5 ++++- src/commands/self_roles/mod.rs | 31 +++++++++++++------------------ src/commands/self_roles/name.rs | 6 +++++- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/commands/self_roles/color.rs b/src/commands/self_roles/color.rs index ac83f8d..f0f6979 100644 --- a/src/commands/self_roles/color.rs +++ b/src/commands/self_roles/color.rs @@ -82,7 +82,10 @@ pub async fn color(ctx: Context<'_>, let user = ctx.author(); - let role = super::edit_role(ctx, user.id, guild, EditRole::new().colour(color), &ctx.data().database).await?; + let mut tx = ctx.data().database.begin().await?; + let role = super::edit_role(ctx, user.id, guild, EditRole::new().colour(color), &mut *tx).await?; + tx.commit().await?; + common::no_ping_reply(&ctx, format!("{}'s color has been updated.", guild.role(ctx, role).await?)).await?; Ok(()) diff --git a/src/commands/self_roles/mod.rs b/src/commands/self_roles/mod.rs index 98dfcfc..b723e35 100644 --- a/src/commands/self_roles/mod.rs +++ b/src/commands/self_roles/mod.rs @@ -1,6 +1,6 @@ use crate::common::{Context, Error}; -use sqlx::{PgExecutor, Row}; +use sqlx::{PgConnection, Row}; use poise::serenity_prelude::{EditRole, GuildId, Permissions, RoleId, UserId}; mod register; @@ -27,10 +27,9 @@ pub async fn role(_ctx: Context<'_>) -> Result<(), Error> { } /// Edit a user's personal role, creates it with some default values if it doesn't exist. -pub async fn edit_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit: EditRole<'a>, db: E) -> Result - where E: PgExecutor<'a> + Clone, +pub async fn edit_role(ctx: Context<'_>, user: UserId, guild: GuildId, edit: EditRole<'_>, db: &mut PgConnection) -> Result { - if let Some(role) = get_user_role(user, guild, db.clone()).await? { + if let Some(role) = get_user_role(user, guild, db).await? { guild.role(ctx, role).await?.edit(ctx, edit).await?; Ok(role) } else { @@ -38,8 +37,12 @@ pub async fn edit_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, ed } } -async fn create_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit: EditRole<'a>, db: E) -> Result - where E: PgExecutor<'a> + Clone, +async fn create_role( + ctx: Context<'_>, + user: UserId, + guild: GuildId, + edit: EditRole<'_>, + db: &mut PgConnection) -> Result { let def = EditRole::new() .name(user.to_user(ctx).await?.name) @@ -63,9 +66,7 @@ async fn create_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit } /// Remove a row concerning a user's self role from the database -pub async fn remove_user_role<'a, E>(user: UserId, guild: GuildId, db: E) -> Result<(), Error> - where E: PgExecutor<'a>, -{ +pub async fn remove_user_role(user: UserId, guild: GuildId, db: &mut PgConnection) -> Result<(), Error> { sqlx::query("DELETE FROM selfroles WHERE userid = $1 AND guildid = $2") .bind(user.get() as i64) .bind(guild.get() as i64) @@ -74,9 +75,7 @@ pub async fn remove_user_role<'a, E>(user: UserId, guild: GuildId, db: E) -> Res Ok(()) } -pub async fn remove_role<'a, E>(role: RoleId, guild: GuildId, db: E) -> Result<(), Error> -where E: PgExecutor<'a> -{ +pub async fn remove_role(role: RoleId, guild: GuildId, db: &mut PgConnection) -> Result<(), Error> { sqlx::query("DELETE FROM selfroles WHERE roleid = $1 AND guildid = $2") .bind(role.get() as i64) .bind(guild.get() as i64) @@ -86,9 +85,7 @@ where E: PgExecutor<'a> } /// Replace a user's custom role with a new one -pub async fn update_user_role<'a, E>(user: UserId, guild: GuildId, role: RoleId, db: E) -> Result<(), Error> - where E: PgExecutor<'a>, -{ +pub async fn update_user_role(user: UserId, guild: GuildId, role: RoleId, db: &mut PgConnection) -> Result<(), Error> { sqlx::query("INSERT INTO selfroles (userid, guildid, roleid) VALUES($1, $2, $3) ON CONFLICT (userid, guildid) DO UPDATE SET roleid = EXCLUDED.roleid") .bind(user.get() as i64) .bind(guild.get() as i64) @@ -99,9 +96,7 @@ pub async fn update_user_role<'a, E>(user: UserId, guild: GuildId, role: RoleId, } /// Get a user's personal role id from the database -pub async fn get_user_role<'a, E>(user: UserId, guild: GuildId, db: E) -> Result, Error> - where E: PgExecutor<'a>, -{ +pub async fn get_user_role(user: UserId, guild: GuildId, db: &mut PgConnection) -> Result, Error> { match sqlx::query("SELECT roleid FROM selfroles WHERE userid = $1 AND guildid = $2") .bind(user.get() as i64) .bind(guild.get() as i64) diff --git a/src/commands/self_roles/name.rs b/src/commands/self_roles/name.rs index 17a5120..5f353c6 100644 --- a/src/commands/self_roles/name.rs +++ b/src/commands/self_roles/name.rs @@ -15,8 +15,12 @@ pub async fn name(ctx: Context<'_>, #[rest] name: String) -> Result<(), Error> { let user = ctx.author(); - let role = super::edit_role(ctx, user.id, guild, EditRole::new().name(name), &ctx.data().database).await?; + let mut tx = ctx.data().database.begin().await?; + let role = super::edit_role(ctx, user.id, guild, EditRole::new().name(name), &mut *tx).await?; + tx.commit().await?; + common::no_ping_reply(&ctx, format!("{} has been updated.", guild.role(ctx, role).await?)).await?; + Ok(()) }