@@ -270,7 +270,7 @@ impl Guild {
270270 let member = self . members . get ( & uid) ?;
271271 self . channels . iter ( ) . find ( |& channel| {
272272 channel. kind != ChannelType :: Category
273- && self . user_permissions_in ( channel, member) . view_channel ( )
273+ && self . member_permissions_in ( channel, member) . view_channel ( )
274274 } )
275275 }
276276
@@ -285,7 +285,7 @@ impl Guild {
285285 && self
286286 . members
287287 . iter ( )
288- . map ( |member| self . user_permissions_in ( channel, member) )
288+ . map ( |member| self . member_permissions_in ( channel, member) )
289289 . all ( Permissions :: view_channel)
290290 } )
291291 }
@@ -804,8 +804,8 @@ impl Guild {
804804
805805 /// Calculate a [`Member`]'s permissions in a given channel in the guild.
806806 #[ must_use]
807- pub fn user_permissions_in ( & self , channel : & GuildChannel , member : & Member ) -> Permissions {
808- Self :: user_permissions_in_ (
807+ pub fn member_permissions_in ( & self , channel : & GuildChannel , member : & Member ) -> Permissions {
808+ Self :: member_permissions_in_ (
809809 channel,
810810 member. user . id ,
811811 & member. roles ,
@@ -831,7 +831,7 @@ impl Guild {
831831 assert_eq ! ( user. id, member_id, "User::id does not match provided PartialMember" ) ;
832832 }
833833
834- Self :: user_permissions_in_ (
834+ Self :: member_permissions_in_ (
835835 channel,
836836 member_id,
837837 & member. roles ,
@@ -841,8 +841,84 @@ impl Guild {
841841 )
842842 }
843843
844+ /// Calculate a [`Member`]'s permissions in the guild.
845+ ///
846+ /// **Note**: This method calculates the permissions granted to the member across the entire
847+ /// guild, excluding any channel-specific overrides. If you are not checking permissions that
848+ /// do not apply on the channel level, like `Permissions::BAN_MEMBERS`, use
849+ /// member_permissions_in instead.
850+ #[ must_use]
851+ pub fn member_permissions_guild_level ( & self , member : & Member ) -> Permissions {
852+ Self :: member_permissions_guild_level_ (
853+ member. user . id ,
854+ & member. roles ,
855+ self . id ,
856+ self . owner_id ,
857+ & self . roles ,
858+ )
859+ }
860+
861+ /// Calculate a [`PartialMember`]'s permissions in the guild.
862+ ///
863+ /// See `Self::member_permissions_guild_level` for note on usage.
864+ ///
865+ /// # Panics
866+ ///
867+ /// Panics if the passed [`UserId`] does not match the [`PartialMember`] id, if user is Some.
868+ #[ must_use]
869+ pub fn partial_member_permissions_guild_level (
870+ & self ,
871+ member_id : UserId ,
872+ member : & PartialMember ,
873+ ) -> Permissions {
874+ if let Some ( user) = & member. user {
875+ assert_eq ! ( user. id, member_id, "User::id does not match provided PartialMember" ) ;
876+ }
877+
878+ Self :: member_permissions_guild_level_ (
879+ member_id,
880+ & member. roles ,
881+ self . id ,
882+ self . owner_id ,
883+ & self . roles ,
884+ )
885+ }
886+
887+ /// Helper function that can also be used from [`PartialGuild`].
888+ pub ( crate ) fn member_permissions_guild_level_ (
889+ user_id : UserId ,
890+ member_roles : & [ RoleId ] ,
891+ guild_id : GuildId ,
892+ guild_owner_id : UserId ,
893+ guild_roles : & ExtractMap < RoleId , Role > ,
894+ ) -> Permissions {
895+ if user_id == guild_owner_id {
896+ return Permissions :: all ( ) ;
897+ }
898+
899+ //// Get @everyone permissions first.
900+ let mut permissions = if let Some ( role) = guild_roles. get ( & RoleId :: new ( guild_id. get ( ) ) ) {
901+ role. permissions
902+ } else {
903+ error ! ( "@everyone role missing in {}" , guild_id) ;
904+ Permissions :: empty ( )
905+ } ;
906+
907+ for role_id in member_roles {
908+ if let Some ( role) = guild_roles. get ( role_id) {
909+ permissions |= role. permissions ;
910+ }
911+ }
912+
913+ if permissions. contains ( Permissions :: ADMINISTRATOR ) {
914+ return Permissions :: all ( ) ;
915+ }
916+
917+ permissions
918+ }
919+
844920 /// Helper function that can also be used from [`PartialGuild`].
845- pub ( crate ) fn user_permissions_in_ (
921+ pub ( crate ) fn member_permissions_in_ (
846922 channel : & GuildChannel ,
847923 member_user_id : UserId ,
848924 member_roles : & [ RoleId ] ,
0 commit comments