-
Notifications
You must be signed in to change notification settings - Fork 25
/
command.go
62 lines (50 loc) · 1.29 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package hanu
import (
"github.com/sbstjn/allot"
)
// Handler is the interface for the handler function
type Handler func(ConversationInterface)
// CommandInterface defines a command interface
type CommandInterface interface {
Get() allot.CommandInterface
Description() string
Handle(conv ConversationInterface)
}
// Command a command
type Command struct {
command allot.CommandInterface
description string
handler Handler
}
// SetHandler sets the handler
func (c *Command) SetHandler(handler Handler) {
c.handler = handler
}
// Description returns the description
func (c Command) Description() string {
return c.description
}
// SetDescription sets the description
func (c *Command) SetDescription(text string) {
c.description = text
}
// Handle calls the command's handler
func (c Command) Handle(conv ConversationInterface) {
go c.handler(conv)
}
// Get returns the platzhalter command
func (c Command) Get() allot.CommandInterface {
return c.command
}
// Set defines the platzhalter command
func (c *Command) Set(cmd allot.CommandInterface) {
c.command = cmd
}
// NewCommand creates a new command
func NewCommand(text string, description string, handler Handler) Command {
cmd := Command{}
cmd.Set(allot.New(text))
cmd.SetDescription(description)
cmd.SetHandler(handler)
return cmd
}