new role management commands, bug fix, table restructuring, better output
This commit is contained in:
@@ -1,27 +1,36 @@
|
||||
|
||||
use crate::common::{Context, Error};
|
||||
use crate::common::{self, Context, Error};
|
||||
|
||||
use poise::serenity_prelude as serenity;
|
||||
|
||||
/// Register an existing role as a user's custom role
|
||||
#[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")]
|
||||
pub async fn register(ctx: Context<'_>, user: Option<serenity::User>, role: serenity::Role) -> Result<(), Error> {
|
||||
pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> {
|
||||
let data = ctx.data();
|
||||
let mut db = data.database.lock().await;
|
||||
let db = db.as_mut();
|
||||
|
||||
let user = user.as_ref().unwrap_or(ctx.author());
|
||||
if let Some(guild) = ctx.guild_id() {
|
||||
match super::get_user_role(user.id, guild, db).await? {
|
||||
Some(role) => {
|
||||
common::no_ping_reply(&ctx, format!("{} already has the role {} registered.", user, role)).await?;
|
||||
Ok(())
|
||||
},
|
||||
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(db).await?;
|
||||
|
||||
if let Some(guild) = ctx.guild().map(|g| g.id) {
|
||||
sqlx::query("INSERT INTO selfroles (userid, roleid, guildid) VALUES ($1, $2, $3) ON CONFLICT (userid) DO UPDATE SET roleid = EXCLUDED.roleid")
|
||||
.bind(user.id.get() as i64)
|
||||
.bind(role.id.get() as i64)
|
||||
.bind(guild.get() as i64)
|
||||
.execute(db).await?;
|
||||
|
||||
ctx.reply(format!("**{}** now has **{}** set as their personal role.", user.display_name(), role.name)).await?;
|
||||
|
||||
Ok(())
|
||||
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?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ctx.reply("This command can only be run in a guild!").await?;
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user