diff --git a/main.go b/main.go index a62e296..18f5247 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "samhofi.us/x/keybase" ) +var typeCommands = make(map[string]TypeCommand) var commands = make(map[string]Command) var baseCommands = make([]string, 0) @@ -279,6 +280,14 @@ func cleanChannelName(c string) string { } func handleMessage(api keybase.ChatAPI) { + if _, ok := typeCommands[api.Msg.Content.Type]; ok { + if api.Msg.Channel.MembersType == channel.MembersType && cleanChannelName(api.Msg.Channel.Name) == channel.Name { + if channel.MembersType == keybase.TEAM && channel.TopicName != api.Msg.Channel.TopicName { + } else { + go typeCommands[api.Msg.Content.Type].Exec(api) + } + } + } if api.Msg.Content.Type == "text" || api.Msg.Content.Type == "attachment" { go populateList() msgBody := api.Msg.Content.Text.Body @@ -366,6 +375,22 @@ func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } +// RegisterTypeCommand registers a command to be used within the client +func RegisterTypeCommand(c TypeCommand) error { + var notAdded string + for _, cmd := range c.Cmd { + if _, ok := typeCommands[cmd]; !ok { + typeCommands[cmd] = c + continue + } + notAdded = fmt.Sprintf("%s, %s", notAdded, cmd) + } + if notAdded != "" { + return fmt.Errorf("The following aliases were not added because they already exist: %s", notAdded) + } + return nil +} + // RegisterCommand registers a command to be used within the client func RegisterCommand(c Command) error { var notAdded string diff --git a/tcmdAutoReact.go b/tcmdAutoReact.go new file mode 100644 index 0000000..37b4b1a --- /dev/null +++ b/tcmdAutoReact.go @@ -0,0 +1,24 @@ +// +build type_commands autoreactcmd + +package main + +import ( + "samhofi.us/x/keybase" +) + +func init() { + command := TypeCommand{ + Cmd: []string{"text"}, + Description: "Automatically reacts to every incoming message with an emoji", + Exec: tcmdAutoReact, + } + + RegisterTypeCommand(command) +} + +func tcmdAutoReact(m keybase.ChatAPI) { + msgID := m.Msg.ID + channel := m.Msg.Channel + chat := k.NewChat(channel) + chat.React(msgID, ":sunglasses:") +} diff --git a/types.go b/types.go index d385b47..dedae0b 100644 --- a/types.go +++ b/types.go @@ -1,5 +1,7 @@ package main +import "samhofi.us/x/keybase" + // Command outlines a command type Command struct { Cmd []string // Any aliases that trigger this command @@ -7,3 +9,10 @@ type Command struct { Help string // The full help text explaining how to use the command Exec func([]string) // A function that takes the command (arg[0]) and any arguments (arg[1:]) as input } + +// TypeCommand outlines a command that reacts on message type +type TypeCommand struct { + Cmd []string // Message types that trigger this command + Description string // A short description of the command + Exec func(keybase.ChatAPI) // A function that takes the command (arg[0]) and any arguments (arg[1:]) as input +}