2021-04-21 20:43:33 +00:00
package main
import (
"strings"
"github.com/pkg/errors"
2023-09-11 17:51:38 +00:00
"gopkg.in/irc.v4"
2021-11-25 22:48:16 +00:00
2024-04-03 19:00:28 +00:00
"github.com/Luzifer/go_helpers/v2/fieldcollection"
2023-08-25 21:37:37 +00:00
"github.com/Luzifer/twitch-bot/v3/pkg/twitch"
2022-11-02 21:38:14 +00:00
"github.com/Luzifer/twitch-bot/v3/plugins"
2021-04-21 20:43:33 +00:00
)
func init ( ) {
2024-04-03 19:00:28 +00:00
tplFuncs . Register ( "arg" , func ( m * irc . Message , _ * plugins . Rule , _ * fieldcollection . FieldCollection ) interface { } {
2021-04-21 20:43:33 +00:00
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
}
2023-08-25 21:37:37 +00:00
} , 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 ` ,
} ,
2021-04-21 20:43:33 +00:00
} )
2024-04-03 19:00:28 +00:00
tplFuncs . Register ( "chatterHasBadge" , func ( m * irc . Message , _ * plugins . Rule , _ * fieldcollection . FieldCollection ) interface { } {
2021-09-29 16:23:29 +00:00
return func ( badge string ) bool {
2023-08-25 21:37:37 +00:00
badges := twitch . ParseBadgeLevels ( m )
return badges . Has ( badge )
2021-09-29 16:23:29 +00:00
}
2023-08-25 21:37:37 +00:00
} , plugins . TemplateFuncDocumentation {
2023-10-26 16:39:20 +00:00
Description : "Checks whether chatter writing the current line has the given badge in the current channel" ,
Syntax : "chatterHasBadge <badge>" ,
2023-08-25 21:37:37 +00:00
Example : & plugins . TemplateFuncDocumentationExample {
2023-10-26 16:39:20 +00:00
Template : ` {{ chatterHasBadge "moderator" }} ` ,
2023-08-25 21:37:37 +00:00
ExpectedOutput : "true" ,
} ,
2021-09-29 16:23:29 +00:00
} )
2023-08-25 21:37:37 +00:00
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" ,
} ,
} ,
)
2021-04-21 20:43:33 +00:00
2024-04-03 19:00:28 +00:00
tplFuncs . Register ( "group" , func ( m * irc . Message , r * plugins . Rule , _ * fieldcollection . FieldCollection ) interface { } {
2021-09-08 13:09:01 +00:00
return func ( idx int , fallback ... string ) ( string , error ) {
2021-08-19 13:33:56 +00:00
fields := r . GetMatchMessage ( ) . FindStringSubmatch ( m . Trailing ( ) )
2021-04-21 20:43:33 +00:00
if len ( fields ) <= idx {
return "" , errors . New ( "group not found" )
}
2021-09-08 13:09:01 +00:00
if fields [ idx ] == "" && len ( fallback ) > 0 {
return fallback [ 0 ] , nil
}
2021-04-21 20:43:33 +00:00
return fields [ idx ] , nil
}
2023-08-25 21:37:37 +00:00
} , 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" ,
} ,
2021-04-21 20:43:33 +00:00
} )
2023-08-25 21:37:37 +00:00
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" ,
} ,
} ,
)
2023-05-27 19:58:58 +00:00
2024-04-03 19:00:28 +00:00
tplFuncs . Register ( "tag" , func ( m * irc . Message , _ * plugins . Rule , _ * fieldcollection . FieldCollection ) interface { } {
2023-09-11 17:51:38 +00:00
return func ( tag string ) string { return m . Tags [ tag ] }
2023-08-25 21:37:37 +00:00
} , 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" ,
} ,
2021-04-21 20:43:33 +00:00
} )
}