From 0c4f556201587332e61e61364e87b07b8b80d676 Mon Sep 17 00:00:00 2001 From: Sean Hinchee Date: Wed, 17 Jul 2019 23:47:50 -0700 Subject: [PATCH] add :! command to print server statistics ;; update :? menu ;; add some comments ;; run go (vet fmt) --- DiscordState/state.go | 1 + README.md | 1 + TODO | 5 +++++ commands.go | 21 +++++++++++++++++++++ helper.go | 29 ++++++++++++++++++++++------- main.go | 2 +- 6 files changed, 51 insertions(+), 8 deletions(-) diff --git a/DiscordState/state.go b/DiscordState/state.go index e571727fa30105d7d679dc8c37f4620d0f0067b0..697fd05d9743ddba11780cc3c0e631dbda39a6f5 100644 --- a/DiscordState/state.go +++ b/DiscordState/state.go @@ -56,6 +56,7 @@ func (State *State) DelMessage(Message *discordgo.Message) { //RetrieveMessages retrieves last N Messages and puts it in state func (State *State) RetrieveMessages(Amount int) error { + // See: https://godoc.org/github.com/bwmarrin/discordgo#Channel and https://godoc.org/github.com/bwmarrin/discordgo#State for max message count Messages, err := State.Session.DiscordGo.ChannelMessages(State.Channel.ID, Amount, "", "", "") if err != nil { return err diff --git a/README.md b/README.md index 0faf90703786f6bdd062d571c0fc2cbc3e46e9e6..d4f4d1ffbd0443e8999cdb4ea150145faef2d42f 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Commands available in chat: | :m [n] | Display last [n] messages: ex. `:m 2` displays last two messages | | :p | Pulls up the private channel menu | | :n name | Change nickname to `name` | +| :! | Print current server information | | :? | List the available commands | You can regex the last message sent using a format such as: diff --git a/TODO b/TODO index 6dec3966547613c8b819dd76b1c14a8df33e5c43..e0b9c5dd825698f6ccc07845775f370c28bd56a5 100644 --- a/TODO +++ b/TODO @@ -30,3 +30,8 @@ Features: Bugs: * (maybe a Go bug) ­ if you do: `drawterm -G … -c disco` then ctrl+c the terminal (from Linux or so), load maxes and memory allocation spikes on the plan9 server + +* The following error at boot: + + Connecting...2019/07/17 23:39:53 [DG0] wsapi.go:552:onEvent() error unmarshalling READY event, json: cannot unmarshal number into Go struct field ReadState.last_message_id of type string + PASSED! diff --git a/commands.go b/commands.go index 9095591205312dd3566add0d994a2f0364925e93..5f93cadcadcdac69ee5bb8f6909bfbdb70f37211 100644 --- a/commands.go +++ b/commands.go @@ -15,6 +15,7 @@ func ParseForCommands(line string) string { switch line[:2] { case "s/": + // `s/X/Y/` replace X with Y in the last message sent r := regexp.MustCompile(`s/([^/]+)/([^/]*)/$`) n := r.FindStringSubmatchIndex(line) if len(n) < 1 { @@ -45,9 +46,11 @@ func ParseForCommands(line string) string { } Msg(TextMsg, "%s → %s\n", m.Content, rep) return "" + case ":?": // Show help menu Msg(TextMsg, "Commands: \n") + Msg(TextMsg, "s/X/Y/ - Replace X with Y in the last message\n") Msg(TextMsg, "[:g] - Open guild menu\n") Msg(TextMsg, "[:p] - Open private message menu\n") Msg(TextMsg, "[:c] - Open guild channel menu\n") @@ -55,14 +58,22 @@ func ParseForCommands(line string) string { Msg(TextMsg, "[:c ] - Go directly to channel \n") Msg(TextMsg, "[:m ] - Display last messages\n") Msg(TextMsg, "[:n ] - Change username to \n") + Msg(TextMsg, "[:!] - Print current server information\n") + Msg(TextMsg, "[:?] - Print this menu\n") return "" + case ":g": + // Choose a server (guild) SelectGuild() return "" + case ":p": + // Enter DM menu SelectPrivate() return "" + case ":c": + // Choose a channel within the server opts := strings.Split(line, " ") if len(opts) == 1 { SelectChannel() @@ -98,7 +109,9 @@ func ParseForCommands(line string) string { State.SetChannel(channel.ID) ShowContent() return "" + case ":m": + // Load message backlog -- TODO ­ currently does not retrieve more than n messages AmountStr := strings.Split(line, " ") if len(AmountStr) < 2 { Msg(ErrorMsg, "[:m] No Arguments \n") @@ -115,7 +128,9 @@ func ParseForCommands(line string) string { State.RetrieveMessages(Amount) PrintMessages(Amount) return "" + case ":n": + // Change nickname session := State.Session user := session.User oldName := user.Username @@ -127,7 +142,13 @@ func ParseForCommands(line string) string { } Msg(TextMsg, "%s → %s\n", oldName, newName) return "" + + case ":!": + // Print current server information + Msg(TextMsg, GuildInfo(State.Guild)) + return "" } + return line } diff --git a/helper.go b/helper.go index 42f313096425305f1be3e5bbb6fad4edc4bb11ba..fb4db1f86d4bd12df9f8f65d374676eac0009821 100644 --- a/helper.go +++ b/helper.go @@ -132,7 +132,7 @@ func MessagePrint(Time, Username, Content string) { content := ParseForEmoji(Content) //var Color color.Attribute log.SetFlags(0) - if ! *timeStamp { + if !*timeStamp { log.Printf("%s %s %s\n", Username, Config.PromptChar, content) } else { TimeStamp, _ := time.Parse(time.RFC3339, Time) @@ -147,6 +147,7 @@ func dis(a, b int) float64 { return float64((a - b) * (a - b)) } +// Turn raw mode on -- rio in Plan9 only func Rawon() (*os.File, error) { consctl, err := os.OpenFile("/dev/consctl", os.O_WRONLY, 0200) if err != nil { @@ -165,13 +166,8 @@ func Rawon() (*os.File, error) { return consctl, nil } +// Turn raw mode off -- rio in Plan9 only func RawOff(consctl *os.File) error { - //consctl, err := os.OpenFile("/dev/consctl", os.O_WRONLY, 0200) - //if err != nil { - // /* not on Plan 9 */ - // return err - //} - rawoff := []byte("rawoff") _, err := consctl.Write(rawoff) if err != nil { @@ -183,6 +179,7 @@ func RawOff(consctl *os.File) error { return nil } +// Return the text currently in /dev/cons -- Plan9 only func GetCons() string { cons, err := os.OpenFile("/dev/cons", os.O_RDWR, 0600) if err != nil { @@ -192,3 +189,21 @@ func GetCons() string { consScan.Scan() return consScan.Text() } + +// Nicely stringifies the useful info about a guild -- ends in \n +func GuildInfo(g *discordgo.Guild) (s string) { + s += "\n" + s += fmt.Sprintf("Name:\t\t%s\n", g.Name) + s += fmt.Sprintf("ID:\t\t\t%s\n", g.ID) + s += fmt.Sprintf("Icon:\t\t%s\n", g.Icon) + s += fmt.Sprintf("Region:\t\t%s\n", g.Region) + s += fmt.Sprintf("Owner ID:\t%s\n", g.OwnerID) + s += fmt.Sprintf("Join time:\t%s\n", g.JoinedAt) + s += fmt.Sprintf("# Members:\t%d\n", g.MemberCount) + s += fmt.Sprintf("# Channels:\t%d\n", len(g.Channels)) + s += fmt.Sprintf("# Roles:\t%d\n", len(g.Roles)) + s += fmt.Sprintf("# Emojis:\t%d\n", len(g.Emojis)) + s += "\n" + + return +} diff --git a/main.go b/main.go index 47a5b4fbd43cd99759e346d6d0aeb1311becbc76..e1d9228bc543a4083de93e11a554853e84122a39 100644 --- a/main.go +++ b/main.go @@ -68,7 +68,7 @@ func main() { // Attach New Window InitWindow() - // Attach Even Handlers + // Attach Event Handlers State.Session.DiscordGo.AddHandler(newMessage) //State.Session.DiscordGo.AddHandler(newReaction)