use a transaction for name changes

This commit is contained in:
2024-12-10 02:19:48 -05:00
parent ae127367c6
commit d62d1feb09

View File

@@ -6,10 +6,10 @@ 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 db = &ctx.data().database;
let mut tx = ctx.data().database.begin().await?;
if let Some(guild) = ctx.guild_id() {
let role = match super::get_user_role(ctx.author().id, guild, db).await? {
let role = match super::get_user_role(ctx.author().id, guild, &mut *tx).await? {
Some(role) => role,
None => {
let role = guild.create_role(ctx, EditRole::new().name(name)).await?;
@@ -18,11 +18,13 @@ pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> {
.bind(ctx.author().id.get() as i64)
.bind(role.id.get() as i64)
.bind(guild.get() as i64)
.execute(db).await?;
.execute(&mut *tx).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(());