update v0.1.2, update self roles code

This commit is contained in:
2025-02-17 17:21:40 -05:00
parent bd4b67b619
commit a248066e9e
11 changed files with 172 additions and 160 deletions

View File

@@ -3,35 +3,29 @@ use crate::common::{self, Context, Error};
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. This deletes their current self 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> {
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?;
common::no_ping_reply(&ctx, format!("{} already has the role {} registered.", user, role)).await?;
},
None => {
sqlx::query("INSERT INTO selfroles (userid, guildid, roleid) VALUES ($1, $2, $3)")
.bind(user.id.get() as i64)
.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?;
common::no_ping_reply(&ctx, format!("{} now has {} set as their personal role.", user, role)).await?;
}
}
pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> {
let guild = if let Some(guild) = ctx.guild_id() {
guild
} else {
ctx.reply("This command can only be run in a guild!").await?;
ctx.reply("This command can only be run inside of a guild.").await?;
return Ok(());
};
let mut tx = ctx.data().database.begin().await?;
if let Some(role) = super::get_user_role(user.id, guild, &mut *tx).await? {
guild.delete_role(ctx, role).await?;
}
let member = guild.member(ctx, user).await?;
member.add_role(ctx, role.id).await?;
super::update_user_role(member.user.id, guild, role.id, &mut *tx).await?;
tx.commit().await?;
common::no_ping_reply(&ctx, format!("{} has been set as {}'s self role.", role, member.user)).await?;
Ok(())
}