list channels, switch guild, fix codeblock regex, sort commands in help, fix message update crash
This commit is contained in:
parent
80a343e4ee
commit
a9b3accdf6
6 changed files with 199 additions and 5 deletions
|
@ -2,9 +2,13 @@ package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
"github.com/Cynosphere/comcord/lib"
|
||||||
|
"github.com/Cynosphere/comcord/state"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -50,3 +54,134 @@ func ListGuildsCommand(session *discordgo.Session) {
|
||||||
fmt.Print(strings.Repeat("-", 80) + "\n\r")
|
fmt.Print(strings.Repeat("-", 80) + "\n\r")
|
||||||
fmt.Print("\n\r")
|
fmt.Print("\n\r")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetSortedChannels(session *discordgo.Session, guildId string) []*discordgo.Channel {
|
||||||
|
channels := make([]*discordgo.Channel, 0)
|
||||||
|
guild, err := session.State.Guild(guildId)
|
||||||
|
if err != nil {
|
||||||
|
return channels
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
})
|
||||||
|
|
||||||
|
return channels
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListChannelsCommand(session *discordgo.Session) {
|
||||||
|
currentGuild := state.GetCurrentGuild()
|
||||||
|
if currentGuild == "" {
|
||||||
|
fmt.Print("<not in a guild>\n\r")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
longest := 0
|
||||||
|
channels := GetSortedChannels(session, currentGuild)
|
||||||
|
|
||||||
|
for _, channel := range channels {
|
||||||
|
perms, err := session.State.UserChannelPermissions(session.State.User.ID, channel.ID)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
private := perms & discordgo.PermissionViewChannel == 0
|
||||||
|
|
||||||
|
privLen := 0
|
||||||
|
if private {
|
||||||
|
privLen = 1
|
||||||
|
}
|
||||||
|
length := utf8.RuneCountInString(channel.Name) + privLen
|
||||||
|
|
||||||
|
if length > longest {
|
||||||
|
longest = int(math.Min(25, float64(length)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print("\n\r")
|
||||||
|
fmt.Printf(" %*s topic\n\r", longest, "channel-name")
|
||||||
|
fmt.Print(strings.Repeat("-", 80) + "\n\r")
|
||||||
|
for _, channel := range channels {
|
||||||
|
perms, err := session.State.UserChannelPermissions(session.State.User.ID, channel.ID)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
private := perms & discordgo.PermissionViewChannel == 0
|
||||||
|
topic := strings.ReplaceAll(channel.Topic, "\n", " ")
|
||||||
|
name := channel.Name
|
||||||
|
if private {
|
||||||
|
name = "*" + name
|
||||||
|
}
|
||||||
|
|
||||||
|
nameLength := utf8.RuneCountInString(name)
|
||||||
|
if nameLength > 25 {
|
||||||
|
name = name[:24] + "\u2026"
|
||||||
|
}
|
||||||
|
|
||||||
|
topicLength := utf8.RuneCountInString(topic)
|
||||||
|
longestTopic := 80 - (longest + 5)
|
||||||
|
if topicLength > longestTopic {
|
||||||
|
topic = topic[:(longestTopic - 1)] + "\u2026"
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf(" %*s %s\n\r", longest, name, topic)
|
||||||
|
}
|
||||||
|
fmt.Print(strings.Repeat("-", 80) + "\n\r")
|
||||||
|
fmt.Print("\n\r")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListUsersCommand(session *discordgo.Session) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func SwitchGuild(session *discordgo.Session, input string) {
|
||||||
|
if input == "" {
|
||||||
|
ListChannelsCommand(session)
|
||||||
|
ListUsersCommand(session)
|
||||||
|
} else {
|
||||||
|
target := ""
|
||||||
|
|
||||||
|
for _, guild := range session.State.Guilds {
|
||||||
|
if strings.Index(strings.ToLower(guild.Name), strings.ToLower(input)) > -1 {
|
||||||
|
target = guild.ID
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if target == "" {
|
||||||
|
fmt.Print("<guild not found>\n\r")
|
||||||
|
} else {
|
||||||
|
state.SetCurrentGuild(target)
|
||||||
|
last := state.GetLastChannel(target)
|
||||||
|
if last == "" {
|
||||||
|
channels := GetSortedChannels(session, target)
|
||||||
|
topChannel := channels[0]
|
||||||
|
|
||||||
|
state.SetCurrentChannel(topChannel.ID)
|
||||||
|
state.SetLastChannel(target, topChannel.ID)
|
||||||
|
} else {
|
||||||
|
state.SetCurrentChannel(last)
|
||||||
|
}
|
||||||
|
|
||||||
|
ListChannelsCommand(session)
|
||||||
|
ListUsersCommand(session)
|
||||||
|
|
||||||
|
// TODO: update presence
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SwitchGuildsCommand(session *discordgo.Session) {
|
||||||
|
lib.MakePrompt(session, ":guild> ", false, func(session *discordgo.Session, input string, interrupt bool) {
|
||||||
|
fmt.Print("\r")
|
||||||
|
SwitchGuild(session, input)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -2,7 +2,10 @@ package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"unicode"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/Cynosphere/comcord/state"
|
"github.com/Cynosphere/comcord/state"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
|
@ -11,13 +14,52 @@ import (
|
||||||
|
|
||||||
const format string = " %s - %s%s"
|
const format string = " %s - %s%s"
|
||||||
|
|
||||||
|
func lessLower(sa, sb string) bool {
|
||||||
|
for {
|
||||||
|
rb, nb := utf8.DecodeRuneInString(sb)
|
||||||
|
if nb == 0 {
|
||||||
|
// The number of runes in sa is greater than or
|
||||||
|
// equal to the number of runes in sb. It follows
|
||||||
|
// that sa is not less than sb.
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
ra, na := utf8.DecodeRuneInString(sa)
|
||||||
|
if na == 0 {
|
||||||
|
// The number of runes in sa is less than the
|
||||||
|
// number of runes in sb. It follows that sa
|
||||||
|
// is less than sb.
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
rbl := unicode.ToLower(rb)
|
||||||
|
ral := unicode.ToLower(ra)
|
||||||
|
|
||||||
|
if ral != rbl {
|
||||||
|
return ral < rbl
|
||||||
|
} else {
|
||||||
|
return ra > rb
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func HelpCommand(session *discordgo.Session) {
|
func HelpCommand(session *discordgo.Session) {
|
||||||
noColor := state.HasNoColor()
|
noColor := state.HasNoColor()
|
||||||
|
|
||||||
fmt.Println("\r\nCOMcord (c)left 2023\n\r")
|
fmt.Println("\r\nCOMcord (c)left 2023\n\r")
|
||||||
|
|
||||||
|
commands := GetAllCommands()
|
||||||
|
keys := make([]string, 0, len(commands))
|
||||||
|
for key := range commands {
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
sort.Slice(keys, func(i, j int) bool {
|
||||||
|
return lessLower(keys[i], keys[j])
|
||||||
|
})
|
||||||
|
|
||||||
index := 0
|
index := 0
|
||||||
for key, cmd := range GetAllCommands() {
|
for _, key := range keys {
|
||||||
|
cmd := commands[key]
|
||||||
str := fmt.Sprintf(format, key, cmd.Description, "")
|
str := fmt.Sprintf(format, key, cmd.Description, "")
|
||||||
length := len(str)
|
length := len(str)
|
||||||
padding := strings.Repeat(" ", 25 - length)
|
padding := strings.Repeat(" ", 25 - length)
|
||||||
|
|
|
@ -36,6 +36,16 @@ func Setup() {
|
||||||
Run: ListGuildsCommand,
|
Run: ListGuildsCommand,
|
||||||
Description: "list guilds",
|
Description: "list guilds",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
commandMap["l"] = Command{
|
||||||
|
Run: ListChannelsCommand,
|
||||||
|
Description: "list channels",
|
||||||
|
}
|
||||||
|
|
||||||
|
commandMap["G"] = Command{
|
||||||
|
Run: SwitchGuildsCommand,
|
||||||
|
Description: "goto guild",
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCommand(key string) (Command, bool) {
|
func GetCommand(key string) (Command, bool) {
|
||||||
|
|
|
@ -30,6 +30,10 @@ func MessageCreate(session *discordgo.Session, msg *discordgo.MessageCreate) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func MessageUpdate(session *discordgo.Session, msg *discordgo.MessageUpdate) {
|
func MessageUpdate(session *discordgo.Session, msg *discordgo.MessageUpdate) {
|
||||||
|
if msg.Author == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if msg.Author.ID == session.State.User.ID {
|
if msg.Author.ID == session.State.User.ID {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,12 +20,13 @@ func Ready(session *discordgo.Session, event *discordgo.Ready) {
|
||||||
defaultGuild := state.GetConfigValue("defaultGuild")
|
defaultGuild := state.GetConfigValue("defaultGuild")
|
||||||
defaultChannel := state.GetConfigValue("defaultChannel")
|
defaultChannel := state.GetConfigValue("defaultChannel")
|
||||||
if defaultGuild != "" {
|
if defaultGuild != "" {
|
||||||
_, err := session.State.Guild(defaultGuild)
|
guild, err := session.State.Guild(defaultGuild)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if defaultChannel != "" {
|
if defaultChannel != "" {
|
||||||
state.SetCurrentChannel(defaultChannel)
|
state.SetCurrentChannel(defaultChannel)
|
||||||
state.SetLastChannel(defaultGuild, defaultChannel)
|
state.SetLastChannel(defaultGuild, defaultChannel)
|
||||||
}
|
}
|
||||||
|
commands.SwitchGuild(session, guild.Name)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("\r% This account is not in the defined default guild.")
|
fmt.Println("\r% This account is not in the defined default guild.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
"github.com/mgutz/ansi"
|
"github.com/mgutz/ansi"
|
||||||
)
|
)
|
||||||
|
|
||||||
var REGEX_CODEBLOCK = regexp.MustCompile(`(?i)\x60\x60\x60(?:([a-z0-9_+\-\.]+?)\n)?\n*([^\n].*?)\n*\x60\x60\x60`)
|
var REGEX_CODEBLOCK = regexp.MustCompile(`(?i)\x60\x60\x60(?:([a-z0-9_+\-\.]+?)\n)?\n*([^\n](?:.|\n)*?)\n*\x60\x60\x60`)
|
||||||
var REGEX_EMOTE = regexp.MustCompile(`<(?:\x{200b}|&)?a?:(\w+):(\d+)>`)
|
var REGEX_EMOTE = regexp.MustCompile(`<(?:\x{200b}|&)?a?:(\w+):(\d+)>`)
|
||||||
|
|
||||||
type MessageOptions struct {
|
type MessageOptions struct {
|
||||||
|
@ -288,14 +288,16 @@ func ProcessMessage(session *discordgo.Session, msg *discordgo.Message, options
|
||||||
isDump := REGEX_CODEBLOCK.MatchString(content)
|
isDump := REGEX_CODEBLOCK.MatchString(content)
|
||||||
|
|
||||||
if strings.Index(content, "\n") > -1 && !isDump {
|
if strings.Index(content, "\n") > -1 && !isDump {
|
||||||
for _, line := range strings.Split(content, "\n") {
|
for i, line := range strings.Split(content, "\n") {
|
||||||
options.Content = line
|
options.Content = line
|
||||||
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.Attachments = msg.Attachments
|
options.Attachments = msg.Attachments
|
||||||
options.Stickers = msg.StickerItems
|
options.Stickers = msg.StickerItems
|
||||||
|
if i == 0 {
|
||||||
options.Reply = msg.ReferencedMessage
|
options.Reply = msg.ReferencedMessage
|
||||||
|
}
|
||||||
options.IsMention = isPing
|
options.IsMention = isPing
|
||||||
options.IsDM = isDM
|
options.IsDM = isDM
|
||||||
options.IsJoin = msg.Type == discordgo.MessageTypeGuildMemberJoin
|
options.IsJoin = msg.Type == discordgo.MessageTypeGuildMemberJoin
|
||||||
|
|
Loading…
Reference in a new issue