From 5115636f60fa4e1b0b711ba1d8e6d31121cd9aae Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Thu, 20 Feb 2025 23:00:22 -0500 Subject: [PATCH] hoist selfroles option --- src/commands/self_roles/mod.rs | 3 +- src/commands/settings.rs | 68 ++++++++++++++++++++++++++++++++-- src/main.rs | 1 + 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/src/commands/self_roles/mod.rs b/src/commands/self_roles/mod.rs index e6587e1..98dfcfc 100644 --- a/src/commands/self_roles/mod.rs +++ b/src/commands/self_roles/mod.rs @@ -49,7 +49,8 @@ async fn create_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit Some(role) => guild.role(ctx, role).await?.position, None => 0u16, } - }); + }) + .hoist(crate::commands::settings::get_hoist_selfroles(ctx, guild).await?); let member = guild.member(ctx, user).await?; diff --git a/src/commands/settings.rs b/src/commands/settings.rs index 82d864d..b4d3ada 100644 --- a/src/commands/settings.rs +++ b/src/commands/settings.rs @@ -25,7 +25,9 @@ async fn prefix(ctx: Context<'_>, prefix: Option) -> Result<(), Error> { match prefix { Some(prefix) => { - if !ctx.author_member().await.unwrap().permissions.iter().any(|p| p.manage_guild()) { + let member = ctx.author_member().await.unwrap(); + + if !member.permissions(ctx).iter().any(|p| p.manage_guild()) { ctx.reply("You do not have permission to change this setting.").await?; return Ok(()); } @@ -70,7 +72,9 @@ pub async fn position(ctx: Context<'_>, role: Option) -> Result<(), Error> } }; - if !ctx.author_member().await.unwrap().permissions.iter().any(|p| p.manage_guild()) { + let member = ctx.author_member().await.unwrap(); + + if !member.permissions(ctx).iter().any(|p| p.manage_guild()) { ctx.reply("You do not have permission to see or change this setting.").await?; return Ok(()); } @@ -101,7 +105,65 @@ pub async fn position(ctx: Context<'_>, role: Option) -> Result<(), Error> Ok(()) } -#[poise::command(prefix_command, slash_command, subcommands("prefix", "position"), subcommand_required)] +pub async fn get_hoist_selfroles(ctx: Context<'_>, guild: GuildId) -> Result { + let db = &ctx.data().database; + + let hoist: Option = sqlx::query("SELECT hoist_selfroles FROM settings WHERE guildid = $1") + .bind(guild.get() as i64) + .fetch_one(db).await?.get(0); + + Ok(hoist.unwrap_or(false)) +} + +#[poise::command(prefix_command, slash_command)] +pub async fn hoist(ctx: Context<'_>, hoist: Option) -> Result<(), Error> { + let guild = match ctx.guild_id() { + Some(g) => g, + None => { + ctx.reply("This command must be ran within a guild.").await?; + return Ok(()); + } + }; + + match hoist { + Some(hoist) => { + let member = ctx.author_member().await.unwrap(); + + if !member.permissions(ctx).iter().any(|p| p.manage_guild()) { + ctx.reply("You do not have permission to change this setting.").await?; + return Ok(()); + } + + let mut tx = ctx.data().database.begin().await?; + + sqlx::query("INSERT INTO settings (guildid, hoist_selfroles) VALUES ($1, $2) ON CONFLICT (guildid) DO UPDATE SET hoist_selfroles = EXCLUDED.hoist_selfroles") + .bind(guild.get() as i64) + .bind(hoist) + .execute(&mut *tx).await?; + + tx.commit().await?; + + if hoist { + ctx.reply("New self roles will now be automatically hoisted.").await?; + } else { + ctx.reply("New self roles will not be hoisted.").await?; + } + } + None => { + let s = if get_hoist_selfroles(ctx, guild).await? { + "enabled" + } else { + "disabled" + }; + + ctx.reply(format!("Hoisting selfroles is {s}.")).await?; + } + } + + Ok(()) +} + +#[poise::command(prefix_command, slash_command, subcommands("prefix", "position", "hoist"), subcommand_required)] pub async fn setting(_ctx: Context<'_>) -> Result<(), Error> { Ok(()) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9f3dc20..82f20a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -164,6 +164,7 @@ async fn main() -> Result<(), Error> { CREATE TABLE IF NOT EXISTS settings ( guildid BIGINT NOT NULL PRIMARY KEY, positional_role BIGINT, + hoist_selfroles BOOLEAN, prefix TEXT ) "#