mirror of
https://github.com/Rudi9719/kbtui.git
synced 2026-03-22 08:47:24 +00:00
First pass at type commands
This commit is contained in:
25
main.go
25
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
|
||||
|
||||
24
tcmdAutoReact.go
Normal file
24
tcmdAutoReact.go
Normal 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:")
|
||||
}
|
||||
9
types.go
9
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user