forked from lovelaced/glitzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
79 lines (67 loc) · 2.03 KB
/
base.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package core
import (
"errors"
"github.com/bheru27/glitzz/config"
"github.com/bheru27/glitzz/logging"
"github.com/bheru27/glitzz/util"
"github.com/thoj/go-ircevent"
)
func NewBase(moduleName string, sender Sender, conf config.Config) Base {
return Base{
Config: conf,
Sender: sender,
commands: make(map[string]ModuleCommand),
Log: logging.New("modules/" + moduleName),
}
}
type CommandArguments struct {
Arguments []string
Nick string
Target string
}
type ModuleCommand func(arguments CommandArguments) ([]string, error)
type Base struct {
Config config.Config
Sender Sender
commands map[string]ModuleCommand
Log logging.Logger
}
func (b *Base) AddCommand(name string, moduleCommand ModuleCommand) {
b.Log.Debug("adding command", "name", name)
b.commands[name] = moduleCommand
}
var argumentParsingError = errors.New("Argument parsing failed")
var commandNotFoundError = errors.New("Command not found")
var commandParsingError = errors.New("Command parsing failed")
func isMalformedCommandError(err error) bool {
return err == argumentParsingError ||
err == commandNotFoundError ||
err == commandParsingError
}
func (b *Base) RunCommand(command Command) ([]string, error) {
if name, err := b.GetCommandName(command.Text); err == nil {
moduleCommand, ok := b.commands[name]
if ok {
if arguments, err := b.GetCommandArguments(command.Text); err == nil {
b.Log.Debug("executing command", "name", name)
commandArguments := CommandArguments{
Arguments: arguments,
Nick: command.Nick,
Target: command.Target,
}
return moduleCommand(commandArguments)
}
return nil, argumentParsingError
}
return nil, commandNotFoundError
}
return nil, commandParsingError
}
func (b *Base) HandleEvent(event *irc.Event) {
}
func (b *Base) GetCommandName(msg string) (string, error) {
return util.GetCommandName(msg, b.Config.CommandPrefix)
}
func (b *Base) GetCommandArguments(msg string) ([]string, error) {
return util.GetCommandArguments(msg, b.Config.CommandPrefix)
}