/
roll_function.go
51 lines (40 loc) · 1.14 KB
/
roll_function.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
package function
import (
"errors"
"math/rand"
"regexp"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
)
func Roll(message *discordgo.MessageCreate) (string, error) {
contentParts := strings.Split(message.Content, " ")
if len(contentParts) < 3 {
return "", errors.New("Not enough arguments")
}
sum := 0
text := "**" + message.Author.Username + "** rolled:\n"
argExpression, _ := regexp.Compile("^[1-9]\\d*d[1-9]\\d*((\\+|-)\\d+){0,1}")
submatchExpression, _ := regexp.Compile("(\\+|-){0,1}\\d+")
for _, part := range contentParts[2:] {
if !argExpression.MatchString(part) {
return "", errors.New("Bad dice expression format")
}
argSubmatches := submatchExpression.FindAllStringSubmatch(part, -1)
count, _ := strconv.Atoi(argSubmatches[0][0])
point, _ := strconv.Atoi(argSubmatches[1][0])
for i := 0; i < count; i++ {
result := rand.Intn(point) + 1
sum += result
text += strconv.Itoa(result) + "\n"
}
if len(argSubmatches) == 3 {
plus, _ := strconv.Atoi(argSubmatches[2][0])
sum += plus
text += argSubmatches[2][0] + "\n"
}
text += "-----\n"
}
text += strconv.Itoa(sum)
return text, nil
}