From 2df9e08bc495611ed13b80b76f3bd48e70a654bf Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Sun, 23 Feb 2025 01:30:15 -0500 Subject: [PATCH 1/3] 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(()) } From f18e080612ac19f97b87925523b2d55e5372588d Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Sun, 23 Feb 2025 01:30:43 -0500 Subject: [PATCH 2/3] fix typo --- src/commands/gambling/leaderboard.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/gambling/leaderboard.rs b/src/commands/gambling/leaderboard.rs index 71498bf..42301f5 100644 --- a/src/commands/gambling/leaderboard.rs +++ b/src/commands/gambling/leaderboard.rs @@ -55,7 +55,7 @@ async fn display_leaderboard(ctx: Context<'_>, t: LeaderboardType) -> Result<(), Ok(()) } -/// DIsplay users with the top highest balances +/// Display users with the top highest balances #[poise::command(slash_command, prefix_command)] pub async fn tokens(ctx: Context<'_>, count: Option) -> Result<(), Error> { let count = count.unwrap_or(10); From f15517f567ccf6c4bccd49881293beb7a61708ef Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Sun, 2 Mar 2025 23:45:14 -0500 Subject: [PATCH 3/3] fix: bot uses default prefix in dms now --- src/main.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index bc1c710..c4fe890 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ async fn event_handler( match event { serenity::FullEvent::Message { new_message: message } => { if message.author.bot { return Ok(()) } + println!("{} in #{}: {}", message.author, message.channel_id, message.content); } serenity::FullEvent::GuildMemberRemoval { guild_id, user, .. } => { let mut tx = data.database.begin().await?; @@ -57,7 +58,7 @@ async fn event_handler( async fn get_prefix(ctx: PartialContext<'_, Data, Error>) -> Result, Error> { let guild = match ctx.guild_id { Some(guild) => guild, - None => return Ok(None), + None => return Ok(ctx.data.prefix.clone()), }; let db = &ctx.data.database; @@ -91,7 +92,7 @@ async fn main() -> Result<(), Error> { let framework = poise::Framework::builder() .options(poise::FrameworkOptions { - commands: commands::commands(), + commands: commands::commands().await, prefix_options: poise::PrefixFrameworkOptions { dynamic_prefix: Some(|ctx| Box::pin(get_prefix(ctx))), edit_tracker: Some(Arc::new(