Add config parameter to disable rule on offline stream

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-03-21 14:04:04 +01:00
parent d434e5b623
commit f6a1c33cb7
Signed by: luzifer
GPG Key ID: 0066F03ED215AD7D
3 changed files with 43 additions and 3 deletions

View File

@ -41,9 +41,10 @@ type rule struct {
DisableOnMatchMessages []string `yaml:"disable_on_match_messages"`
DisableOnPermit bool `yaml:"disable_on_permit"`
DisableOn []string `yaml:"disable_on"`
EnableOn []string `yaml:"enable_on"`
DisableOnOffline bool `yaml:"disable_on_offline"`
DisableOnPermit bool `yaml:"disable_on_permit"`
DisableOn []string `yaml:"disable_on"`
EnableOn []string `yaml:"enable_on"`
matchMessage *regexp.Regexp
disableOnMatchMessages []*regexp.Regexp
@ -172,6 +173,17 @@ func (r *rule) Matches(m *irc.Message, event *string) bool {
return false
}
if r.DisableOnOffline {
streamLive, err := twitch.HasLiveStream(m.Params[0])
if err != nil {
logger.WithError(err).Error("Unable to determine live status")
return false
}
if !streamLive {
return false
}
}
// Nothing objected: Matches!
return true
}

View File

@ -73,6 +73,31 @@ func (t twitchClient) GetFollowDate(from, to string) (time.Time, error) {
return payload.Data[0].FollowedAt, nil
}
func (t twitchClient) HasLiveStream(username string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
defer cancel()
var payload struct {
Data []struct {
ID string `json:"id"`
UserLogin string `json:"user_login"`
Type string `json:"type"`
} `json:"data"`
}
if err := t.request(
ctx,
http.MethodGet,
fmt.Sprintf("https://api.twitch.tv/helix/streams?user_login=%s", username),
nil,
&payload,
); err != nil {
return false, errors.Wrap(err, "request stream info")
}
return len(payload.Data) == 1 && payload.Data[0].Type == "live", nil
}
func (t twitchClient) getIDForUsername(username string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
defer cancel()

View File

@ -42,6 +42,9 @@ rules: # See below for examples
# Add a cooldown to the command (not to trigger counters twice, ...)
cooldown: 1s # Duration value: 1s / 1m / 1h
# Disable actions when the matched channel has no active stream
disable_on_offline: false
# Disable actions on this rule if the user has an active permit
disable_on_permit: false