Browse Source

First pass at type commands

pull/1/head
Sam 5 years ago
parent
commit
0eba7f9abb
  1. 25
      main.go
  2. 24
      tcmdAutoReact.go
  3. 9
      types.go

25
main.go

@ -11,6 +11,7 @@ import ( @@ -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 { @@ -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 { @@ -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

24
tcmdAutoReact.go

@ -0,0 +1,24 @@ @@ -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:")
}

9
types.go

@ -1,5 +1,7 @@ @@ -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 { @@ -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
}

Loading…
Cancel
Save