Compare commits

..

No commits in common. "ca09e35027175c00b5ab850b5eaa9520ed9a7545" and "a9b3accdf69966a4762bdacc022a1f096c686724" have entirely different histories.

2 changed files with 16 additions and 107 deletions

View file

@ -3,7 +3,6 @@ package commands
import ( import (
"fmt" "fmt"
"math" "math"
"regexp"
"sort" "sort"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
@ -13,8 +12,6 @@ import (
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
var REGEX_EMOTE = regexp.MustCompile(`<(?:\x{200b}|&)?a?:(\w+):(\d+)>`)
type GuildListing struct { type GuildListing struct {
Name string Name string
Members int Members int
@ -58,90 +55,24 @@ func ListGuildsCommand(session *discordgo.Session) {
fmt.Print("\n\r") fmt.Print("\n\r")
} }
func GetSortedChannels(session *discordgo.Session, guildId string, withCategories bool) []*discordgo.Channel { func GetSortedChannels(session *discordgo.Session, guildId string) []*discordgo.Channel {
channels := make([]*discordgo.Channel, 0) channels := make([]*discordgo.Channel, 0)
guild, err := session.State.Guild(guildId) guild, err := session.State.Guild(guildId)
if err != nil { if err != nil {
return channels return channels
} }
if withCategories { for _, channel := range guild.Channels {
categories := make(map[string][]*discordgo.Channel) if channel.Type != discordgo.ChannelTypeGuildText && channel.Type != discordgo.ChannelTypeGuildNews {
continue
for _, channel := range guild.Channels {
categoryID := "0"
if channel.ParentID != "" {
categoryID = channel.ParentID
}
_, has := categories[categoryID]
if !has {
categories[categoryID] = make([]*discordgo.Channel, 0)
}
if channel.Type != discordgo.ChannelTypeGuildText && channel.Type != discordgo.ChannelTypeGuildNews {
continue
}
categories[categoryID] = append(categories[categoryID], channel)
} }
channels = append(channels, channel)
for id, channels := range categories {
sort.Slice(channels, func(i, j int) bool {
return channels[i].Position < channels[j].Position
})
categoryChannels := make([]*discordgo.Channel, 0)
if id != "0" {
for _, channel := range guild.Channels {
if channel.ID == id {
categoryChannels = append(categoryChannels, channel)
break
}
}
}
for _, channel := range channels {
categoryChannels = append(categoryChannels, channel)
}
categories[id] = categoryChannels
}
keys := make([]string, 0, len(categories) - 1)
for id := range categories {
if id == "0" {
continue
}
keys = append(keys, id)
}
sort.Slice(keys, func(i, j int) bool {
ca, _ := session.State.Channel(keys[i])
cb, _ := session.State.Channel(keys[j])
return ca.Position < cb.Position
})
sortedCategories := make(map[string][]*discordgo.Channel)
sortedCategories["0"] = categories["0"]
for _, id := range keys {
sortedCategories[id] = categories[id]
}
for _, categoryChannels := range sortedCategories {
for _, channel := range categoryChannels {
channels = append(channels, channel)
}
}
} else {
for _, channel := range guild.Channels {
if channel.Type != discordgo.ChannelTypeGuildText && channel.Type != discordgo.ChannelTypeGuildNews {
continue
}
channels = append(channels, channel)
}
sort.Slice(channels, func(i, j int) bool {
return channels[i].Position < channels[j].Position
})
} }
sort.Slice(channels, func(i, j int) bool {
return channels[i].Position < channels[j].Position
})
return channels return channels
} }
@ -153,7 +84,7 @@ func ListChannelsCommand(session *discordgo.Session) {
} }
longest := 0 longest := 0
channels := GetSortedChannels(session, currentGuild, true) channels := GetSortedChannels(session, currentGuild)
for _, channel := range channels { for _, channel := range channels {
perms, err := session.State.UserChannelPermissions(session.State.User.ID, channel.ID) perms, err := session.State.UserChannelPermissions(session.State.User.ID, channel.ID)
@ -162,18 +93,12 @@ func ListChannelsCommand(session *discordgo.Session) {
} }
private := perms & discordgo.PermissionViewChannel == 0 private := perms & discordgo.PermissionViewChannel == 0
category := channel.Type == discordgo.ChannelTypeGuildCategory
catLen := 0
if category {
catLen = 6
}
privLen := 0 privLen := 0
if private { if private {
privLen = 1 privLen = 1
} }
length := utf8.RuneCountInString(channel.Name) + privLen + catLen length := utf8.RuneCountInString(channel.Name) + privLen
if length > longest { if length > longest {
longest = int(math.Min(25, float64(length))) longest = int(math.Min(25, float64(length)))
@ -181,7 +106,7 @@ func ListChannelsCommand(session *discordgo.Session) {
} }
fmt.Print("\n\r") fmt.Print("\n\r")
fmt.Printf(" %*s created topic\n\r", longest, "channel-name") fmt.Printf(" %*s 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, err := session.State.UserChannelPermissions(session.State.User.ID, channel.ID) perms, err := session.State.UserChannelPermissions(session.State.User.ID, channel.ID)
@ -190,13 +115,8 @@ func ListChannelsCommand(session *discordgo.Session) {
} }
private := perms & discordgo.PermissionViewChannel == 0 private := perms & discordgo.PermissionViewChannel == 0
category := channel.Type == discordgo.ChannelTypeGuildCategory topic := strings.ReplaceAll(channel.Topic, "\n", " ")
topic := REGEX_EMOTE.ReplaceAllString(channel.Topic, ":$1:")
topic = strings.ReplaceAll(topic, "\n", " ")
name := channel.Name name := channel.Name
if category {
name = "-- " + name + " --"
}
if private { if private {
name = "*" + name name = "*" + name
} }
@ -207,18 +127,12 @@ func ListChannelsCommand(session *discordgo.Session) {
} }
topicLength := utf8.RuneCountInString(topic) topicLength := utf8.RuneCountInString(topic)
longestTopic := 80 - (longest + 5) - 11 longestTopic := 80 - (longest + 5)
if topicLength > longestTopic { if topicLength > longestTopic {
topic = topic[:(longestTopic - 1)] + "\u2026" topic = topic[:(longestTopic - 1)] + "\u2026"
} }
created := "??-???-??" fmt.Printf(" %*s %s\n\r", longest, name, topic)
timestamp, err := discordgo.SnowflakeTimestamp(channel.ID)
if err == nil {
created = timestamp.Format("02-Jan-06")
}
fmt.Printf(" %*s %s %s\n\r", longest, name, created, topic)
} }
fmt.Print(strings.Repeat("-", 80) + "\n\r") fmt.Print(strings.Repeat("-", 80) + "\n\r")
fmt.Print("\n\r") fmt.Print("\n\r")
@ -248,7 +162,7 @@ func SwitchGuild(session *discordgo.Session, input string) {
state.SetCurrentGuild(target) state.SetCurrentGuild(target)
last := state.GetLastChannel(target) last := state.GetLastChannel(target)
if last == "" { if last == "" {
channels := GetSortedChannels(session, target, false) channels := GetSortedChannels(session, target)
topChannel := channels[0] topChannel := channels[0]
state.SetCurrentChannel(topChannel.ID) state.SetCurrentChannel(topChannel.ID)

View file

@ -21,7 +21,6 @@ type MessageOptions struct {
Name string Name string
Channel string Channel string
Bot bool Bot bool
Webhook bool
Attachments []*discordgo.MessageAttachment Attachments []*discordgo.MessageAttachment
Stickers []*discordgo.Sticker Stickers []*discordgo.Sticker
Reply *discordgo.Message Reply *discordgo.Message
@ -178,8 +177,6 @@ func FormatMessage(session *discordgo.Session, options MessageOptions) {
nameColor := "cyan+b" nameColor := "cyan+b"
if options.IsMention { if options.IsMention {
nameColor = "red+b" nameColor = "red+b"
} else if options.Webhook {
nameColor = "magenta+b"
} else if options.Bot { } else if options.Bot {
nameColor = "yellow+b" nameColor = "yellow+b"
} }
@ -296,7 +293,6 @@ func ProcessMessage(session *discordgo.Session, msg *discordgo.Message, options
options.Name = msg.Author.Username options.Name = msg.Author.Username
options.Channel = msg.ChannelID options.Channel = msg.ChannelID
options.Bot = msg.Author.Bot options.Bot = msg.Author.Bot
options.Webhook = msg.WebhookID != ""
options.Attachments = msg.Attachments options.Attachments = msg.Attachments
options.Stickers = msg.StickerItems options.Stickers = msg.StickerItems
if i == 0 { if i == 0 {
@ -315,7 +311,6 @@ func ProcessMessage(session *discordgo.Session, msg *discordgo.Message, options
options.Name = msg.Author.Username options.Name = msg.Author.Username
options.Channel = msg.ChannelID options.Channel = msg.ChannelID
options.Bot = msg.Author.Bot options.Bot = msg.Author.Bot
options.Webhook = msg.WebhookID != ""
options.Attachments = msg.Attachments options.Attachments = msg.Attachments
options.Stickers = msg.StickerItems options.Stickers = msg.StickerItems
options.Reply = msg.ReferencedMessage options.Reply = msg.ReferencedMessage