2023-09-03 11:01:57 +00:00
package twitch
import (
2024-01-01 16:52:18 +00:00
"context"
"fmt"
2023-09-03 11:01:57 +00:00
"strings"
"time"
"github.com/Luzifer/twitch-bot/v3/plugins"
2024-01-01 16:52:18 +00:00
"github.com/pkg/errors"
2023-09-03 11:01:57 +00:00
)
func init ( ) {
regFn = append (
regFn ,
tplTwitchRecentGame ,
tplTwitchRecentTitle ,
tplTwitchStreamUptime ,
)
}
func tplTwitchRecentGame ( args plugins . RegistrationArguments ) {
args . RegisterTemplateFunction ( "recentGame" , plugins . GenericTemplateFunctionGetter ( func ( username string , v ... string ) ( string , error ) {
2024-01-01 16:52:18 +00:00
game , _ , err := args . GetTwitchClient ( ) . GetRecentStreamInfo ( context . Background ( ) , strings . TrimLeft ( username , "#" ) )
2023-09-03 11:01:57 +00:00
if len ( v ) > 0 && ( err != nil || game == "" ) {
2024-01-01 16:52:18 +00:00
return v [ 0 ] , nil //nolint:nilerr // This is a default fallback
2023-09-03 11:01:57 +00:00
}
2024-01-01 16:52:18 +00:00
return game , errors . Wrap ( err , "getting stream info" )
2023-09-03 11:01:57 +00:00
} ) , plugins . TemplateFuncDocumentation {
Description : "Returns the last played game name of the specified user (see shoutout example) or the `fallback` if the game could not be fetched. If no fallback was supplied the message will fail and not be sent." ,
Syntax : "recentGame <username> [fallback]" ,
Example : & plugins . TemplateFuncDocumentationExample {
Template : ` {{ recentGame "luziferus" "none" }} - {{ recentGame "thisuserdoesnotexist123" "none" }} ` ,
FakedOutput : "Metro Exodus - none" ,
} ,
} )
}
func tplTwitchRecentTitle ( args plugins . RegistrationArguments ) {
args . RegisterTemplateFunction ( "recentTitle" , plugins . GenericTemplateFunctionGetter ( func ( username string , v ... string ) ( string , error ) {
2024-01-01 16:52:18 +00:00
_ , title , err := args . GetTwitchClient ( ) . GetRecentStreamInfo ( context . Background ( ) , strings . TrimLeft ( username , "#" ) )
2023-09-03 11:01:57 +00:00
if len ( v ) > 0 && ( err != nil || title == "" ) {
2024-01-01 16:52:18 +00:00
return v [ 0 ] , nil //nolint:nilerr // This is a default fallback
2023-09-03 11:01:57 +00:00
}
2024-01-01 16:52:18 +00:00
return title , errors . Wrap ( err , "getting stream info" )
2023-09-03 11:01:57 +00:00
} ) , plugins . TemplateFuncDocumentation {
Description : "Returns the last stream title of the specified user or the `fallback` if the title could not be fetched. If no fallback was supplied the message will fail and not be sent." ,
Syntax : "recentTitle <username> [fallback]" ,
Example : & plugins . TemplateFuncDocumentationExample {
Template : ` {{ recentGame "luziferus" "none" }} - {{ recentGame "thisuserdoesnotexist123" "none" }} ` ,
FakedOutput : "Die Oper haben wir überlebt, mal sehen was uns sonst noch alles töten möchte… - none" ,
} ,
} )
}
func tplTwitchStreamUptime ( args plugins . RegistrationArguments ) {
args . RegisterTemplateFunction ( "streamUptime" , plugins . GenericTemplateFunctionGetter ( func ( username string ) ( time . Duration , error ) {
2024-01-01 16:52:18 +00:00
si , err := args . GetTwitchClient ( ) . GetCurrentStreamInfo ( context . Background ( ) , strings . TrimLeft ( username , "#" ) )
2023-09-03 11:01:57 +00:00
if err != nil {
2024-01-01 16:52:18 +00:00
return 0 , fmt . Errorf ( "getting stream info: %w" , err )
2023-09-03 11:01:57 +00:00
}
return time . Since ( si . StartedAt ) , nil
} ) , plugins . TemplateFuncDocumentation {
Description : "Returns the duration the stream is online (causes an error if no current stream is found)" ,
Syntax : "streamUptime <username>" ,
Example : & plugins . TemplateFuncDocumentationExample {
Template : ` {{ formatDuration ( streamUptime "luziferus" ) "hours" "minutes" "" }} ` ,
FakedOutput : "3 hours, 56 minutes" ,
} ,
} )
}