Added UserChannelPermissions function
This commit is contained in:
parent
d0c30f0f14
commit
cd24674ebb
2 changed files with 101 additions and 0 deletions
66
restapi.go
66
restapi.go
|
@ -327,6 +327,72 @@ func (s *Session) UserGuilds() (st []*Guild, err error) {
|
||||||
return
|
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
|
// Functions specific to Discord Guilds
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
|
35
structs.go
35
structs.go
|
@ -365,3 +365,38 @@ type State struct {
|
||||||
Ready
|
Ready
|
||||||
MaxMessageCount int
|
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
|
||||||
|
)
|
Loading…
Reference in a new issue