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

@@ -1,44 +1,34 @@
use crate::common::{Context, Error};
use crate::common::{self, Context, Error};
use poise::serenity_prelude::EditRole;
use poise::serenity_prelude::{EditRole, Permissions};
/// Change the name of your personal role
#[poise::command(slash_command, prefix_command)]
pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> {
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? {
Some(role) => role,
None => {
let role = guild.create_role(ctx, EditRole::new().name(name)).await?;
sqlx::query("INSERT INTO selfroles (userid, roleid, guildid) VALUES ($1, $2, $3)")
.bind(ctx.author().id.get() as i64)
.bind(role.id.get() as i64)
.bind(guild.get() as i64)
.execute(&mut *tx).await?;
tx.commit().await?;
let member = guild.member(ctx, ctx.author().id).await?;
member.add_role(ctx, role.clone()).await?;
ctx.reply(format!("You've been given the {} role!", role)).await?;
return Ok(());
}
};
guild.edit_role(ctx, role, EditRole::new().name(name)).await?;
let role = guild.role(ctx, role).await?;
ctx.reply(format!("{} has been updated.", role)).await?;
Ok(())
let guild = if let Some(guild) = ctx.guild_id() {
guild
} else {
ctx.reply("This command must be run within a server.").await?;
Ok(())
ctx.reply("This command can only be run inside of a guild.").await?;
return Ok(());
};
let user = ctx.author();
let mut tx = ctx.data().database.begin().await?;
if let Some(role) = super::get_user_role(user.id, guild, &mut *tx).await? {
let role = guild.role(ctx, role).await?;
guild.edit_role(ctx, role.id, EditRole::new().name(name)).await?;
common::no_ping_reply(&ctx, format!("{} has been updated.", role)).await?;
} else {
let role = guild.create_role(ctx, EditRole::new().name(name).permissions(Permissions::empty())).await?;
super::update_user_role(user.id, guild, role.id, &mut *tx).await?;
let member = guild.member(ctx, user).await?;
member.add_role(ctx, role.id).await?;
tx.commit().await?;
common::no_ping_reply(&ctx, format!("{} has been given the new role {}.", user, role)).await?;
}
Ok(())
}