From f6a1c33cb798f9688764c0abd220b8cdd14ce905 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 21 Mar 2021 14:04:04 +0100 Subject: [PATCH] Add config parameter to disable rule on offline stream Signed-off-by: Knut Ahlers --- config.go | 18 +++++++++++++++--- twitch.go | 25 +++++++++++++++++++++++++ wiki/Home.md | 3 +++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 48fdc27..5ae4068 100644 --- a/config.go +++ b/config.go @@ -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 } diff --git a/twitch.go b/twitch.go index ed15cdb..7b8c9e7 100644 --- a/twitch.go +++ b/twitch.go @@ -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() diff --git a/wiki/Home.md b/wiki/Home.md index 9e21f3e..6c4e73b 100644 --- a/wiki/Home.md +++ b/wiki/Home.md @@ -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