-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
117 lines (100 loc) · 2.97 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"reflect"
"strconv"
"strings"
telegram "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/theovidal/parcolar/lib"
)
// HandleCommand parses an incoming request to execute a bot command
func HandleCommand(bot *lib.Bot, update telegram.Update, isCallback bool) (err error) {
var commandName string
var args []string
var chatID int64
if isCallback {
parts := strings.Split(strings.TrimPrefix(update.CallbackQuery.Data, "/"), " ")
commandName = parts[0]
args = parts[1:]
chatID = update.CallbackQuery.Message.Chat.ID
} else {
commandName = update.Message.Command()
if update.Message.CommandArguments() != "" {
args = strings.Split(update.Message.CommandArguments(), " ")
}
chatID = update.Message.Chat.ID
}
command, exists := bot.Commands[commandName]
if !exists {
return bot.Error(chatID, "Oups, il semble que cette commande soit inconnue!")
}
var flags map[string]interface{}
args, flags, err = ParseFlags(args, command.Flags)
if err != nil {
return bot.Error(chatID, err.Error())
}
return command.Execute(bot, &update, chatID, args, flags)
}
// ParseFlags extracts flags at the beginning of the command, holding customizable parameters
func ParseFlags(args []string, flags map[string]lib.Flag) (parsedArgs []string, parsedFlags map[string]interface{}, err error) {
parsedFlags = make(map[string]interface{})
if flags == nil || len(flags) == 0 {
for name, flag := range flags {
parsedFlags[name] = flag.Value
}
return args, parsedFlags, nil
}
for index, arg := range args {
if !strings.Contains(arg, "=") {
parsedArgs = append(parsedArgs, args[index])
continue
}
parts := strings.Split(arg, "=")
name := parts[0]
flag, found := flags[name]
if !found {
parsedArgs = append(parsedArgs, args[index])
continue
}
var value interface{}
switch flag.Value.(type) {
case float64:
value, err = strconv.ParseFloat(parts[1], 64)
if err != nil {
err = fmt.Errorf("Le paramètre `%s` attend un nombre réel comme valeur.", name)
return
}
case int, bool:
value, err = strconv.Atoi(parts[1])
if err != nil {
err = fmt.Errorf("Le paramètre `%s` attend un nombre entier comme valeur.", name)
return
}
if reflect.TypeOf(flag.Value).Name() == "bool" {
if value == 0 {
value = false
} else if value == 1 {
value = true
} else {
err = fmt.Errorf("Le paramètre `%s` attend un booléen (0 ou 1) comme valeur.", name)
return
}
}
case string:
value = parts[1]
if exists := lib.Contains(*flag.Enum, value.(string)); !exists && flag.Enum != nil && len(*flag.Enum) > 0 {
err = fmt.Errorf("Les valeurs acceptées pour le paramètre `%s` sont : %s.", name, strings.Join(*flag.Enum, ", "))
return
}
default:
lib.Fatal("Unhandled type for flag %s", name)
}
parsedFlags[name] = value
}
for flag, defaultFlag := range flags {
if _, set := parsedFlags[flag]; !set {
parsedFlags[flag] = defaultFlag.Value
}
}
return
}