From 77622b77a0351f55f8b1a6788aca1fe727a40ecc Mon Sep 17 00:00:00 2001 From: Sean Hinchee Date: Sun, 16 Dec 2018 22:34:16 -0600 Subject: [PATCH] add better notification handling ;; add :c ? ;; add :c n ;; all thanks to halfwit --- TODO | 4 +++ commands.go | 77 ++++++++++++++++++++++++++++++++++++++++------------- events.go | 33 ++++++++++++++--------- helper.go | 20 ++++++++++---- main.go | 23 +++++++++++++--- menu.go | 1 - 6 files changed, 117 insertions(+), 41 deletions(-) diff --git a/TODO b/TODO index 67c50924b9a27020aac1f751005acc931bc954b4..eaebf5998ab38d7ba755deb7907668de49c78d14 100644 --- a/TODO +++ b/TODO @@ -25,9 +25,13 @@ Features: * CAPTCHA solution -- http-proxy is a bad one * [done-ish] fix pm's + * when opening a pm, drops back to channel dialogue * aux/statusmsg backgrounds and doesn't steal input * fix :m not displaying more than loaded backlog * fix ctrl+d not closing out cleanly + +* add [b] go back to pm's menu + diff --git a/commands.go b/commands.go index f7c61fd8fa06809550e2b83c19a40ec9d426cd85..d79b1183c9a2e76f22081f84f7a7a6fad1d3f666 100644 --- a/commands.go +++ b/commands.go @@ -3,27 +3,68 @@ package main import ( "strconv" "strings" + "github.com/bwmarrin/discordgo" ) //ParseForCommands parses input for Commands, returns message if no command specified, else return is empty func ParseForCommands(line string) string { - //One Key Commands - switch line { + if len(line) < 2 { + return line + } + switch line[:2] { + case ":?": + // Show help menu + Msg(TextMsg, "Commands: ") + Msg(TextMsg, "[:g] - Select guild") + Msg(TextMsg, "[:p] - Select private message") + Msg(TextMsg, "[:c] - Select guild channel") + Msg(TextMsg, "[:c ?] - List guild channels") + Msg(TextMsg, "[:c ] - Go directly to channel") + Msg(TextMsg, "[:m ] - Display last messages") + Msg(TextMsg, "[:u ] - Change username") case ":g": SelectGuild() - line = "" - case ":c": - SelectChannel() - line = "" + return "" case ":p": SelectPrivate() - line = "" - default: - // Nothing - } - - //Argument Commands - if strings.HasPrefix(line, ":m") { + return "" + case ":c": + opts := strings.Split(line, " ") + if len(opts) == 1 { + SelectChannel() + return "" + } + selectID := 0 + if opts[1] == "?" { + for _, channel := range State.Channels { + if channel.Type == 0 { + Msg(TextMsg, "[%d] %s\n", selectID, channel.Name) + selectID++ + } + } + return "" + } + selectMap := make(map[int]*discordgo.Channel) + for _, channel := range State.Channels { + if channel.Type == 0 { + selectMap[selectID] = channel + selectID++ + } + } + selection, err := strconv.Atoi(opts[1]) + if err != nil { + Msg(ErrorMsg, "[:c] Argument Error: %s\n", err) + return "" + } + if len(State.Channels) < selection || selection < 0 { + Msg(ErrorMsg, "[:c] Argument Error: Out of bounds\n") + return "" + } + channel := selectMap[selection] + State.SetChannel(channel.ID) + ShowContent() + return "" + case ":m": AmountStr := strings.Split(line, " ") if len(AmountStr) < 2 { Msg(ErrorMsg, "[:m] No Arguments \n") @@ -39,9 +80,8 @@ func ParseForCommands(line string) string { Msg(InfoMsg, "Printing last %d messages!\n", Amount) State.RetrieveMessages(Amount) PrintMessages(Amount) - line = "" - } - if strings.HasPrefix(line, ":u") { + return line + case ":u": session := State.Session user := session.User newName := strings.TrimPrefix(line, ":u ") @@ -49,9 +89,8 @@ func ParseForCommands(line string) string { if err != nil { Msg(ErrorMsg, "[:u] Argument Error: %s\n", err) } - line = "" - } - + return line + } return line } diff --git a/events.go b/events.go index 0f075457eb13036a6c24ba7de4b505b070c614ff..690265dbacd8cd1aa1b86c7824ea8a261615aef5 100644 --- a/events.go +++ b/events.go @@ -16,19 +16,9 @@ func newReaction(s *discordgo.Session, m *discordgo.MessageReactionAdd) { // This function will be called (due to AddHandler above) every time a new // message is created on any channel that the autenticated user has access to. func newMessage(s *discordgo.Session, m *discordgo.MessageCreate) { - //Global Mentions - Mention := "@" + State.Session.User.Username - if strings.Contains(m.ContentWithMentionsReplaced(), Mention) { - go Notify(m.Message) - } - // Do nothing when State is disabled - if !State.Enabled { - return - } - - //State Messages - if m.ChannelID == State.Channel.ID { + //State Messages -- don't notify when we're in the channel + if m.ChannelID == State.Channel.ID && State.Enabled { State.AddMessage(m.Message) Messages := ReceivingMessageParser(m.Message) @@ -37,5 +27,24 @@ func newMessage(s *discordgo.Session, m *discordgo.MessageCreate) { MessagePrint(string(m.Timestamp), m.Author.Username, Msg) //log.Printf("> %s > %s\n", UserName(m.Author.Username), Msg) } + return + } + + //Global Mentions + Mention := "@" + State.Session.User.Username + if strings.Contains(m.ContentWithMentionsReplaced(), Mention) { + go Notify(m.Message) + return + } + DMs, err := Session.DiscordGo.UserChannels() + if err != nil { + // No DMs + return + } + for _, channel := range DMs { + if m.ChannelID == channel.ID { + go Notify(m.Message) + return + } } } diff --git a/helper.go b/helper.go index 400f37d98783dc94f3f3d36a705b230da091009b..de543d7ea2969de9bcd33304f96930a924e0f603 100644 --- a/helper.go +++ b/helper.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/bwmarrin/discordgo" "io" + "io/ioutil" "log" "os" "os/exec" @@ -60,11 +61,16 @@ func ReceivingMessageParser(m *discordgo.Message) []string { //PrintMessages prints amount of Messages to CLI func PrintMessages(Amount int) { for Key, m := range State.Messages { + name := m.Author.Username + if member, ok := State.Members[m.Author.Username]; ok { + if member.Nick != "" { + name = member.Nick + } + } if Key >= len(State.Messages)-Amount { Messages := ReceivingMessageParser(m) for _, Msg := range Messages { - //log.Printf("> %s > %s\n", UserName(m.Author.Username), Msg) - MessagePrint(string(m.Timestamp), m.Author.Username, Msg) + MessagePrint(string(m.Timestamp), name, Msg) } } @@ -77,7 +83,11 @@ func Notify(m *discordgo.Message) { return } var Title string - switch State.Channel.Type { + channel, err := State.Session.DiscordGo.Channel(m.ChannelID) + if err != nil { + Msg(ErrorMsg, "(NOT) PM Error: %s\n", err) + } + switch channel.Type { case discordgo.ChannelTypeGuildText: Channel, err := State.Session.DiscordGo.Channel(m.ChannelID) if err != nil { @@ -94,7 +104,7 @@ func Notify(m *discordgo.Message) { switch runtime.GOOS { case "plan9": pr, pw := io.Pipe() - cmd := exec.Command("/bin/aux/statusmsg", "-k", *notifyFlag, Title) + cmd := exec.Command("/bin/aux/statusmsg", *notifyFlag, Title) cmd.Stdin = pr go func() { defer pw.Close() @@ -105,7 +115,7 @@ func Notify(m *discordgo.Message) { if err != nil { Msg(ErrorMsg, "%s\n", err) } - + ioutil.WriteFile("/dev/wctl", []byte("current"), 0644) default: cmd := exec.Command("notify-send", Title, m.ContentWithMentionsReplaced()) err := cmd.Start() diff --git a/main.go b/main.go index a0df9822fbc2ff4bf9ad408c79d3615b78bf0685..1d63c72546331d1370bb5e628c3eef039e2b344a 100644 --- a/main.go +++ b/main.go @@ -78,6 +78,23 @@ func main() { //fmt.Print("> ") //line, _ := rl.Readline() line, _ := reader.ReadString('\n') + + // ``` + if strings.HasPrefix(line, "```") { + for { + subline, _ := reader.ReadString('\n') + line += subline + if strings.Index(subline, "```") != -1 { + break + } + } + _, err := State.Session.DiscordGo.ChannelMessageSend(State.Channel.ID, line) + if err != nil { + fmt.Printf("Error: %s\n", err) + } + continue + } + line = line[:len(line)-1] //QUIT @@ -141,9 +158,8 @@ func ParseForMentions(line string) string { //ReplaceMentions replaces mentions to ID func ReplaceMentions(input string) string { // Check for guild members that match - channel := State.Guild.Members - for _, member := range channel { - if member.Nick == input[1:] { + for _, member := range State.Guild.Members { + if strings.HasPrefix(member.Nick, input[1:]) { return member.User.Mention() } if strings.HasPrefix(member.User.Username, input[1:]) { @@ -158,7 +174,6 @@ func ReplaceMentions(input string) string { for _, channel := range userChannels { for _, recipient := range channel.Recipients { if strings.HasPrefix(input[1:], recipient.Username) { - fmt.Println("usermatch") return recipient.Mention() } } diff --git a/menu.go b/menu.go index 6914e0546ae0d85a97110156f75e655c9f4ef7c6..603bfccb7b146c8c0bb34c2836afd41ca5e5fa81 100644 --- a/menu.go +++ b/menu.go @@ -87,7 +87,6 @@ Start: } State.Channel = UserChannels[ResponseInteger] - ShowContent() End: }