Compare commits
3 commits
fe836f61dc
...
5e6d804380
Author | SHA1 | Date | |
---|---|---|---|
5e6d804380 | |||
b2b4ac358c | |||
4f21449ffe |
5 changed files with 189 additions and 30 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"math"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
|
@ -106,12 +107,21 @@ func GetSortedChannels(guildId string, withCategories bool, withPrivate bool) []
|
|||
continue
|
||||
}
|
||||
|
||||
perms := discord.CalcOverwrites(*guild, channel, *selfMember)
|
||||
private := false
|
||||
|
||||
private := !perms.Has(discord.PermissionViewChannel)
|
||||
if perms == 0 {
|
||||
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
|
||||
}
|
||||
|
@ -192,12 +202,21 @@ func GetSortedChannels(guildId string, withCategories bool, withPrivate bool) []
|
|||
continue
|
||||
}
|
||||
|
||||
perms := discord.CalcOverwrites(*guild, channel, *selfMember)
|
||||
private := false
|
||||
|
||||
private := !perms.Has(discord.PermissionViewChannel)
|
||||
if perms == 0 {
|
||||
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
|
||||
}
|
||||
|
@ -250,11 +269,19 @@ func ListChannelsCommand() {
|
|||
channels := GetSortedChannels(currentGuild, true, false)
|
||||
|
||||
for _, channel := range channels {
|
||||
perms := discord.CalcOverwrites(*guild, channel, *selfMember)
|
||||
private := false
|
||||
|
||||
private := !perms.Has(discord.PermissionViewChannel)
|
||||
if perms == 0 {
|
||||
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
|
||||
|
@ -279,12 +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 := discord.CalcOverwrites(*guild, channel, *selfMember)
|
||||
private := false
|
||||
|
||||
private := !perms.Has(discord.PermissionViewChannel)
|
||||
if perms == 0 {
|
||||
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", " ")
|
||||
|
@ -380,13 +416,19 @@ func ListUsersCommand() {
|
|||
continue
|
||||
}
|
||||
|
||||
perms := discord.CalcOverwrites(*guild, *channel, *member)
|
||||
private := !perms.Has(discord.PermissionViewChannel)
|
||||
if perms == 0 {
|
||||
private = false
|
||||
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
|
||||
|
@ -451,6 +493,25 @@ func ListUsersCommand() {
|
|||
for _, position := range positions {
|
||||
members := membersByPosition[position]
|
||||
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
|
||||
}
|
||||
for _, member := range members {
|
||||
|
|
|
@ -54,10 +54,19 @@ func SendMode() {
|
|||
return
|
||||
}
|
||||
|
||||
perms := discord.CalcOverwrites(*guild, *channel, *selfMember)
|
||||
cannotSend := !perms.Has(discord.PermissionSendMessages)
|
||||
if perms == 0 {
|
||||
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 {
|
||||
|
|
|
@ -227,7 +227,7 @@ func FormatMessage(options MessageOptions) []string {
|
|||
|
||||
if options.Reply != nil {
|
||||
nameColor := "cyan+b"
|
||||
if options.Bot {
|
||||
if options.Reply.Author.Bot {
|
||||
nameColor = "yellow+b"
|
||||
}
|
||||
|
||||
|
|
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
|
||||
}
|
16
main.go
16
main.go
|
@ -15,6 +15,9 @@ import (
|
|||
"github.com/Cynosphere/comcord/state"
|
||||
"github.com/diamondburned/arikawa/v3/discord"
|
||||
"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/ningen/v3"
|
||||
"golang.org/x/term"
|
||||
|
@ -121,7 +124,15 @@ func main() {
|
|||
presence.Activities = append(presence.Activities, activity)
|
||||
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.AddIntents(gateway.IntentGuilds)
|
||||
|
@ -138,8 +149,7 @@ func main() {
|
|||
|
||||
err = client.Open(context.Background())
|
||||
if err != nil {
|
||||
fmt.Println("% Failed to connect to Discord:", err)
|
||||
fmt.Print("\r")
|
||||
fmt.Print("% Failed to connect to Discord: ", err, "\n\r")
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue