Compare commits

..

1 commit

3 changed files with 91 additions and 86 deletions

View file

@ -107,21 +107,25 @@ func GetSortedChannels(guildId string, withCategories bool, withPrivate bool) []
continue continue
} }
private := false perms := lib.CalcPermissionsFixed(*guild, channel, *selfMember)
if channel.ParentID.IsValid() { private := !perms.Has(discord.PermissionViewChannel)
category, err := client.ChannelStore.Channel(channel.ParentID) fmt.Print(channel.Name, " ", perms, " ", private, "\n\r")
if err == nil { if perms == 0 {
perms := lib.ChannelPermissionsOf(*guild, *category, *selfMember) if channel.ParentID.IsValid() {
private = !perms.Has(discord.PermissionViewChannel) category, err := client.ChannelStore.Channel(channel.ParentID)
if err == nil {
perms := lib.CalcPermissionsFixed(*guild, *category, *selfMember)
private = !perms.Has(discord.PermissionViewChannel)
fmt.Print(category.Name, " ", perms, " ", private, "\n\r")
if perms == 0 {
private = false
}
}
} else {
private = false
} }
} }
if private {
perms := lib.ChannelPermissionsOf(*guild, channel, *selfMember)
private = !perms.Has(discord.PermissionViewChannel)
}
if private && !withPrivate { if private && !withPrivate {
continue continue
} }
@ -202,21 +206,23 @@ func GetSortedChannels(guildId string, withCategories bool, withPrivate bool) []
continue continue
} }
private := false perms := lib.CalcPermissionsFixed(*guild, channel, *selfMember)
if channel.ParentID.IsValid() { private := !perms.Has(discord.PermissionViewChannel)
category, err := client.ChannelStore.Channel(channel.ParentID) if perms == 0 {
if err == nil { if channel.ParentID.IsValid() {
perms := lib.ChannelPermissionsOf(*guild, *category, *selfMember) category, err := client.ChannelStore.Channel(channel.ParentID)
private = !perms.Has(discord.PermissionViewChannel) if err == nil {
perms := lib.CalcPermissionsFixed(*guild, *category, *selfMember)
private = !perms.Has(discord.PermissionViewChannel)
if perms == 0 {
private = false
}
}
} else {
private = false
} }
} }
if private {
perms := lib.ChannelPermissionsOf(*guild, channel, *selfMember)
private = !perms.Has(discord.PermissionViewChannel)
}
if private && !withPrivate { if private && !withPrivate {
continue continue
} }
@ -269,21 +275,24 @@ func ListChannelsCommand() {
channels := GetSortedChannels(currentGuild, true, false) channels := GetSortedChannels(currentGuild, true, false)
for _, channel := range channels { for _, channel := range channels {
private := false perms := lib.CalcPermissionsFixed(*guild, channel, *selfMember)
if channel.ParentID.IsValid() { private := !perms.Has(discord.PermissionViewChannel)
category, err := client.ChannelStore.Channel(channel.ParentID) if perms == 0 {
if err == nil { if channel.ParentID.IsValid() {
perms := lib.ChannelPermissionsOf(*guild, *category, *selfMember) category, err := client.ChannelStore.Channel(channel.ParentID)
private = !perms.Has(discord.PermissionViewChannel) if err == nil {
perms := lib.CalcPermissionsFixed(*guild, *category, *selfMember)
private = !perms.Has(discord.PermissionViewChannel)
if perms == 0 {
private = false
}
}
} else {
private = false
} }
} }
if private {
perms := lib.ChannelPermissionsOf(*guild, channel, *selfMember)
private = !perms.Has(discord.PermissionViewChannel)
}
category := channel.Type == discord.GuildCategory category := channel.Type == discord.GuildCategory
catLen := 0 catLen := 0
@ -306,21 +315,23 @@ 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 {
private := false perms := lib.CalcPermissionsFixed(*guild, channel, *selfMember)
if channel.ParentID.IsValid() { private := !perms.Has(discord.PermissionViewChannel)
category, err := client.ChannelStore.Channel(channel.ParentID) if perms == 0 {
if err == nil { if channel.ParentID.IsValid() {
perms := lib.ChannelPermissionsOf(*guild, *category, *selfMember) category, err := client.ChannelStore.Channel(channel.ParentID)
private = !perms.Has(discord.PermissionViewChannel) if err == nil {
perms := lib.CalcPermissionsFixed(*guild, *category, *selfMember)
private = !perms.Has(discord.PermissionViewChannel)
if perms == 0 {
private = false
}
}
} else {
private = false
} }
} }
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", " ")
@ -416,19 +427,25 @@ func ListUsersCommand() {
continue continue
} }
private := false perms := lib.CalcPermissionsFixed(*guild, *channel, *member)
if channel.ParentID.IsValid() { private := !perms.Has(discord.PermissionViewChannel)
category, err := client.ChannelStore.Channel(channel.ParentID) if perms == 0 {
if err == nil { if channel.ParentID.IsValid() {
perms := lib.ChannelPermissionsOf(*guild, *category, *member) category, err := client.ChannelStore.Channel(channel.ParentID)
private = !perms.Has(discord.PermissionViewChannel) if err == nil {
perms := lib.CalcPermissionsFixed(*guild, *category, *member)
private = !perms.Has(discord.PermissionViewChannel)
if perms == 0 {
private = false
}
}
} else {
private = false
} }
} }
if private { if private {
perms := lib.ChannelPermissionsOf(*guild, *channel, *member) continue
private = !perms.Has(discord.PermissionViewChannel)
} }
length := utf8.RuneCountInString(member.User.Username) + 3 length := utf8.RuneCountInString(member.User.Username) + 3

View file

@ -54,21 +54,23 @@ func SendMode() {
return return
} }
cannotSend := false perms := lib.CalcPermissionsFixed(*guild, *channel, *selfMember)
cannotSend := !perms.Has(discord.PermissionSendMessages)
if channel.ParentID.IsValid() { if perms == 0 {
category, err := client.ChannelStore.Channel(channel.ParentID) if channel.ParentID.IsValid() {
if err == nil { category, err := client.ChannelStore.Channel(channel.ParentID)
perms := lib.ChannelPermissionsOf(*guild, *category, *selfMember) if err == nil {
cannotSend = !perms.Has(discord.PermissionSendMessages) perms := lib.CalcPermissionsFixed(*guild, *category, *selfMember)
cannotSend = perms.Has(discord.PermissionSendMessages)
if perms == 0 {
cannotSend = false
}
}
} else {
cannotSend = false
} }
} }
if cannotSend {
perms := lib.ChannelPermissionsOf(*guild, *channel, *selfMember)
cannotSend = !perms.Has(discord.PermissionSendMessages)
}
if cannotSend { if cannotSend {
fmt.Print("<you do not have permission to send messages here>\n\r") fmt.Print("<you do not have permission to send messages here>\n\r")
return return

View file

@ -2,7 +2,7 @@ package lib
import "github.com/diamondburned/arikawa/v3/discord" import "github.com/diamondburned/arikawa/v3/discord"
func GuildPermissionsOf(guild discord.Guild, member discord.Member) discord.Permissions { func CalcPermissionsFixed(guild discord.Guild, channel discord.Channel, member discord.Member) discord.Permissions {
if guild.OwnerID == member.User.ID { if guild.OwnerID == member.User.ID {
return discord.PermissionAll return discord.PermissionAll
} }
@ -16,10 +16,6 @@ func GuildPermissionsOf(guild discord.Guild, member discord.Member) discord.Perm
} }
} }
if perm.Has(discord.PermissionAdministrator) {
return discord.PermissionAll
}
for _, role := range guild.Roles { for _, role := range guild.Roles {
for _, id := range member.RoleIDs { for _, id := range member.RoleIDs {
if id == role.ID { if id == role.ID {
@ -32,16 +28,6 @@ func GuildPermissionsOf(guild discord.Guild, member discord.Member) discord.Perm
return discord.PermissionAll 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 { for _, overwrite := range channel.Overwrites {
if discord.GuildID(overwrite.ID) == guild.ID { if discord.GuildID(overwrite.ID) == guild.ID {
perm &= ^overwrite.Deny perm &= ^overwrite.Deny