1
0
mirror of https://github.com/Rudi9719/kbtui.git synced 2026-03-22 11:07:22 +00:00

First pass at type commands

This commit is contained in:
Sam
2019-10-04 14:08:13 -04:00
parent d95c87db88
commit 0eba7f9abb
3 changed files with 58 additions and 0 deletions

25
main.go
View File

@ -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

24
tcmdAutoReact.go Normal file
View File

@ -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:")
}

View File

@ -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
}