poise icon indicating copy to clipboard operation
poise copied to clipboard

Add guild permissions check

Open jamesbt365 opened this issue 1 year ago • 3 comments

Saves bot users doing this themselves if they prefer to have commands that follow the guild permissions instead of the current channel permissions, seems like something that should be built into the framework.

jamesbt365 avatar Jul 29 '24 13:07 jamesbt365

No idea if you specifically need this now, but for anyone else figuring this out I threw something together. It's bad, but the type checker is happy. I'm skipping a lot of checking for the example too, so don't forget that. Hope this helps someone.

let guild = ctx.guild().unwrap().to_owned();
let member = ctx.http().get_member(guild.id, ctx.author().id).await.unwrap();

if let Some(role) = guild.member_highest_role(&member) {
    if role.has_permissions(Permissions::MODERATE_MEMBERS, false) {
        // Code here
    }
} else {
    // No role, default action
}

I found that a lot of the functions like .guild() and .guild_member() from ctx made the type checker angry about futures and threads. So this is my workaround.

TaromaruYuki avatar May 13 '25 05:05 TaromaruYuki

@TaromaruYuki that is really bad bad performance wise, is a massive crash risk, doesn't even cover cases of other roles having permissions and doesn't leverage the data you already have.

This is exactly the reason this needs to have something built-in to poise, but I'd prefer you didn't clog this issue with incorrect code.

jamesbt365 avatar May 13 '25 06:05 jamesbt365

i was basing that code off some other code that was completely irrelevant. i make trash, optimize, and learn, its my autistic learning process.

so there is serenity::Member::permissions, but it's deprecated and may be removed anytime. you'll need to use Guild::user_permissions_in to check for a specific channel.

the only other way would be to get all the roles of the member and combine all the permissions of each one with bit wise or. this can be inefficient though.

TaromaruYuki avatar May 13 '25 07:05 TaromaruYuki