Explorar el Código

Added UserChannelPermissions function

abalabahaha hace 8 años
padre
commit
cd24674ebb
Se han modificado 2 ficheros con 101 adiciones y 0 borrados
  1. 66 0
      restapi.go
  2. 35 0
      structs.go

+ 66 - 0
restapi.go

@@ -327,6 +327,72 @@ 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
+	}
+
+	apermissions = 0
+
+	for _, role := range guild.Roles {
+		for _, roleID := range member.Roles {
+			if role.ID == roleID {
+				apermissions = 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 = apermissions & ^overwrite.Deny
+				apermissions = apermissions | overwrite.Allow
+				break
+			}
+		}
+	}
+
+	for _, overwrite := range channel.PermissionOverwrites {
+		if overwrite.Type == "member" && overwrite.ID == userID {
+			apermissions = apermissions & ^overwrite.Deny
+			apermissions = apermissions | overwrite.Allow
+			break
+		}
+	}
+
+	if apermissions & PermissionManageRoles > 0 {
+		apermissions |= PermissionAllChannel
+	}
+
+	return
+}
+
 // ------------------------------------------------------------------------------------------------
 // Functions specific to Discord Guilds
 // ------------------------------------------------------------------------------------------------

+ 35 - 0
structs.go

@@ -365,3 +365,38 @@ type State struct {
 	Ready
 	MaxMessageCount int
 }
+
+// Constants for the different bit offsets of general permissions
+const (
+	PermissionCreateInstantInvite = 1 << iota
+	PermissionKickMembers
+	PermissionBanMembers
+	PermissionManageRoles
+	PermissionManageChannels
+	PermissionManageServer
+
+	PermissionAll        = 66321471
+	PermissionAllChannel = 66321433
+)
+
+// 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
+)