Compare commits

...

3 commits

5 changed files with 189 additions and 30 deletions

View file

@ -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,13 +269,21 @@ 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
catLen := 0 catLen := 0
@ -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 {

View file

@ -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 {

View file

@ -227,7 +227,7 @@ func FormatMessage(options MessageOptions) []string {
if options.Reply != nil { if options.Reply != nil {
nameColor := "cyan+b" nameColor := "cyan+b"
if options.Bot { if options.Reply.Author.Bot {
nameColor = "yellow+b" nameColor = "yellow+b"
} }

79
lib/util.go Normal file
View 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
}

16
main.go
View file

@ -15,6 +15,9 @@ import (
"github.com/Cynosphere/comcord/state" "github.com/Cynosphere/comcord/state"
"github.com/diamondburned/arikawa/v3/discord" "github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway" "github.com/diamondburned/arikawa/v3/gateway"
"github.com/diamondburned/arikawa/v3/session"
arikawa_state "github.com/diamondburned/arikawa/v3/state"
"github.com/diamondburned/arikawa/v3/state/store/defaultstore"
"github.com/diamondburned/arikawa/v3/utils/handler" "github.com/diamondburned/arikawa/v3/utils/handler"
"github.com/diamondburned/ningen/v3" "github.com/diamondburned/ningen/v3"
"golang.org/x/term" "golang.org/x/term"
@ -121,7 +124,15 @@ func main() {
presence.Activities = append(presence.Activities, activity) presence.Activities = append(presence.Activities, activity)
ident.Presence = &presence ident.Presence = &presence
client := ningen.NewWithIdentifier(gateway.NewIdentifier(ident)) gwURL, err := gateway.URL(context.Background())
if err != nil {
fmt.Print("% Failed to get gateway URL: ", err, "\n\r")
os.Exit(1)
}
gw := gateway.NewCustomWithIdentifier(gateway.AddGatewayParams(gwURL), gateway.NewIdentifier(ident), nil)
ses := session.NewWithGateway(gw, handler.New())
st := arikawa_state.NewFromSession(ses, defaultstore.New())
client := ningen.FromState(st)
client.PreHandler = handler.New() client.PreHandler = handler.New()
client.AddIntents(gateway.IntentGuilds) client.AddIntents(gateway.IntentGuilds)
@ -138,8 +149,7 @@ func main() {
err = client.Open(context.Background()) err = client.Open(context.Background())
if err != nil { if err != nil {
fmt.Println("% Failed to connect to Discord:", err) fmt.Print("% Failed to connect to Discord: ", err, "\n\r")
fmt.Print("\r")
os.Exit(1) os.Exit(1)
return return
} }