From f3ac3359eccfa7d3bfa205717fa360bd6eb0b535 Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Thu, 20 Feb 2025 21:17:39 -0500 Subject: [PATCH 1/4] slightly better help message --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 9d43278..a5c94dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use sqlx::Row; #[derive(Parser, Debug)] struct BotArgs { - /// Prefix for the bot (if unspecified, the bot will not have one) + /// Prefix for the bot. If unspecified, the bot will not have one and will also not have access to message content. #[arg(short, long)] prefix: Option, } From 764538d928e704b146b38f25549d051e64fd6a2b Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Thu, 20 Feb 2025 21:18:00 -0500 Subject: [PATCH 2/4] create more usable helper functions --- src/commands/self_roles/color.rs | 18 +++--------------- src/commands/self_roles/mod.rs | 31 ++++++++++++++++++++++++++++++- src/commands/self_roles/name.rs | 18 +++--------------- 3 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/commands/self_roles/color.rs b/src/commands/self_roles/color.rs index ed230fd..c411c16 100644 --- a/src/commands/self_roles/color.rs +++ b/src/commands/self_roles/color.rs @@ -5,7 +5,7 @@ use once_cell::sync::Lazy; use std::collections::HashMap; use hex_color::HexColor; -use poise::serenity_prelude::{colours, Color, EditRole, Permissions}; +use poise::serenity_prelude::{colours, Color, EditRole}; static COLORS: Lazy> = Lazy::new(|| { HashMap::from([ @@ -78,20 +78,8 @@ pub async fn color(ctx: Context<'_>, #[autocomplete = "autocomplete_colors"] col 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().colour(color)).await?; - common::no_ping_reply(&ctx, format!("{}'s color has been updated.", role)).await?; - } else { - let role = guild.create_role(ctx, EditRole::new().colour(color).name(user.name.clone()).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?; - } + let role = super::edit_role(ctx, user.id, guild, EditRole::new().colour(color), &ctx.data().database).await?; + common::no_ping_reply(&ctx, format!("{}'s color has been updated.", guild.role(ctx, role).await?)).await?; Ok(()) } diff --git a/src/commands/self_roles/mod.rs b/src/commands/self_roles/mod.rs index f9489b3..52d50b7 100644 --- a/src/commands/self_roles/mod.rs +++ b/src/commands/self_roles/mod.rs @@ -1,7 +1,7 @@ use crate::common::{Context, Error}; use sqlx::{PgExecutor, Row}; -use poise::serenity_prelude::{RoleId, UserId, GuildId}; +use poise::serenity_prelude::{EditRole, GuildId, Permissions, RoleId, UserId}; mod register; mod whois; @@ -26,6 +26,35 @@ pub async fn role(_ctx: Context<'_>) -> Result<(), Error> { Ok(()) } +/// Edit a user's personal role, creates it with some default values if it doesn't exist. +pub async fn edit_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit: EditRole<'a>, db: E) -> Result + where E: PgExecutor<'a> + Clone, +{ + if let Some(role) = get_user_role(user, guild, db.clone()).await? { + guild.role(ctx, role).await?.edit(ctx, edit).await?; + Ok(role) + } else { + create_role(ctx, user, guild, edit, db).await + } +} + +async fn create_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit: EditRole<'a>, db: E) -> Result + where E: PgExecutor<'a>, +{ + let def = EditRole::new() + .name(user.to_user(ctx).await?.name) + .permissions(Permissions::empty()); + + let member = guild.member(ctx, user).await?; + + let mut role = guild.create_role(ctx, def).await?; + role.edit(ctx, edit).await?; + member.add_role(ctx, &role).await?; + update_user_role(user, guild, role.id, db).await?; + + Ok(role.id) +} + /// Remove a row concerning a user's self role from the database pub async fn remove_user_role<'a, E>(user: UserId, guild: GuildId, db: E) -> Result<(), Error> where E: PgExecutor<'a>, diff --git a/src/commands/self_roles/name.rs b/src/commands/self_roles/name.rs index 58760c1..314f901 100644 --- a/src/commands/self_roles/name.rs +++ b/src/commands/self_roles/name.rs @@ -1,7 +1,7 @@ use crate::common::{self, Context, Error}; -use poise::serenity_prelude::{EditRole, Permissions}; +use poise::serenity_prelude::EditRole; /// Change the name of your personal role #[poise::command(slash_command, prefix_command)] @@ -15,20 +15,8 @@ pub async fn name(ctx: Context<'_>, name: String) -> Result<(), Error> { 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?; - } + let role = super::edit_role(ctx, user.id, guild, EditRole::new().name(name), &ctx.data().database).await?; + common::no_ping_reply(&ctx, format!("{} has been updated.", guild.role(ctx, role).await?)).await?; Ok(()) } From 792bc70cd025356ae32a0e386091773cd459c1fa Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Thu, 20 Feb 2025 22:30:35 -0500 Subject: [PATCH 3/4] better setting handling, position setting --- src/commands/self_roles/mod.rs | 10 +++- src/commands/settings.rs | 99 ++++++++++++++++++++++++++++++---- src/main.rs | 8 +-- 3 files changed, 100 insertions(+), 17 deletions(-) diff --git a/src/commands/self_roles/mod.rs b/src/commands/self_roles/mod.rs index 52d50b7..e6587e1 100644 --- a/src/commands/self_roles/mod.rs +++ b/src/commands/self_roles/mod.rs @@ -39,11 +39,17 @@ pub async fn edit_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, ed } async fn create_role<'a, E>(ctx: Context<'a>, user: UserId, guild: GuildId, edit: EditRole<'a>, db: E) -> Result - where E: PgExecutor<'a>, + where E: PgExecutor<'a> + Clone, { let def = EditRole::new() .name(user.to_user(ctx).await?.name) - .permissions(Permissions::empty()); + .permissions(Permissions::empty()) + .position({ + match crate::commands::settings::get_positional_role(ctx, guild).await? { + Some(role) => guild.role(ctx, role).await?.position, + None => 0u16, + } + }); let member = guild.member(ctx, user).await?; diff --git a/src/commands/settings.rs b/src/commands/settings.rs index 5278b70..82d864d 100644 --- a/src/commands/settings.rs +++ b/src/commands/settings.rs @@ -1,7 +1,20 @@ use crate::common::{Context, Error}; -#[poise::command(prefix_command, slash_command, required_permissions = "MANAGE_GUILD")] -async fn prefix(ctx: Context<'_>, prefix: String) -> Result<(), Error> { +use poise::serenity_prelude::{Role, RoleId, GuildId}; +use sqlx::Row; + +async fn get_prefix(ctx: Context<'_>, guild: GuildId) -> Result, Error> { + let db = &ctx.data().database; + + let prefix: Option = sqlx::query("SELECT prefix FROM settings WHERE guildid = $1") + .bind(guild.get() as i64) + .fetch_one(db).await?.get(0); + + Ok(prefix.or(ctx.data().prefix.clone())) +} + +#[poise::command(prefix_command, slash_command)] +async fn prefix(ctx: Context<'_>, prefix: Option) -> Result<(), Error> { let guild = match ctx.guild_id() { Some(g) => g, None => { @@ -10,21 +23,85 @@ async fn prefix(ctx: Context<'_>, prefix: String) -> Result<(), Error> { } }; - let mut tx = ctx.data().database.begin().await?; + match prefix { + Some(prefix) => { + if !ctx.author_member().await.unwrap().permissions.iter().any(|p| p.manage_guild()) { + ctx.reply("You do not have permission to change this setting.").await?; + return Ok(()); + } - sqlx::query("INSERT INTO settings (guildid, prefix) VALUES ($1, $2) ON CONFLICT (guildid) DO UPDATE SET prefix = EXCLUDED.prefix") - .bind(guild.get() as i64) - .bind(&prefix) - .execute(&mut *tx).await?; + let mut tx = ctx.data().database.begin().await?; - tx.commit().await?; - - ctx.reply(format!("This server's custom prefix has been updated to `{prefix}`.")).await?; + sqlx::query("INSERT INTO settings (guildid, prefix) VALUES ($1, $2) ON CONFLICT (guildid) DO UPDATE SET prefix = EXCLUDED.prefix") + .bind(guild.get() as i64) + .bind(&prefix) + .execute(&mut *tx).await?; + + tx.commit().await?; + + ctx.reply(format!("This server's custom prefix has been updated to `{prefix}`.")).await?; + } + None => { + let s = get_prefix(ctx, guild).await?.map(|s| format!("`{s}`")).unwrap_or("not set".into()); + ctx.reply(format!("This server's command prefix is {s}.")).await?; + } + } Ok(()) } -#[poise::command(prefix_command, slash_command, subcommands("prefix"), subcommand_required)] +pub async fn get_positional_role(ctx: Context<'_>, guild: GuildId) -> Result, Error> { + let db = &ctx.data().database; + + let role: Option = sqlx::query("SELECT positional_role FROM settings WHERE guildid = $1") + .bind(guild.get() as i64) + .fetch_one(db).await?.get(0); + + Ok(role.map(|sf| RoleId::new(sf as u64))) +} + +#[poise::command(prefix_command, slash_command)] +pub async fn position(ctx: Context<'_>, role: 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(()); + } + }; + + if !ctx.author_member().await.unwrap().permissions.iter().any(|p| p.manage_guild()) { + ctx.reply("You do not have permission to see or change this setting.").await?; + return Ok(()); + } + + match role { + Some(role) => { + let mut tx = ctx.data().database.begin().await?; + + sqlx::query("INSERT INTO settings (guildid, positional_role) VALUES ($1, $2) ON CONFLICT (guildid) DO UPDATE SET positional_role = EXCLUDED.positional_role") + .bind(guild.get() as i64) + .bind(role.id.get() as i64) + .execute(&mut *tx).await?; + + tx.commit().await?; + + ctx.reply(format!("The bot will now place newly created self roles below `{role}`.")).await?; + } + None => { + let s = match get_positional_role(ctx, guild).await? { + Some(r) => format!("{}", guild.role(ctx, r).await?), + None => "not set".into() + }; + + ctx.reply(format!("This server's positional role is {s}.")).await?; + } + } + + Ok(()) +} + +#[poise::command(prefix_command, slash_command, subcommands("prefix", "position"), 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 a5c94dc..9f3dc20 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,12 +62,11 @@ async fn get_prefix(ctx: PartialContext<'_, Data, Error>) -> Result = sqlx::query("SELECT prefix FROM settings WHERE guildid = $1") .bind(guild.get() as i64) - .fetch_one(db).await.ok() - .map(|x| x.get(0)).unwrap_or(ctx.data.prefix.clone()); + .fetch_one(db).await?.get(0); - Ok(prefix) + Ok(prefix.or(ctx.data.prefix.clone())) } #[tokio::main] @@ -164,6 +163,7 @@ async fn main() -> Result<(), Error> { r#" CREATE TABLE IF NOT EXISTS settings ( guildid BIGINT NOT NULL PRIMARY KEY, + positional_role BIGINT, prefix TEXT ) "# From 5115636f60fa4e1b0b711ba1d8e6d31121cd9aae Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Thu, 20 Feb 2025 23:00:22 -0500 Subject: [PATCH 4/4] 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 ) "#