some better transaction usage

This commit is contained in:
2024-12-10 15:41:49 -05:00
parent 0b0888e9a8
commit f09280260e
5 changed files with 15 additions and 13 deletions

2
Cargo.lock generated
View File

@@ -202,7 +202,7 @@ dependencies = [
[[package]] [[package]]
name = "bot" name = "bot"
version = "0.1.0" version = "0.1.1"
dependencies = [ dependencies = [
"clap", "clap",
"dotenv", "dotenv",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "bot" name = "bot"
version = "0.1.0" version = "0.1.1"
edition = "2021" edition = "2021"
[dependencies] [dependencies]

View File

@@ -59,9 +59,9 @@ pub async fn color(ctx: Context<'_>, #[autocomplete = "autocomplete_colors"] col
Color::from_rgb(rgb.r, rgb.g, rgb.b) Color::from_rgb(rgb.r, rgb.g, rgb.b)
}; };
let mut tx = ctx.data().database.begin().await?;
if let Some(guild) = ctx.guild_id() { 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? { match super::get_user_role(ctx.author().id, guild, &mut *tx).await? {
Some(role) => { Some(role) => {
guild.edit_role(ctx, role, EditRole::new().colour(color)).await?; 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(role.id.get() as i64)
.bind(guild.get() as i64) .bind(guild.get() as i64)
.execute(&mut *tx).await?; .execute(&mut *tx).await?;
tx.commit().await?;
let member = guild.member(ctx, ctx.author().id).await?; let member = guild.member(ctx, ctx.author().id).await?;
member.add_role(ctx, role.clone()).await?; member.add_role(ctx, role.clone()).await?;
ctx.reply(format!("You have been given the {} role!", role)).await?; ctx.reply(format!("You have been given the {} role!", role)).await?;
tx.commit().await?;
return Ok(()); return Ok(());
} }
} }

View File

@@ -6,9 +6,9 @@ use poise::serenity_prelude::EditRole;
/// Change the name of your personal role /// Change the name of your personal role
#[poise::command(slash_command, prefix_command)] #[poise::command(slash_command, prefix_command)]
pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> { 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() { 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? { let role = match super::get_user_role(ctx.author().id, guild, &mut *tx).await? {
Some(role) => role, Some(role) => role,
None => { None => {
@@ -20,11 +20,11 @@ pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> {
.bind(guild.get() as i64) .bind(guild.get() as i64)
.execute(&mut *tx).await?; .execute(&mut *tx).await?;
tx.commit().await?;
let member = guild.member(ctx, ctx.author().id).await?; let member = guild.member(ctx, ctx.author().id).await?;
member.add_role(ctx, role.clone()).await?; member.add_role(ctx, role.clone()).await?;
tx.commit().await?;
ctx.reply(format!("You've been given the {} role!", role)).await?; ctx.reply(format!("You've been given the {} role!", role)).await?;
return Ok(()); return Ok(());

View File

@@ -5,10 +5,10 @@ use poise::serenity_prelude as serenity;
/// Register an existing role as a user's custom role /// Register an existing role as a user's custom role
#[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")] #[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")]
pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> { pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> {
let mut tx = ctx.data().database.begin().await?;
if let Some(guild) = ctx.guild_id() { 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? { match super::get_user_role(user.id, guild, &mut *tx).await? {
Some(role) => { Some(role) => {
let role = guild.role(ctx, role).await?; 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(guild.get() as i64)
.bind(role.id.get() as i64) .bind(role.id.get() as i64)
.execute(&mut *tx).await?; .execute(&mut *tx).await?;
tx.commit().await?;
let member = guild.member(ctx, user.id).await?; let member = guild.member(ctx, user.id).await?;
member.add_role(ctx, role.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?; ctx.reply("This command can only be run in a guild!").await?;
} }
tx.commit().await?;
Ok(()) Ok(())
} }