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"` DisableOnMatchMessages []string `yaml:"disable_on_match_messages"`
DisableOnPermit bool `yaml:"disable_on_permit"` DisableOnOffline bool `yaml:"disable_on_offline"`
DisableOn []string `yaml:"disable_on"` DisableOnPermit bool `yaml:"disable_on_permit"`
EnableOn []string `yaml:"enable_on"` DisableOn []string `yaml:"disable_on"`
EnableOn []string `yaml:"enable_on"`
matchMessage *regexp.Regexp matchMessage *regexp.Regexp
disableOnMatchMessages []*regexp.Regexp disableOnMatchMessages []*regexp.Regexp
@ -172,6 +173,17 @@ func (r *rule) Matches(m *irc.Message, event *string) bool {
return false 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! // Nothing objected: Matches!
return true return true
} }

View file

@ -73,6 +73,31 @@ func (t twitchClient) GetFollowDate(from, to string) (time.Time, error) {
return payload.Data[0].FollowedAt, nil 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) { func (t twitchClient) getIDForUsername(username string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout) ctx, cancel := context.WithTimeout(context.Background(), twitchRequestTimeout)
defer cancel() defer cancel()

View file

@ -42,6 +42,9 @@ rules: # See below for examples
# Add a cooldown to the command (not to trigger counters twice, ...) # Add a cooldown to the command (not to trigger counters twice, ...)
cooldown: 1s # Duration value: 1s / 1m / 1h 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 actions on this rule if the user has an active permit
disable_on_permit: false disable_on_permit: false