From 1593736c70e912969ead17a74b90de5e68217ddd Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Mon, 10 Jul 2023 21:41:51 -0600 Subject: [PATCH 1/3] prompt util func to reduce code reuse --- README.md | 4 ++-- commands/send.go | 43 ++++++++++++++++--------------------------- lib/prompt.go | 33 +++++++++++++++++++++++++++++++++ state/main.go | 10 ++++++++++ 4 files changed, 61 insertions(+), 29 deletions(-) create mode 100644 lib/prompt.go diff --git a/README.md b/README.md index 07d8591..f145b1a 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Go is more portable than Node.js ## TODO - [x] Send mode -- [ ] Commands +- [x] Commands - [x] Quit (q) - [ ] Switch guilds (G) - [ ] Switch channels (g) @@ -29,7 +29,7 @@ Go is more portable than Node.js - [ ] Cross-guild peek (P) - [ ] List channels (l) - [ ] List guilds (L) - - [ ] Clear (c) + - [x] Clear (c) - [ ] Surf channels forwards (>) - [ ] Surf channels backwards (<) - [ ] AFK toggle (A) diff --git a/commands/send.go b/commands/send.go index fba8a53..205a58c 100644 --- a/commands/send.go +++ b/commands/send.go @@ -8,7 +8,6 @@ import ( "github.com/Cynosphere/comcord/lib" "github.com/Cynosphere/comcord/state" "github.com/bwmarrin/discordgo" - "github.com/ergochat/readline" "github.com/mgutz/ansi" ) @@ -16,7 +15,7 @@ func SendMode(session *discordgo.Session) { channelId := state.GetCurrentChannel() if channelId == "" { fmt.Print("\n\r") - return + return } state.SetInPrompt(true) @@ -29,32 +28,22 @@ func SendMode(session *discordgo.Session) { prompt = ansi.Color(prompt, "cyan+b") } - input, _ := readline.NewFromConfig(&readline.Config{ - Prompt: prompt, - UniqueEditLine: true, - }) - defer input.Close() - - out, err := input.Readline() - out = strings.TrimSpace(out) - input.Close() - - if out == "" { - if err == readline.ErrInterrupt { - fmt.Print("^C\n\r") + lib.MakePrompt(session, prompt, true, func(session *discordgo.Session, input string, interrupt bool) { + if input == "" { + if interrupt { + fmt.Print("^C\n\r") + } else { + fmt.Print(prompt, "\n\r") + } } else { - fmt.Print(prompt, "\n\r") - } - } else { - fmt.Print(prompt, out, "\n\r") - _, err := session.ChannelMessageSend(channelId, out) + fmt.Print(prompt, input, "\n\r") + _, err := session.ChannelMessageSend(channelId, input) - if err != nil { - fmt.Print("\n\r") - } + if err != nil { + fmt.Print("\n\r") + } - // TODO: update afk state - } - state.SetInPrompt(false) - lib.ProcessQueue(session) + // TODO: update afk state + } + }) } diff --git a/lib/prompt.go b/lib/prompt.go new file mode 100644 index 0000000..a85709f --- /dev/null +++ b/lib/prompt.go @@ -0,0 +1,33 @@ +package lib + +import ( + "strings" + + "github.com/Cynosphere/comcord/state" + "github.com/bwmarrin/discordgo" + "github.com/ergochat/readline" +) + +func MakePrompt(session *discordgo.Session, prompt string, uniqueLine bool, callback func(session *discordgo.Session, input string, interrupt bool)) { + state.SetInPrompt(true) + state.SetPromptText(prompt) + + rl, _ := readline.NewFromConfig(&readline.Config{ + Prompt: prompt, + UniqueEditLine: uniqueLine, + }) + defer rl.Close() + + input, err := rl.Readline() + input = strings.TrimSpace(input) + rl.Close() + + interrupt := err == readline.ErrInterrupt + + callback(session, input, interrupt) + + state.SetInPrompt(false) + state.SetPromptText("") + + ProcessQueue(session) +} diff --git a/state/main.go b/state/main.go index 4499601..cd63ec8 100644 --- a/state/main.go +++ b/state/main.go @@ -15,6 +15,7 @@ type ComcordState struct { CurrentChannel string NameLength int InPrompt bool + PromptText string AFK bool MessageQueue []*discordgo.Message LastChannel map[string]string @@ -34,6 +35,7 @@ func Setup(config map[string]string) { state.CurrentChannel = "" state.NameLength = 2 state.InPrompt = false + state.PromptText = "" state.AFK = false state.MessageQueue = make([]*discordgo.Message, 0) state.LastChannel = make(map[string]string) @@ -93,6 +95,14 @@ func SetInPrompt(value bool) { state.InPrompt = value } +func GetPromptText() string { + return state.PromptText +} + +func SetPromptText(value string) { + state.PromptText = value +} + func IsAFK() bool { return state.AFK } From 4aa208ee6949a3433d7ef3e7d4b890108da46daf Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Mon, 10 Jul 2023 21:46:47 -0600 Subject: [PATCH 2/3] emote command --- commands/emote.go | 37 +++++++++++++++++++++++++++++++++++++ commands/main.go | 5 +++++ commands/send.go | 4 +--- 3 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 commands/emote.go diff --git a/commands/emote.go b/commands/emote.go new file mode 100644 index 0000000..5c151c2 --- /dev/null +++ b/commands/emote.go @@ -0,0 +1,37 @@ +package commands + +import ( + "fmt" + + "github.com/Cynosphere/comcord/lib" + "github.com/Cynosphere/comcord/state" + "github.com/bwmarrin/discordgo" +) + +func EmoteCommand(session *discordgo.Session) { + channelId := state.GetCurrentChannel() + if channelId == "" { + fmt.Print("\n\r") + return + } + + prompt := ":emote> " + lib.MakePrompt(session, prompt, true, func(session *discordgo.Session, input string, interrupt bool) { + if input == "" { + if interrupt { + fmt.Print("^C\n\r") + } else { + fmt.Print(prompt, "\n\r") + } + } else { + fmt.Print(prompt, input, "\n\r") + _, err := session.ChannelMessageSend(channelId, "*" + input + "*") + + if err != nil { + fmt.Print("\n\r") + } + + // TODO: update afk state + } + }) +} diff --git a/commands/main.go b/commands/main.go index 916ff01..ae9510f 100644 --- a/commands/main.go +++ b/commands/main.go @@ -26,6 +26,11 @@ func Setup() { Run: ClearCommand, Description: "clear", } + + commandMap["e"] = Command{ + Run: EmoteCommand, + Description: "emote", + } } func GetCommand(key string) (Command, bool) { diff --git a/commands/send.go b/commands/send.go index 205a58c..fedc660 100644 --- a/commands/send.go +++ b/commands/send.go @@ -15,11 +15,9 @@ func SendMode(session *discordgo.Session) { channelId := state.GetCurrentChannel() if channelId == "" { fmt.Print("\n\r") - return + return } - state.SetInPrompt(true) - length := utf8.RuneCountInString(session.State.User.Username) + 2 curLength := state.GetNameLength() From 80a343e4ee06ca9f061a5bc5e3aabd06dfe69f04 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Mon, 10 Jul 2023 22:35:15 -0600 Subject: [PATCH 3/3] list guilds --- README.md | 4 ++-- commands/guild.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++ commands/main.go | 5 +++++ events/ready.go | 3 +++ 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 commands/guild.go diff --git a/README.md b/README.md index f145b1a..03e2353 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Go is more portable than Node.js - [ ] Switch guilds (G) - [ ] Switch channels (g) - [ ] List online users in guild (w) - - [ ] Emote (e) + - [x] Emote (e) - Just sends message surrounded in `*`'s - [ ] Finger (f) - Shows presence data if available @@ -28,7 +28,7 @@ Go is more portable than Node.js - [ ] Peek (p) - [ ] Cross-guild peek (P) - [ ] List channels (l) - - [ ] List guilds (L) + - [x] List guilds (L) - [x] Clear (c) - [ ] Surf channels forwards (>) - [ ] Surf channels backwards (<) diff --git a/commands/guild.go b/commands/guild.go new file mode 100644 index 0000000..1ec1d17 --- /dev/null +++ b/commands/guild.go @@ -0,0 +1,52 @@ +package commands + +import ( + "fmt" + "strings" + "unicode/utf8" + + "github.com/bwmarrin/discordgo" +) + +type GuildListing struct { + Name string + Members int + Online int +} + +func ListGuildsCommand(session *discordgo.Session) { + longest := 0 + guilds := make([]GuildListing, 0) + + for _, guild := range session.State.Guilds { + length := utf8.RuneCountInString(guild.Name) + if length > longest { + longest = length + } + + guildWithCounts, err := session.GuildWithCounts(guild.ID) + if err != nil { + guilds = append(guilds, GuildListing{ + Name: guild.Name, + Members: guild.MemberCount, + Online: 0, + }) + return + } + + guilds = append(guilds, GuildListing{ + Name: guild.Name, + Members: guildWithCounts.ApproximateMemberCount, + Online: guildWithCounts.ApproximatePresenceCount, + }) + } + + fmt.Print("\n\r") + fmt.Printf(" %*s online total\n\r", longest, "guild-name") + fmt.Print(strings.Repeat("-", 80) + "\n\r") + for _, guild := range guilds { + fmt.Printf(" %*s %6d %5d\n\r", longest, guild.Name, guild.Online, guild.Members) + } + fmt.Print(strings.Repeat("-", 80) + "\n\r") + fmt.Print("\n\r") +} diff --git a/commands/main.go b/commands/main.go index ae9510f..bb7e524 100644 --- a/commands/main.go +++ b/commands/main.go @@ -31,6 +31,11 @@ func Setup() { Run: EmoteCommand, Description: "emote", } + + commandMap["L"] = Command{ + Run: ListGuildsCommand, + Description: "list guilds", + } } func GetCommand(key string) (Command, bool) { diff --git a/events/ready.go b/events/ready.go index 911c8d9..42380bb 100644 --- a/events/ready.go +++ b/events/ready.go @@ -4,6 +4,7 @@ import ( "fmt" "unicode/utf8" + "github.com/Cynosphere/comcord/commands" "github.com/Cynosphere/comcord/state" "github.com/bwmarrin/discordgo" "github.com/mgutz/ansi" @@ -14,6 +15,8 @@ func Ready(session *discordgo.Session, event *discordgo.Ready) { state.SetNameLength(utf8.RuneCountInString(session.State.User.Username) + 2) + commands.ListGuildsCommand(session) + defaultGuild := state.GetConfigValue("defaultGuild") defaultChannel := state.GetConfigValue("defaultChannel") if defaultGuild != "" {