~kris/9p

disco

77622b77a0351f55f8b1a6788aca1fe727a40ecc — Sean Hinchee 7 years ago 912b421
add better notification handling ;; add :c ? ;; add :c n ;; all thanks to halfwit
6 files changed, 117 insertions(+), 41 deletions(-)

M TODO
M commands.go
M events.go
M helper.go
M main.go
M menu.go
M TODO => TODO +4 -0
@@ 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


M commands.go => commands.go +58 -19
@@ 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 <num>] - Go directly to channel")
		Msg(TextMsg, "[:m <num>] - Display last <num> messages")
		Msg(TextMsg, "[:u <name>] - 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
}


M events.go => events.go +21 -12
@@ 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
		}
	}
}

M helper.go => helper.go +15 -5
@@ 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()

M main.go => main.go +19 -4
@@ 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()
			}
		}

M menu.go => menu.go +0 -1
@@ 87,7 87,6 @@ Start:
	}

	State.Channel = UserChannels[ResponseInteger]
	ShowContent()
End:
}