From 3773e286e69ae1dc4e1fcdd13599408cb9dcbfd4 Mon Sep 17 00:00:00 2001 From: Carson Hoffman Date: Wed, 20 Jan 2021 18:11:20 -0500 Subject: [PATCH] Add v8 permissions changes --- restapi.go | 22 ++++++++++------------ state.go | 4 ++-- structs.go | 22 ++++++++++++++++------ 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/restapi.go b/restapi.go index 53970f2..4c11e2d 100644 --- a/restapi.go +++ b/restapi.go @@ -465,7 +465,7 @@ func (s *Session) UserGuildSettingsEdit(guildID string, settings *UserGuildSetti // // NOTE: This function is now deprecated and will be removed in the future. // Please see the same function inside state.go -func (s *Session) UserChannelPermissions(userID, channelID string) (apermissions int, err error) { +func (s *Session) UserChannelPermissions(userID, channelID string) (apermissions int64, err error) { // Try to just get permissions from state. apermissions, err = s.State.UserChannelPermissions(userID, channelID) if err == nil { @@ -507,7 +507,7 @@ func (s *Session) UserChannelPermissions(userID, channelID string) (apermissions // Calculates the permissions for a member. // https://support.discord.com/hc/en-us/articles/206141927-How-is-the-permission-hierarchy-structured- -func memberPermissions(guild *Guild, channel *Channel, userID string, roles []string) (apermissions int) { +func memberPermissions(guild *Guild, channel *Channel, userID string, roles []string) (apermissions int64) { if userID == guild.OwnerID { apermissions = PermissionAll return @@ -542,13 +542,11 @@ func memberPermissions(guild *Guild, channel *Channel, userID string, roles []st } } - denies := 0 - allows := 0 - + var denies, allows int64 // Member overwrites can override role overrides, so do two passes for _, overwrite := range channel.PermissionOverwrites { for _, roleID := range roles { - if overwrite.Type == "role" && roleID == overwrite.ID { + if overwrite.Type == PermissionOverwriteTypeRole && roleID == overwrite.ID { denies |= overwrite.Deny allows |= overwrite.Allow break @@ -560,7 +558,7 @@ func memberPermissions(guild *Guild, channel *Channel, userID string, roles []st apermissions |= allows for _, overwrite := range channel.PermissionOverwrites { - if overwrite.Type == "member" && overwrite.ID == userID { + if overwrite.Type == PermissionOverwriteTypeMember && overwrite.ID == userID { apermissions &= ^overwrite.Deny apermissions |= overwrite.Allow break @@ -1809,13 +1807,13 @@ func (s *Session) ChannelInviteCreate(channelID string, i Invite) (st *Invite, e // ChannelPermissionSet creates a Permission Override for the given channel. // NOTE: This func name may changed. Using Set instead of Create because // you can both create a new override or update an override with this function. -func (s *Session) ChannelPermissionSet(channelID, targetID, targetType string, allow, deny int) (err error) { +func (s *Session) ChannelPermissionSet(channelID, targetID string, targetType PermissionOverwriteType, allow, deny int) (err error) { data := struct { - ID string `json:"id"` - Type string `json:"type"` - Allow int `json:"allow"` - Deny int `json:"deny"` + ID string `json:"id"` + Type PermissionOverwriteType `json:"type"` + Allow int `json:"allow"` + Deny int `json:"deny"` }{targetID, targetType, allow, deny} _, err = s.RequestWithBucketID("PUT", EndpointChannelPermission(channelID, targetID), data, EndpointChannelPermission(channelID, "")) diff --git a/state.go b/state.go index c89f8bb..4f6479b 100644 --- a/state.go +++ b/state.go @@ -997,7 +997,7 @@ func (s *State) OnInterface(se *Session, i interface{}) (err error) { // 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 *State) UserChannelPermissions(userID, channelID string) (apermissions int, err error) { +func (s *State) UserChannelPermissions(userID, channelID string) (apermissions int64, err error) { if s == nil { return 0, ErrNilState } @@ -1022,7 +1022,7 @@ func (s *State) UserChannelPermissions(userID, channelID string) (apermissions i // MessagePermissions returns the permissions of the author of the message // in the channel in which it was sent. -func (s *State) MessagePermissions(message *Message) (apermissions int, err error) { +func (s *State) MessagePermissions(message *Message) (apermissions int64, err error) { if s == nil { return 0, ErrNilState } diff --git a/structs.go b/structs.go index 24e245f..fc4fd9a 100644 --- a/structs.go +++ b/structs.go @@ -322,12 +322,22 @@ type ChannelFollow struct { WebhookID string `json:"webhook_id"` } +// PermissionOverwriteType represents the type of resource on which +// a permission overwrite acts. +type PermissionOverwriteType int + +// The possible permission overwrite types. +const ( + PermissionOverwriteTypeRole PermissionOverwriteType = iota + PermissionOverwriteTypeMember +) + // A PermissionOverwrite holds permission overwrite data for a Channel type PermissionOverwrite struct { - ID string `json:"id"` - Type string `json:"type"` - Deny int `json:"deny"` - Allow int `json:"allow"` + ID string `json:"id"` + Type PermissionOverwriteType `json:"type"` + Deny int64 `json:"deny,string"` + Allow int64 `json:"allow,string"` } // Emoji struct holds data related to Emoji's @@ -564,7 +574,7 @@ type Guild struct { ApproximatePresenceCount int `json:"approximate_presence_count"` // Permissions of our user - Permissions int `json:"permissions"` + Permissions int64 `json:"permissions,string"` } // MessageNotifications is the notification level for a guild @@ -650,7 +660,7 @@ type Role struct { // The permissions of the role on the guild (doesn't include channel overrides). // This is a combination of bit masks; the presence of a certain permission can // be checked by performing a bitwise AND between this int and the permission. - Permissions int `json:"permissions"` + Permissions int64 `json:"permissions,string"` } // Mention returns a string which mentions the role