-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_irc.go
108 lines (96 loc) · 3.84 KB
/
functions_irc.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
package main
import (
"strings"
"github.com/pkg/errors"
"gopkg.in/irc.v4"
"github.com/Luzifer/go_helpers/v2/fieldcollection"
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
func init() {
tplFuncs.Register("arg", func(m *irc.Message, _ *plugins.Rule, _ *fieldcollection.FieldCollection) interface{} {
return func(arg int) (string, error) {
msgParts := strings.Split(m.Trailing(), " ")
if len(msgParts) <= arg {
return "", errors.New("argument not found")
}
return msgParts[arg], nil
}
}, plugins.TemplateFuncDocumentation{
Description: "Takes the message sent to the channel, splits by space and returns the Nth element",
Syntax: "arg <index>",
Example: &plugins.TemplateFuncDocumentationExample{
MessageContent: "!bsg @tester",
Template: `{{ arg 1 }} please refrain from BSG`,
ExpectedOutput: `@tester please refrain from BSG`,
},
})
tplFuncs.Register("chatterHasBadge", func(m *irc.Message, _ *plugins.Rule, _ *fieldcollection.FieldCollection) interface{} {
return func(badge string) bool {
badges := twitch.ParseBadgeLevels(m)
return badges.Has(badge)
}
}, plugins.TemplateFuncDocumentation{
Description: "Checks whether chatter writing the current line has the given badge in the current channel",
Syntax: "chatterHasBadge <badge>",
Example: &plugins.TemplateFuncDocumentationExample{
Template: `{{ chatterHasBadge "moderator" }}`,
ExpectedOutput: "true",
},
})
tplFuncs.Register(
"fixUsername",
plugins.GenericTemplateFunctionGetter(func(username string) string { return strings.TrimLeft(username, "@#") }),
plugins.TemplateFuncDocumentation{
Description: "Ensures the username no longer contains the `@` or `#` prefix",
Syntax: "fixUsername <username>",
Example: &plugins.TemplateFuncDocumentationExample{
Template: `{{ fixUsername .channel }} - {{ fixUsername "@luziferus" }}`,
ExpectedOutput: "example - luziferus",
},
},
)
tplFuncs.Register("group", func(m *irc.Message, r *plugins.Rule, _ *fieldcollection.FieldCollection) interface{} {
return func(idx int, fallback ...string) (string, error) {
fields := r.GetMatchMessage().FindStringSubmatch(m.Trailing())
if len(fields) <= idx {
return "", errors.New("group not found")
}
if fields[idx] == "" && len(fallback) > 0 {
return fallback[0], nil
}
return fields[idx], nil
}
}, plugins.TemplateFuncDocumentation{
Description: "Gets matching group specified by index from `match_message` regular expression, when `fallback` is defined, it is used when group has an empty match",
Syntax: "group <idx> [fallback]",
Example: &plugins.TemplateFuncDocumentationExample{
MatchMessage: "!command ([0-9]+) ([a-z]+) ?([a-z]*)",
MessageContent: "!command 12 test",
Template: `{{ group 2 "oops" }} - {{ group 3 "oops" }}`,
ExpectedOutput: "test - oops",
},
})
tplFuncs.Register(
"mention",
plugins.GenericTemplateFunctionGetter(func(username string) string { return "@" + strings.TrimLeft(username, "@#") }),
plugins.TemplateFuncDocumentation{
Description: "Strips username and converts into a mention",
Syntax: "mention <username>",
Example: &plugins.TemplateFuncDocumentationExample{
Template: `{{ mention "@user" }} {{ mention "user" }} {{ mention "#user" }}`,
ExpectedOutput: "@user @user @user",
},
},
)
tplFuncs.Register("tag", func(m *irc.Message, _ *plugins.Rule, _ *fieldcollection.FieldCollection) interface{} {
return func(tag string) string { return m.Tags[tag] }
}, plugins.TemplateFuncDocumentation{
Description: "Takes the message sent to the channel, returns the value of the tag specified",
Syntax: "tag <tagname>",
Example: &plugins.TemplateFuncDocumentationExample{
Template: `{{ tag "display-name" }}`,
ExpectedOutput: "ExampleUser",
},
})
}