Ver código fonte

Merge pull request #140 from abalabahaha/develop

Added UserChannelPermissions function
Bruce 8 anos atrás
pai
commit
f9db2f7ed6
2 arquivos alterados com 120 adições e 0 exclusões
  1. 64 0
      restapi.go
  2. 56 0
      structs.go

+ 64 - 0
restapi.go

@@ -327,6 +327,70 @@ func (s *Session) UserGuilds() (st []*Guild, err error) {
 	return
 }
 
+// UserChannelPermissions returns the permission of a user in a channel.
+// userID    : The ID of the user to calculate permissions for.
+// channelID : The ID of the channel to calculate permission for.
+func (s *Session) UserChannelPermissions(userID, channelID string) (apermissions int, err error) {
+
+	channel, err := s.Channel(channelID)
+	if err != nil {
+		return
+	}
+
+	guild, err := s.Guild(channel.GuildID)
+	if err != nil {
+		return
+	}
+
+	if userID == guild.OwnerID {
+		apermissions = PermissionAll
+		return
+	}
+
+	member, err := s.GuildMember(guild.ID, userID)
+	if err != nil {
+		return
+	}
+
+	for _, role := range guild.Roles {
+		for _, roleID := range member.Roles {
+			if role.ID == roleID {
+				apermissions |= role.Permissions
+				break
+			}
+		}
+	}
+
+	if apermissions & PermissionManageRoles > 0 {
+		apermissions |= PermissionAll
+	}
+
+	// Member overwrites can override role overrides, so do two passes
+	for _, overwrite := range channel.PermissionOverwrites {
+		for _, roleID := range member.Roles {
+			if overwrite.Type == "role" && roleID == overwrite.ID {
+				apermissions &= ^overwrite.Deny
+				apermissions |= overwrite.Allow
+				break
+			}
+		}
+	}
+
+	for _, overwrite := range channel.PermissionOverwrites {
+		if overwrite.Type == "member" && overwrite.ID == userID {
+			apermissions &= ^overwrite.Deny
+			apermissions |= overwrite.Allow
+			break
+		}
+	}
+
+	if apermissions & PermissionManageRoles > 0 {
+		apermissions |= PermissionAllChannel
+	}
+
+	return
+}
+
 // ------------------------------------------------------------------------------------------------
 // Functions specific to Discord Guilds
 // ------------------------------------------------------------------------------------------------

+ 56 - 0
structs.go

@@ -365,3 +365,59 @@ type State struct {
 	Ready
 	MaxMessageCount int
 }
+
+// Constants for the different bit offsets of text channel permissions
+const (
+	PermissionReadMessages = 1 << (iota + 10)
+	PermissionSendMessages
+	PermissionSendTTSMessages
+	PermissionManageMessages
+	PermissionEmbedLinks
+	PermissionAttachFiles
+	PermissionReadMessageHistory
+	PermissionMentionEveryone
+)
+
+// Constants for the different bit offsets of voice permissions
+const (
+	PermissionVoiceConnect = 1 << (iota + 20)
+	PermissionVoiceSpeak
+	PermissionVoiceMuteMembers
+	PermissionVoiceDeafenMembers
+	PermissionVoiceMoveMembers
+	PermissionVoiceUseVAD
+)
+
+// Constants for the different bit offsets of general permissions
+const (
+	PermissionCreateInstantInvite = 1 << iota
+	PermissionKickMembers
+	PermissionBanMembers
+	PermissionManageRoles
+	PermissionManageChannels
+	PermissionManageServer
+
+	PermissionAllText    = PermissionReadMessages |
+	                       PermissionSendMessages |
+	                       PermissionSendTTSMessages |
+	                       PermissionManageMessages |
+	                       PermissionEmbedLinks |
+	                       PermissionAttachFiles |
+	                       PermissionReadMessageHistory |
+	                       PermissionMentionEveryone
+	PermissionAllVoice   = PermissionVoiceConnect |
+	                       PermissionVoiceSpeak |
+	                       PermissionVoiceMuteMembers |
+	                       PermissionVoiceDeafenMembers |
+	                       PermissionVoiceMoveMembers |
+	                       PermissionVoiceUseVAD
+	PermissionAllChannel = PermissionAllText |
+	                       PermissionAllVoice |
+	                       PermissionCreateInstantInvite |
+	                       PermissionManageRoles |
+	                       PermissionManageChannels
+	PermissionAll        = PermissionAllChannel |
+	                       PermissionKickMembers |
+	                       PermissionBanMembers |
+	                       PermissionManageServer
+)