diff --git a/cli.database.go b/cli.database.go index 264a7d5..2529df4 100644 --- a/cli.database.go +++ b/cli.database.go @@ -3,6 +3,8 @@ package main import ( "os" + "github.com/strimertul/strimertul/utils" + "github.com/strimertul/strimertul/database" "github.com/urfave/cli/v2" @@ -16,7 +18,7 @@ func cliImport(ctx *cli.Context) error { if err != nil { return fatalError(err, "could not open import file for reading") } - defer file.Close() + defer utils.Close(file, logger) inStream = file } var entries map[string]string @@ -47,7 +49,7 @@ func cliRestore(ctx *cli.Context) error { if err != nil { return fatalError(err, "could not open import file for reading") } - defer file.Close() + defer utils.Close(file, logger) inStream = file } @@ -73,7 +75,7 @@ func cliExport(ctx *cli.Context) error { if err != nil { return fatalError(err, "could not open output file for writing") } - defer file.Close() + defer utils.Close(file, logger) outStream = file } diff --git a/database/driver.pebble.go b/database/driver.pebble.go index 410fcf5..0f62b00 100644 --- a/database/driver.pebble.go +++ b/database/driver.pebble.go @@ -6,6 +6,8 @@ import ( "os" "path/filepath" + "github.com/strimertul/strimertul/utils" + "github.com/cockroachdb/pebble" kv "github.com/strimertul/kilovolt/v9" pebble_driver "github.com/strimertul/kv-pebble" @@ -50,7 +52,7 @@ func (p *PebbleDatabase) Hub() *kv.Hub { func (p *PebbleDatabase) Close() error { err := p.db.Close() if err != nil { - return fmt.Errorf("Could not close database: %w", err) + return fmt.Errorf("could not close database: %w", err) } return nil } @@ -89,8 +91,12 @@ func (p *PebbleDatabase) Restore(file io.Reader) error { } func (p *PebbleDatabase) Backup(file io.Writer) error { - iter := p.db.NewSnapshot().NewIter(&pebble.IterOptions{}) - defer iter.Close() + snapshot := p.db.NewSnapshot() + defer utils.Close(snapshot, p.logger) + + iter := snapshot.NewIter(&pebble.IterOptions{}) + defer utils.Close(iter, p.logger) + out := make(map[string]string) for iter.First(); iter.Valid(); iter.Next() { val, err := iter.ValueAndErr() diff --git a/go.mod b/go.mod index 10ccfd4..0624bb8 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( gopkg.in/natefinch/lumberjack.v2 v2.0.0 ) -replace github.com/nicklaw5/helix/v2 => github.com/ashkeel/helix/v2 v2.11.0-ws +replace github.com/nicklaw5/helix/v2 => github.com/ashkeel/helix/v2 v2.20.0-ws require ( github.com/DataDog/zstd v1.5.2 // indirect diff --git a/go.sum b/go.sum index c5ca04e..91cbf87 100644 --- a/go.sum +++ b/go.sum @@ -22,8 +22,8 @@ github.com/apenwarr/fixconsole v0.0.0-20191012055117-5a9f6489cc29/go.mod h1:JYWa github.com/apenwarr/w32 v0.0.0-20190407065021-aa00fece76ab h1:CMGzRRCjnD50RjUFSArBLuCxiDvdp7b8YPAcikBEQ+k= github.com/apenwarr/w32 v0.0.0-20190407065021-aa00fece76ab/go.mod h1:nfFtvHn2Hgs9G1u0/J6LHQv//EksNC+7G8vXmd1VTJ8= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= -github.com/ashkeel/helix/v2 v2.11.0-ws h1:AG2mWRs7qfhigv+UcNDkjJaSSbtiJIM1njlwLu7FVa4= -github.com/ashkeel/helix/v2 v2.11.0-ws/go.mod h1:zZcKsyyBWDli34x3QleYsVMiiNGMXPAEU5NjsiZDtvY= +github.com/ashkeel/helix/v2 v2.20.0-ws h1:IvwMHs4PBCoVgu0IVlTZGyQG8h40zBY/FYmRXGcfEco= +github.com/ashkeel/helix/v2 v2.20.0-ws/go.mod h1:zZcKsyyBWDli34x3QleYsVMiiNGMXPAEU5NjsiZDtvY= github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= diff --git a/twitch/client.auth.go b/twitch/client.auth.go index 0a2d5f5..2c03ae5 100644 --- a/twitch/client.auth.go +++ b/twitch/client.auth.go @@ -22,7 +22,7 @@ func (c *Client) GetAuthorizationURL() string { } return c.API.GetAuthorizationURL(&helix.AuthorizationURLParams{ ResponseType: "code", - Scopes: []string{"bits:read channel:read:subscriptions channel:read:redemptions channel:read:polls channel:read:predictions channel:read:hype_train user_read chat:read chat:edit channel:moderate whispers:read whispers:edit moderator:read:chatters"}, + Scopes: []string{"bits:read channel:read:subscriptions channel:read:redemptions channel:read:polls channel:read:predictions channel:read:hype_train user_read chat:read chat:edit channel:moderate whispers:read whispers:edit moderator:read:chatters moderator:read:followers"}, }) } diff --git a/twitch/client.eventsub.go b/twitch/client.eventsub.go index 6a0a731..09372f6 100644 --- a/twitch/client.eventsub.go +++ b/twitch/client.eventsub.go @@ -4,6 +4,8 @@ import ( "fmt" "time" + "github.com/strimertul/strimertul/utils" + "github.com/gorilla/websocket" jsoniter "github.com/json-iterator/go" "github.com/nicklaw5/helix/v2" @@ -30,7 +32,7 @@ func (c *Client) connectWebsocket(url string, userClient *helix.Client) (string, c.logger.Error("could not connect to eventsub ws", zap.Error(err)) return "", err } - defer connection.Close() + defer utils.Close(connection, c.logger) received := make(chan []byte, 10) wsErr := make(chan error, 1) @@ -163,7 +165,7 @@ func (c *Client) addSubscriptionsForSession(userClient *helix.Client, session st Condition: topicCondition(topic, c.User.ID), }) if sub.Error != "" || sub.ErrorMessage != "" { - c.logger.Error("subscription error", zap.String("err", sub.Error), zap.String("message", sub.ErrorMessage)) + c.logger.Error("subscription error", zap.String("topic", topic), zap.String("topic-version", version), zap.String("err", sub.Error), zap.String("message", sub.ErrorMessage)) return fmt.Errorf("%s: %s", sub.Error, sub.ErrorMessage) } if err != nil { @@ -180,6 +182,11 @@ func topicCondition(topic string, id string) helix.EventSubCondition { return helix.EventSubCondition{ ToBroadcasterUserID: id, } + case "channel.follow": + return helix.EventSubCondition{ + BroadcasterUserID: id, + ModeratorUserID: id, + } default: return helix.EventSubCondition{ BroadcasterUserID: id, @@ -218,7 +225,7 @@ type EventSubMetadata struct { var subscriptionVersions = map[string]string{ helix.EventSubTypeChannelUpdate: "1", - helix.EventSubTypeChannelFollow: "1", + helix.EventSubTypeChannelFollow: "2", helix.EventSubTypeChannelSubscription: "1", helix.EventSubTypeChannelSubscriptionGift: "1", helix.EventSubTypeChannelSubscriptionMessage: "1", diff --git a/utils/deferlogger.go b/utils/deferlogger.go new file mode 100644 index 0000000..d0fdd9e --- /dev/null +++ b/utils/deferlogger.go @@ -0,0 +1,16 @@ +package utils + +import ( + "io" + "reflect" + "runtime/debug" + + "go.uber.org/zap" +) + +func Close(res io.Closer, logger *zap.Logger) { + err := res.Close() + if err != nil { + logger.Error("could not close resource", zap.String("name", reflect.TypeOf(res).String()), zap.Error(err), zap.String("stack", string(debug.Stack()))) + } +}