diff --git a/commands/guild.go b/commands/guild.go index 9d2bea1..fdf1e91 100644 --- a/commands/guild.go +++ b/commands/guild.go @@ -107,25 +107,21 @@ func GetSortedChannels(guildId string, withCategories bool, withPrivate bool) [] continue } - perms := lib.CalcPermissionsFixed(*guild, channel, *selfMember) + private := false - private := !perms.Has(discord.PermissionViewChannel) - fmt.Print(channel.Name, " ", perms, " ", private, "\n\r") - if perms == 0 { - if channel.ParentID.IsValid() { - 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 channel.ParentID.IsValid() { + category, err := client.ChannelStore.Channel(channel.ParentID) + 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 { continue } @@ -206,23 +202,21 @@ func GetSortedChannels(guildId string, withCategories bool, withPrivate bool) [] continue } - perms := lib.CalcPermissionsFixed(*guild, channel, *selfMember) + private := false - private := !perms.Has(discord.PermissionViewChannel) - if perms == 0 { - if channel.ParentID.IsValid() { - category, err := client.ChannelStore.Channel(channel.ParentID) - if err == nil { - perms := lib.CalcPermissionsFixed(*guild, *category, *selfMember) - private = !perms.Has(discord.PermissionViewChannel) - if perms == 0 { - private = false - } - } - } else { - private = false + if channel.ParentID.IsValid() { + category, err := client.ChannelStore.Channel(channel.ParentID) + 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 { continue } @@ -275,24 +269,21 @@ func ListChannelsCommand() { channels := GetSortedChannels(currentGuild, true, false) for _, channel := range channels { - perms := lib.CalcPermissionsFixed(*guild, channel, *selfMember) + private := false - private := !perms.Has(discord.PermissionViewChannel) - if perms == 0 { - if channel.ParentID.IsValid() { - category, err := client.ChannelStore.Channel(channel.ParentID) - if err == nil { - perms := lib.CalcPermissionsFixed(*guild, *category, *selfMember) - private = !perms.Has(discord.PermissionViewChannel) - if perms == 0 { - private = false - } - } - } else { - private = false + if channel.ParentID.IsValid() { + category, err := client.ChannelStore.Channel(channel.ParentID) + 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 catLen := 0 @@ -315,23 +306,21 @@ func ListChannelsCommand() { fmt.Printf(" %*s created topic\n\r", longest, "channel-name") fmt.Print(strings.Repeat("-", 80) + "\n\r") for _, channel := range channels { - perms := lib.CalcPermissionsFixed(*guild, channel, *selfMember) + private := false - private := !perms.Has(discord.PermissionViewChannel) - if perms == 0 { - if channel.ParentID.IsValid() { - category, err := client.ChannelStore.Channel(channel.ParentID) - if err == nil { - perms := lib.CalcPermissionsFixed(*guild, *category, *selfMember) - private = !perms.Has(discord.PermissionViewChannel) - if perms == 0 { - private = false - } - } - } else { - private = false + if channel.ParentID.IsValid() { + category, err := client.ChannelStore.Channel(channel.ParentID) + 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 topic := REGEX_EMOTE.ReplaceAllString(channel.Topic, ":$1:") topic = strings.ReplaceAll(topic, "\n", " ") @@ -427,25 +416,19 @@ func ListUsersCommand() { continue } - perms := lib.CalcPermissionsFixed(*guild, *channel, *member) + private := false - private := !perms.Has(discord.PermissionViewChannel) - if perms == 0 { - if channel.ParentID.IsValid() { - category, err := client.ChannelStore.Channel(channel.ParentID) - if err == nil { - perms := lib.CalcPermissionsFixed(*guild, *category, *member) - private = !perms.Has(discord.PermissionViewChannel) - if perms == 0 { - private = false - } - } - } else { - private = false + if channel.ParentID.IsValid() { + category, err := client.ChannelStore.Channel(channel.ParentID) + if err == nil { + perms := lib.ChannelPermissionsOf(*guild, *category, *member) + private = !perms.Has(discord.PermissionViewChannel) } } + if private { - continue + perms := lib.ChannelPermissionsOf(*guild, *channel, *member) + private = !perms.Has(discord.PermissionViewChannel) } length := utf8.RuneCountInString(member.User.Username) + 3 diff --git a/commands/send.go b/commands/send.go index 22034a1..2e5fe5c 100644 --- a/commands/send.go +++ b/commands/send.go @@ -54,23 +54,21 @@ func SendMode() { return } - perms := lib.CalcPermissionsFixed(*guild, *channel, *selfMember) - cannotSend := !perms.Has(discord.PermissionSendMessages) - if perms == 0 { - if channel.ParentID.IsValid() { - category, err := client.ChannelStore.Channel(channel.ParentID) - if err == nil { - perms := lib.CalcPermissionsFixed(*guild, *category, *selfMember) - cannotSend = perms.Has(discord.PermissionSendMessages) - if perms == 0 { - cannotSend = false - } - } - } else { - cannotSend = false + cannotSend := false + + if channel.ParentID.IsValid() { + 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 { fmt.Print("\n\r") return diff --git a/lib/util.go b/lib/util.go index 1a1968d..1f15385 100644 --- a/lib/util.go +++ b/lib/util.go @@ -2,7 +2,7 @@ package lib import "github.com/diamondburned/arikawa/v3/discord" -func CalcPermissionsFixed(guild discord.Guild, channel discord.Channel, member discord.Member) discord.Permissions { +func GuildPermissionsOf(guild discord.Guild, member discord.Member) discord.Permissions { if guild.OwnerID == member.User.ID { return discord.PermissionAll } @@ -16,6 +16,10 @@ func CalcPermissionsFixed(guild discord.Guild, channel discord.Channel, member d } } + if perm.Has(discord.PermissionAdministrator) { + return discord.PermissionAll + } + for _, role := range guild.Roles { for _, id := range member.RoleIDs { if id == role.ID { @@ -28,6 +32,16 @@ func CalcPermissionsFixed(guild discord.Guild, channel discord.Channel, member d 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