-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.go
53 lines (43 loc) · 1.19 KB
/
response.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
package main
import (
"context"
"strings"
"text/template"
"github.com/bwmarrin/discordgo"
"github.com/travis-g/dice/math"
)
// Response templates for dice roll message responses.
var (
ResponseTemplate = "{{if .Name}}{{.Name}} rolled{{end}}{{if .Expression}} `{{.Expression}}`{{end}}{{if .Label}} _{{.Label}}_{{end}}: `{{.Rolled}}` = **{{.Result}}**"
)
var (
responseResultTemplateCompiled = template.Must(
template.New("result").Parse(ResponsePrefix + ResponseTemplate),
)
)
// Response is a message response for dice roll responses.
type Response struct {
*math.ExpressionResult
// Name of who made the roll (optional)
Name string
Rolled string
Result string
Expression string
Label string
FriendlyError error
Error error
}
func (r *Response) Interaction(ctx context.Context) (i *discordgo.Interaction) {
_, in, _ := FromContext(ctx)
u := UserFromInteraction(in)
if isRollPublic(i) {
r.Name = u.Mention()
}
return i
}
func (r *Response) MessageSend(ctx context.Context) *discordgo.MessageSend {
return new(discordgo.MessageSend)
}
func executeResponseTemplate(b *strings.Builder, r *Response) {
responseResultTemplateCompiled.Execute(b, r)
}