mirror of
https://git.sr.ht/~ashkeel/strimertul
synced 2024-09-20 02:00:49 +00:00
refactor: less repetition but it's probably bad
This commit is contained in:
parent
bc54d07e86
commit
e3ee43e6ae
11 changed files with 45 additions and 37 deletions
|
@ -3,7 +3,6 @@ package main
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
jsoniter "github.com/json-iterator/go"
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,7 +18,7 @@ func cliImport(ctx *cli.Context) error {
|
||||||
inStream = file
|
inStream = file
|
||||||
}
|
}
|
||||||
var entries map[string]string
|
var entries map[string]string
|
||||||
err := jsoniter.ConfigFastest.NewDecoder(inStream).Decode(&entries)
|
err := json.NewDecoder(inStream).Decode(&entries)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fatalError(err, "could not decode import file")
|
return fatalError(err, "could not decode import file")
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/cockroachdb/pebble"
|
"github.com/cockroachdb/pebble"
|
||||||
jsoniter "github.com/json-iterator/go"
|
|
||||||
"github.com/labstack/gommon/log"
|
"github.com/labstack/gommon/log"
|
||||||
kv "github.com/strimertul/kilovolt/v9"
|
kv "github.com/strimertul/kilovolt/v9"
|
||||||
pebble_driver "github.com/strimertul/kv-pebble"
|
pebble_driver "github.com/strimertul/kv-pebble"
|
||||||
|
@ -69,7 +68,7 @@ func (p *PebbleDatabase) Export(file io.Writer) error {
|
||||||
|
|
||||||
func (p *PebbleDatabase) Restore(file io.Reader) error {
|
func (p *PebbleDatabase) Restore(file io.Reader) error {
|
||||||
in := make(map[string]string)
|
in := make(map[string]string)
|
||||||
err := jsoniter.ConfigFastest.NewDecoder(file).Decode(&in)
|
err := json.NewDecoder(file).Decode(&in)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Could not decode backup: %w", err)
|
return fmt.Errorf("Could not decode backup: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -91,5 +90,5 @@ func (p *PebbleDatabase) Backup(file io.Writer) error {
|
||||||
for iter.First(); iter.Valid(); iter.Next() {
|
for iter.First(); iter.Valid(); iter.Next() {
|
||||||
out[string(iter.Key())] = string(iter.Value())
|
out[string(iter.Key())] = string(iter.Value())
|
||||||
}
|
}
|
||||||
return jsoniter.ConfigFastest.NewEncoder(file).Encode(out)
|
return json.NewEncoder(file).Encode(out)
|
||||||
}
|
}
|
||||||
|
|
4
main.go
4
main.go
|
@ -8,6 +8,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"github.com/urfave/cli/v2"
|
||||||
"github.com/wailsapp/wails/v2"
|
"github.com/wailsapp/wails/v2"
|
||||||
"github.com/wailsapp/wails/v2/pkg/options"
|
"github.com/wailsapp/wails/v2/pkg/options"
|
||||||
|
@ -23,6 +25,8 @@ import (
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var json = jsoniter.ConfigFastest
|
||||||
|
|
||||||
const databaseDefaultDriver = "pebble"
|
const databaseDefaultDriver = "pebble"
|
||||||
|
|
||||||
var appVersion = "v0.0.0-UNKNOWN"
|
var appVersion = "v0.0.0-UNKNOWN"
|
||||||
|
|
|
@ -20,6 +20,8 @@ import (
|
||||||
"github.com/strimertul/strimertul/modules"
|
"github.com/strimertul/strimertul/modules"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var json = jsoniter.ConfigFastest
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Config ServerConfig
|
Config ServerConfig
|
||||||
db *database.DBModule
|
db *database.DBModule
|
||||||
|
@ -136,7 +138,7 @@ func (s *Server) Listen() error {
|
||||||
if key == ServerConfigKey {
|
if key == ServerConfigKey {
|
||||||
oldBind := s.Config.Bind
|
oldBind := s.Config.Bind
|
||||||
oldPassword := s.Config.KVPassword
|
oldPassword := s.Config.KVPassword
|
||||||
err := jsoniter.ConfigFastest.Unmarshal([]byte(value), &s.Config)
|
err := json.Unmarshal([]byte(value), &s.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Error("Failed to unmarshal config", zap.Error(err))
|
s.logger.Error("Failed to unmarshal config", zap.Error(err))
|
||||||
return
|
return
|
||||||
|
|
|
@ -15,6 +15,8 @@ import (
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var json = jsoniter.ConfigFastest
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrRedeemInCooldown = errors.New("redeem is on cooldown")
|
ErrRedeemInCooldown = errors.New("redeem is on cooldown")
|
||||||
ErrGoalNotFound = errors.New("goal not found")
|
ErrGoalNotFound = errors.New("goal not found")
|
||||||
|
@ -83,7 +85,7 @@ func Register(manager *modules.Manager) error {
|
||||||
loyalty.points = make(map[string]PointsEntry)
|
loyalty.points = make(map[string]PointsEntry)
|
||||||
for k, v := range points {
|
for k, v := range points {
|
||||||
var entry PointsEntry
|
var entry PointsEntry
|
||||||
err := jsoniter.ConfigFastest.UnmarshalFromString(v, &entry)
|
err := json.UnmarshalFromString(v, &entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -145,35 +147,35 @@ func (m *Manager) update(key, value string) {
|
||||||
err = func() error {
|
err = func() error {
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
return jsoniter.ConfigFastest.UnmarshalFromString(value, &m.config)
|
return json.UnmarshalFromString(value, &m.config)
|
||||||
}()
|
}()
|
||||||
case GoalsKey:
|
case GoalsKey:
|
||||||
err = func() error {
|
err = func() error {
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
return jsoniter.ConfigFastest.UnmarshalFromString(value, &m.goals)
|
return json.UnmarshalFromString(value, &m.goals)
|
||||||
}()
|
}()
|
||||||
case RewardsKey:
|
case RewardsKey:
|
||||||
err = func() error {
|
err = func() error {
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
return jsoniter.ConfigFastest.UnmarshalFromString(value, &m.rewards)
|
return json.UnmarshalFromString(value, &m.rewards)
|
||||||
}()
|
}()
|
||||||
case QueueKey:
|
case QueueKey:
|
||||||
err = func() error {
|
err = func() error {
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
return jsoniter.ConfigFastest.UnmarshalFromString(value, &m.queue)
|
return json.UnmarshalFromString(value, &m.queue)
|
||||||
}()
|
}()
|
||||||
case CreateRedeemRPC:
|
case CreateRedeemRPC:
|
||||||
var redeem Redeem
|
var redeem Redeem
|
||||||
err = jsoniter.ConfigFastest.UnmarshalFromString(value, &redeem)
|
err = json.UnmarshalFromString(value, &redeem)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = m.AddRedeem(redeem)
|
err = m.AddRedeem(redeem)
|
||||||
}
|
}
|
||||||
case RemoveRedeemRPC:
|
case RemoveRedeemRPC:
|
||||||
var redeem Redeem
|
var redeem Redeem
|
||||||
err = jsoniter.ConfigFastest.UnmarshalFromString(value, &redeem)
|
err = json.UnmarshalFromString(value, &redeem)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = m.RemoveRedeem(redeem)
|
err = m.RemoveRedeem(redeem)
|
||||||
}
|
}
|
||||||
|
@ -183,7 +185,7 @@ func (m *Manager) update(key, value string) {
|
||||||
// User point changed
|
// User point changed
|
||||||
case strings.HasPrefix(key, PointsPrefix):
|
case strings.HasPrefix(key, PointsPrefix):
|
||||||
var entry PointsEntry
|
var entry PointsEntry
|
||||||
err = jsoniter.ConfigFastest.UnmarshalFromString(value, &entry)
|
err = json.UnmarshalFromString(value, &entry)
|
||||||
user := key[len(PointsPrefix):]
|
user := key[len(PointsPrefix):]
|
||||||
func() {
|
func() {
|
||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
|
@ -205,7 +207,7 @@ func (m *Manager) handleRemote(key, value string) {
|
||||||
case KVExLoyaltyRedeem:
|
case KVExLoyaltyRedeem:
|
||||||
// Parse request
|
// Parse request
|
||||||
var redeemRequest ExLoyaltyRedeem
|
var redeemRequest ExLoyaltyRedeem
|
||||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &redeemRequest)
|
err := json.UnmarshalFromString(value, &redeemRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.logger.Warn("error decoding redeem request", zap.Error(err))
|
m.logger.Warn("error decoding redeem request", zap.Error(err))
|
||||||
break
|
break
|
||||||
|
@ -229,7 +231,7 @@ func (m *Manager) handleRemote(key, value string) {
|
||||||
case KVExLoyaltyContribute:
|
case KVExLoyaltyContribute:
|
||||||
// Parse request
|
// Parse request
|
||||||
var contributeRequest ExLoyaltyContribute
|
var contributeRequest ExLoyaltyContribute
|
||||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &contributeRequest)
|
err := json.UnmarshalFromString(value, &contributeRequest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.logger.Warn("error decoding contribution request", zap.Error(err))
|
m.logger.Warn("error decoding contribution request", zap.Error(err))
|
||||||
break
|
break
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
|
|
||||||
"github.com/Masterminds/sprig/v3"
|
"github.com/Masterminds/sprig/v3"
|
||||||
irc "github.com/gempir/go-twitch-irc/v3"
|
irc "github.com/gempir/go-twitch-irc/v3"
|
||||||
jsoniter "github.com/json-iterator/go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
|
@ -167,7 +166,7 @@ func (b *Bot) updateCommands(key, value string) {
|
||||||
err := func() error {
|
err := func() error {
|
||||||
b.mu.Lock()
|
b.mu.Lock()
|
||||||
defer b.mu.Unlock()
|
defer b.mu.Unlock()
|
||||||
return jsoniter.ConfigFastest.UnmarshalFromString(value, &b.customCommands)
|
return json.UnmarshalFromString(value, &b.customCommands)
|
||||||
}()
|
}()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.logger.Error("failed to decode new custom commands", zap.Error(err))
|
b.logger.Error("failed to decode new custom commands", zap.Error(err))
|
||||||
|
|
1
modules/twitch/client.eventsub.go
Normal file
1
modules/twitch/client.eventsub.go
Normal file
|
@ -0,0 +1 @@
|
||||||
|
package twitch
|
|
@ -15,6 +15,8 @@ import (
|
||||||
"github.com/strimertul/strimertul/modules/loyalty"
|
"github.com/strimertul/strimertul/modules/loyalty"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var json = jsoniter.ConfigFastest
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
Config Config
|
Config Config
|
||||||
Bot *Bot
|
Bot *Bot
|
||||||
|
@ -97,7 +99,7 @@ func Register(manager *modules.Manager) error {
|
||||||
go db.Subscribe(func(key, value string) {
|
go db.Subscribe(func(key, value string) {
|
||||||
switch key {
|
switch key {
|
||||||
case ConfigKey:
|
case ConfigKey:
|
||||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &config)
|
err := json.UnmarshalFromString(value, &config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("failed to unmarshal config", zap.Error(err))
|
logger.Error("failed to unmarshal config", zap.Error(err))
|
||||||
return
|
return
|
||||||
|
@ -111,7 +113,7 @@ func Register(manager *modules.Manager) error {
|
||||||
logger.Info("reloaded/updated Twitch API")
|
logger.Info("reloaded/updated Twitch API")
|
||||||
case BotConfigKey:
|
case BotConfigKey:
|
||||||
var twitchBotConfig BotConfig
|
var twitchBotConfig BotConfig
|
||||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &twitchBotConfig)
|
err := json.UnmarshalFromString(value, &twitchBotConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("failed to unmarshal config", zap.Error(err))
|
logger.Error("failed to unmarshal config", zap.Error(err))
|
||||||
return
|
return
|
||||||
|
|
|
@ -20,9 +20,11 @@ type BotConfig struct {
|
||||||
ChatHistory int `json:"chat_history"`
|
ChatHistory int `json:"chat_history"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const ChatEventKey = "twitch/ev/chat-message"
|
const (
|
||||||
const ChatHistoryKey = "twitch/chat-history"
|
ChatEventKey = "twitch/ev/chat-message"
|
||||||
const ChatActivityKey = "twitch/chat-activity"
|
ChatHistoryKey = "twitch/chat-history"
|
||||||
|
ChatActivityKey = "twitch/chat-activity"
|
||||||
|
)
|
||||||
|
|
||||||
type BotCustomCommand struct {
|
type BotCustomCommand struct {
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
|
|
|
@ -2,7 +2,6 @@ package twitch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
@ -20,7 +19,7 @@ const BotAlertsKey = "twitch/bot-modules/alerts/config"
|
||||||
type eventSubNotification struct {
|
type eventSubNotification struct {
|
||||||
Subscription helix.EventSubSubscription `json:"subscription"`
|
Subscription helix.EventSubSubscription `json:"subscription"`
|
||||||
Challenge string `json:"challenge"`
|
Challenge string `json:"challenge"`
|
||||||
Event json.RawMessage `json:"event"`
|
Event jsoniter.RawMessage `json:"event"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BotAlertsConfig struct {
|
type BotAlertsConfig struct {
|
||||||
|
@ -114,7 +113,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
||||||
|
|
||||||
go bot.api.db.Subscribe(func(key, value string) {
|
go bot.api.db.Subscribe(func(key, value string) {
|
||||||
if key == BotAlertsKey {
|
if key == BotAlertsKey {
|
||||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &mod.Config)
|
err := json.UnmarshalFromString(value, &mod.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bot.logger.Debug("error reloading timer config", zap.Error(err))
|
bot.logger.Debug("error reloading timer config", zap.Error(err))
|
||||||
} else {
|
} else {
|
||||||
|
@ -238,7 +237,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
||||||
go bot.api.db.Subscribe(func(key, value string) {
|
go bot.api.db.Subscribe(func(key, value string) {
|
||||||
if key == "stulbe/ev/webhook" {
|
if key == "stulbe/ev/webhook" {
|
||||||
var ev eventSubNotification
|
var ev eventSubNotification
|
||||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &ev)
|
err := json.UnmarshalFromString(value, &ev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bot.logger.Debug("error parsing webhook payload", zap.Error(err))
|
bot.logger.Debug("error parsing webhook payload", zap.Error(err))
|
||||||
return
|
return
|
||||||
|
@ -251,7 +250,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
||||||
}
|
}
|
||||||
// Parse as follow event
|
// Parse as follow event
|
||||||
var followEv helix.EventSubChannelFollowEvent
|
var followEv helix.EventSubChannelFollowEvent
|
||||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &followEv)
|
err := json.Unmarshal(ev.Event, &followEv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bot.logger.Debug("error parsing follow event", zap.Error(err))
|
bot.logger.Debug("error parsing follow event", zap.Error(err))
|
||||||
return
|
return
|
||||||
|
@ -272,7 +271,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
||||||
}
|
}
|
||||||
// Parse as raid event
|
// Parse as raid event
|
||||||
var raidEv helix.EventSubChannelRaidEvent
|
var raidEv helix.EventSubChannelRaidEvent
|
||||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &raidEv)
|
err := json.Unmarshal(ev.Event, &raidEv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bot.logger.Debug("error parsing raid event", zap.Error(err))
|
bot.logger.Debug("error parsing raid event", zap.Error(err))
|
||||||
return
|
return
|
||||||
|
@ -309,7 +308,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
||||||
}
|
}
|
||||||
// Parse as cheer event
|
// Parse as cheer event
|
||||||
var cheerEv helix.EventSubChannelCheerEvent
|
var cheerEv helix.EventSubChannelCheerEvent
|
||||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &cheerEv)
|
err := json.Unmarshal(ev.Event, &cheerEv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bot.logger.Debug("error parsing cheer event", zap.Error(err))
|
bot.logger.Debug("error parsing cheer event", zap.Error(err))
|
||||||
return
|
return
|
||||||
|
@ -346,7 +345,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
||||||
}
|
}
|
||||||
// Parse as subscription event
|
// Parse as subscription event
|
||||||
var subEv helix.EventSubChannelSubscribeEvent
|
var subEv helix.EventSubChannelSubscribeEvent
|
||||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &subEv)
|
err := json.Unmarshal(ev.Event, &subEv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bot.logger.Debug("error parsing sub event", zap.Error(err))
|
bot.logger.Debug("error parsing sub event", zap.Error(err))
|
||||||
return
|
return
|
||||||
|
@ -359,7 +358,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
||||||
}
|
}
|
||||||
// Parse as subscription event
|
// Parse as subscription event
|
||||||
var subEv helix.EventSubChannelSubscriptionMessageEvent
|
var subEv helix.EventSubChannelSubscriptionMessageEvent
|
||||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &subEv)
|
err := json.Unmarshal(ev.Event, &subEv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bot.logger.Debug("error parsing sub event", zap.Error(err))
|
bot.logger.Debug("error parsing sub event", zap.Error(err))
|
||||||
return
|
return
|
||||||
|
@ -372,7 +371,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
||||||
}
|
}
|
||||||
// Parse as gift event
|
// Parse as gift event
|
||||||
var giftEv helix.EventSubChannelSubscriptionGiftEvent
|
var giftEv helix.EventSubChannelSubscriptionGiftEvent
|
||||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &giftEv)
|
err := json.Unmarshal(ev.Event, &giftEv)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bot.logger.Debug("error parsing raid event", zap.Error(err))
|
bot.logger.Debug("error parsing raid event", zap.Error(err))
|
||||||
return
|
return
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
|
||||||
irc "github.com/gempir/go-twitch-irc/v3"
|
irc "github.com/gempir/go-twitch-irc/v3"
|
||||||
jsoniter "github.com/json-iterator/go"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const BotTimersKey = "twitch/bot-modules/timers/config"
|
const BotTimersKey = "twitch/bot-modules/timers/config"
|
||||||
|
@ -58,7 +57,7 @@ func SetupTimers(bot *Bot) *BotTimerModule {
|
||||||
|
|
||||||
go bot.api.db.Subscribe(func(key, value string) {
|
go bot.api.db.Subscribe(func(key, value string) {
|
||||||
if key == BotTimersKey {
|
if key == BotTimersKey {
|
||||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &mod.Config)
|
err := json.UnmarshalFromString(value, &mod.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
bot.logger.Debug("error reloading timer config", zap.Error(err))
|
bot.logger.Debug("error reloading timer config", zap.Error(err))
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue