fix permission issues with listing channels/sending messages
This commit is contained in:
parent
b2b4ac358c
commit
5e6d804380
3 changed files with 175 additions and 26 deletions
|
@ -5,6 +5,7 @@ import (
|
||||||
"math"
|
"math"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
@ -106,12 +107,21 @@ func GetSortedChannels(guildId string, withCategories bool, withPrivate bool) []
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
perms := discord.CalcOverwrites(*guild, channel, *selfMember)
|
private := false
|
||||||
|
|
||||||
private := !perms.Has(discord.PermissionViewChannel)
|
if channel.ParentID.IsValid() {
|
||||||
if perms == 0 {
|
category, err := client.ChannelStore.Channel(channel.ParentID)
|
||||||
private = false
|
if err == nil {
|
||||||
|
perms := lib.ChannelPermissionsOf(*guild, *category, *selfMember)
|
||||||
|
private = !perms.Has(discord.PermissionViewChannel)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if private {
|
||||||
|
perms := lib.ChannelPermissionsOf(*guild, channel, *selfMember)
|
||||||
|
private = !perms.Has(discord.PermissionViewChannel)
|
||||||
|
}
|
||||||
|
|
||||||
if private && !withPrivate {
|
if private && !withPrivate {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -192,12 +202,21 @@ func GetSortedChannels(guildId string, withCategories bool, withPrivate bool) []
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
perms := discord.CalcOverwrites(*guild, channel, *selfMember)
|
private := false
|
||||||
|
|
||||||
private := !perms.Has(discord.PermissionViewChannel)
|
if channel.ParentID.IsValid() {
|
||||||
if perms == 0 {
|
category, err := client.ChannelStore.Channel(channel.ParentID)
|
||||||
private = false
|
if err == nil {
|
||||||
|
perms := lib.ChannelPermissionsOf(*guild, *category, *selfMember)
|
||||||
|
private = !perms.Has(discord.PermissionViewChannel)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if private {
|
||||||
|
perms := lib.ChannelPermissionsOf(*guild, channel, *selfMember)
|
||||||
|
private = !perms.Has(discord.PermissionViewChannel)
|
||||||
|
}
|
||||||
|
|
||||||
if private && !withPrivate {
|
if private && !withPrivate {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -250,11 +269,19 @@ func ListChannelsCommand() {
|
||||||
channels := GetSortedChannels(currentGuild, true, false)
|
channels := GetSortedChannels(currentGuild, true, false)
|
||||||
|
|
||||||
for _, channel := range channels {
|
for _, channel := range channels {
|
||||||
perms := discord.CalcOverwrites(*guild, channel, *selfMember)
|
private := false
|
||||||
|
|
||||||
private := !perms.Has(discord.PermissionViewChannel)
|
if channel.ParentID.IsValid() {
|
||||||
if perms == 0 {
|
category, err := client.ChannelStore.Channel(channel.ParentID)
|
||||||
private = false
|
if err == nil {
|
||||||
|
perms := lib.ChannelPermissionsOf(*guild, *category, *selfMember)
|
||||||
|
private = !perms.Has(discord.PermissionViewChannel)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if private {
|
||||||
|
perms := lib.ChannelPermissionsOf(*guild, channel, *selfMember)
|
||||||
|
private = !perms.Has(discord.PermissionViewChannel)
|
||||||
}
|
}
|
||||||
|
|
||||||
category := channel.Type == discord.GuildCategory
|
category := channel.Type == discord.GuildCategory
|
||||||
|
@ -279,12 +306,21 @@ func ListChannelsCommand() {
|
||||||
fmt.Printf(" %*s created topic\n\r", longest, "channel-name")
|
fmt.Printf(" %*s created topic\n\r", longest, "channel-name")
|
||||||
fmt.Print(strings.Repeat("-", 80) + "\n\r")
|
fmt.Print(strings.Repeat("-", 80) + "\n\r")
|
||||||
for _, channel := range channels {
|
for _, channel := range channels {
|
||||||
perms := discord.CalcOverwrites(*guild, channel, *selfMember)
|
private := false
|
||||||
|
|
||||||
private := !perms.Has(discord.PermissionViewChannel)
|
if channel.ParentID.IsValid() {
|
||||||
if perms == 0 {
|
category, err := client.ChannelStore.Channel(channel.ParentID)
|
||||||
private = false
|
if err == nil {
|
||||||
|
perms := lib.ChannelPermissionsOf(*guild, *category, *selfMember)
|
||||||
|
private = !perms.Has(discord.PermissionViewChannel)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if private {
|
||||||
|
perms := lib.ChannelPermissionsOf(*guild, channel, *selfMember)
|
||||||
|
private = !perms.Has(discord.PermissionViewChannel)
|
||||||
|
}
|
||||||
|
|
||||||
category := channel.Type == discord.GuildCategory
|
category := channel.Type == discord.GuildCategory
|
||||||
topic := REGEX_EMOTE.ReplaceAllString(channel.Topic, ":$1:")
|
topic := REGEX_EMOTE.ReplaceAllString(channel.Topic, ":$1:")
|
||||||
topic = strings.ReplaceAll(topic, "\n", " ")
|
topic = strings.ReplaceAll(topic, "\n", " ")
|
||||||
|
@ -380,13 +416,19 @@ func ListUsersCommand() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
perms := discord.CalcOverwrites(*guild, *channel, *member)
|
private := false
|
||||||
private := !perms.Has(discord.PermissionViewChannel)
|
|
||||||
if perms == 0 {
|
if channel.ParentID.IsValid() {
|
||||||
private = false
|
category, err := client.ChannelStore.Channel(channel.ParentID)
|
||||||
|
if err == nil {
|
||||||
|
perms := lib.ChannelPermissionsOf(*guild, *category, *member)
|
||||||
|
private = !perms.Has(discord.PermissionViewChannel)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if private {
|
if private {
|
||||||
continue
|
perms := lib.ChannelPermissionsOf(*guild, *channel, *member)
|
||||||
|
private = !perms.Has(discord.PermissionViewChannel)
|
||||||
}
|
}
|
||||||
|
|
||||||
length := utf8.RuneCountInString(member.User.Username) + 3
|
length := utf8.RuneCountInString(member.User.Username) + 3
|
||||||
|
@ -451,6 +493,25 @@ func ListUsersCommand() {
|
||||||
for _, position := range positions {
|
for _, position := range positions {
|
||||||
members := membersByPosition[position]
|
members := membersByPosition[position]
|
||||||
if len(members) > 150 {
|
if len(members) > 150 {
|
||||||
|
str := "[hiding " + strconv.Itoa(len(members)) + " members]"
|
||||||
|
length := utf8.RuneCountInString(str)
|
||||||
|
|
||||||
|
index++
|
||||||
|
|
||||||
|
pad := 0
|
||||||
|
if index % columns != 0 {
|
||||||
|
pad = longest - length
|
||||||
|
}
|
||||||
|
if pad < 0 {
|
||||||
|
pad = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf(str + strings.Repeat(" ", pad))
|
||||||
|
|
||||||
|
if index % columns == 0 {
|
||||||
|
fmt.Print("\n\r")
|
||||||
|
}
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, member := range members {
|
for _, member := range members {
|
||||||
|
|
|
@ -54,10 +54,19 @@ func SendMode() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
perms := discord.CalcOverwrites(*guild, *channel, *selfMember)
|
cannotSend := false
|
||||||
cannotSend := !perms.Has(discord.PermissionSendMessages)
|
|
||||||
if perms == 0 {
|
if channel.ParentID.IsValid() {
|
||||||
cannotSend = false
|
category, err := client.ChannelStore.Channel(channel.ParentID)
|
||||||
|
if err == nil {
|
||||||
|
perms := lib.ChannelPermissionsOf(*guild, *category, *selfMember)
|
||||||
|
cannotSend = !perms.Has(discord.PermissionSendMessages)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if cannotSend {
|
||||||
|
perms := lib.ChannelPermissionsOf(*guild, *channel, *selfMember)
|
||||||
|
cannotSend = !perms.Has(discord.PermissionSendMessages)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cannotSend {
|
if cannotSend {
|
||||||
|
|
79
lib/util.go
Normal file
79
lib/util.go
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
package lib
|
||||||
|
|
||||||
|
import "github.com/diamondburned/arikawa/v3/discord"
|
||||||
|
|
||||||
|
func GuildPermissionsOf(guild discord.Guild, member discord.Member) discord.Permissions {
|
||||||
|
if guild.OwnerID == member.User.ID {
|
||||||
|
return discord.PermissionAll
|
||||||
|
}
|
||||||
|
|
||||||
|
var perm discord.Permissions
|
||||||
|
|
||||||
|
for _, role := range guild.Roles {
|
||||||
|
if role.ID == discord.RoleID(guild.ID) {
|
||||||
|
perm |= role.Permissions
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if perm.Has(discord.PermissionAdministrator) {
|
||||||
|
return discord.PermissionAll
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, role := range guild.Roles {
|
||||||
|
for _, id := range member.RoleIDs {
|
||||||
|
if id == role.ID {
|
||||||
|
perm |= role.Permissions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if perm.Has(discord.PermissionAdministrator) {
|
||||||
|
return discord.PermissionAll
|
||||||
|
}
|
||||||
|
|
||||||
|
return perm
|
||||||
|
}
|
||||||
|
|
||||||
|
func ChannelPermissionsOf(guild discord.Guild, channel discord.Channel, member discord.Member) discord.Permissions {
|
||||||
|
perm := GuildPermissionsOf(guild, member)
|
||||||
|
|
||||||
|
if perm.Has(discord.PermissionAdministrator) {
|
||||||
|
return discord.PermissionAll
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, overwrite := range channel.Overwrites {
|
||||||
|
if discord.GuildID(overwrite.ID) == guild.ID {
|
||||||
|
perm &= ^overwrite.Deny
|
||||||
|
perm |= overwrite.Allow
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var deny, allow discord.Permissions
|
||||||
|
|
||||||
|
for _, overwrite := range channel.Overwrites {
|
||||||
|
for _, id := range member.RoleIDs {
|
||||||
|
if id == discord.RoleID(overwrite.ID) && overwrite.Type == discord.OverwriteRole {
|
||||||
|
deny |= overwrite.Deny
|
||||||
|
allow |= overwrite.Allow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
perm &= ^deny
|
||||||
|
perm |= allow
|
||||||
|
|
||||||
|
for _, overwrite := range channel.Overwrites {
|
||||||
|
if discord.UserID(overwrite.ID) == member.User.ID && overwrite.Type == discord.OverwriteMember {
|
||||||
|
perm &= ^overwrite.Deny
|
||||||
|
perm |= overwrite.Allow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if perm.Has(discord.PermissionAdministrator) {
|
||||||
|
return discord.PermissionAll
|
||||||
|
}
|
||||||
|
|
||||||
|
return perm
|
||||||
|
}
|
Loading…
Reference in a new issue