mirror of
https://git.sr.ht/~ashkeel/strimertul
synced 2024-09-18 01:50:50 +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 (
|
||||
"os"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
|
@ -19,7 +18,7 @@ func cliImport(ctx *cli.Context) error {
|
|||
inStream = file
|
||||
}
|
||||
var entries map[string]string
|
||||
err := jsoniter.ConfigFastest.NewDecoder(inStream).Decode(&entries)
|
||||
err := json.NewDecoder(inStream).Decode(&entries)
|
||||
if err != nil {
|
||||
return fatalError(err, "could not decode import file")
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"path/filepath"
|
||||
|
||||
"github.com/cockroachdb/pebble"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/labstack/gommon/log"
|
||||
kv "github.com/strimertul/kilovolt/v9"
|
||||
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 {
|
||||
in := make(map[string]string)
|
||||
err := jsoniter.ConfigFastest.NewDecoder(file).Decode(&in)
|
||||
err := json.NewDecoder(file).Decode(&in)
|
||||
if err != nil {
|
||||
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() {
|
||||
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"
|
||||
"time"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/wailsapp/wails/v2"
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
|
@ -23,6 +25,8 @@ import (
|
|||
_ "net/http/pprof"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigFastest
|
||||
|
||||
const databaseDefaultDriver = "pebble"
|
||||
|
||||
var appVersion = "v0.0.0-UNKNOWN"
|
||||
|
|
|
@ -20,6 +20,8 @@ import (
|
|||
"github.com/strimertul/strimertul/modules"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigFastest
|
||||
|
||||
type Server struct {
|
||||
Config ServerConfig
|
||||
db *database.DBModule
|
||||
|
@ -136,7 +138,7 @@ func (s *Server) Listen() error {
|
|||
if key == ServerConfigKey {
|
||||
oldBind := s.Config.Bind
|
||||
oldPassword := s.Config.KVPassword
|
||||
err := jsoniter.ConfigFastest.Unmarshal([]byte(value), &s.Config)
|
||||
err := json.Unmarshal([]byte(value), &s.Config)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to unmarshal config", zap.Error(err))
|
||||
return
|
||||
|
|
|
@ -15,6 +15,8 @@ import (
|
|||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigFastest
|
||||
|
||||
var (
|
||||
ErrRedeemInCooldown = errors.New("redeem is on cooldown")
|
||||
ErrGoalNotFound = errors.New("goal not found")
|
||||
|
@ -83,7 +85,7 @@ func Register(manager *modules.Manager) error {
|
|||
loyalty.points = make(map[string]PointsEntry)
|
||||
for k, v := range points {
|
||||
var entry PointsEntry
|
||||
err := jsoniter.ConfigFastest.UnmarshalFromString(v, &entry)
|
||||
err := json.UnmarshalFromString(v, &entry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -132,7 +134,7 @@ func (m *Manager) Status() modules.ModuleStatus {
|
|||
}
|
||||
|
||||
func (m *Manager) Close() error {
|
||||
//TODO Stop subscriptions?
|
||||
// TODO Stop subscriptions?
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -145,35 +147,35 @@ func (m *Manager) update(key, value string) {
|
|||
err = func() error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return jsoniter.ConfigFastest.UnmarshalFromString(value, &m.config)
|
||||
return json.UnmarshalFromString(value, &m.config)
|
||||
}()
|
||||
case GoalsKey:
|
||||
err = func() error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return jsoniter.ConfigFastest.UnmarshalFromString(value, &m.goals)
|
||||
return json.UnmarshalFromString(value, &m.goals)
|
||||
}()
|
||||
case RewardsKey:
|
||||
err = func() error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return jsoniter.ConfigFastest.UnmarshalFromString(value, &m.rewards)
|
||||
return json.UnmarshalFromString(value, &m.rewards)
|
||||
}()
|
||||
case QueueKey:
|
||||
err = func() error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
return jsoniter.ConfigFastest.UnmarshalFromString(value, &m.queue)
|
||||
return json.UnmarshalFromString(value, &m.queue)
|
||||
}()
|
||||
case CreateRedeemRPC:
|
||||
var redeem Redeem
|
||||
err = jsoniter.ConfigFastest.UnmarshalFromString(value, &redeem)
|
||||
err = json.UnmarshalFromString(value, &redeem)
|
||||
if err == nil {
|
||||
err = m.AddRedeem(redeem)
|
||||
}
|
||||
case RemoveRedeemRPC:
|
||||
var redeem Redeem
|
||||
err = jsoniter.ConfigFastest.UnmarshalFromString(value, &redeem)
|
||||
err = json.UnmarshalFromString(value, &redeem)
|
||||
if err == nil {
|
||||
err = m.RemoveRedeem(redeem)
|
||||
}
|
||||
|
@ -183,7 +185,7 @@ func (m *Manager) update(key, value string) {
|
|||
// User point changed
|
||||
case strings.HasPrefix(key, PointsPrefix):
|
||||
var entry PointsEntry
|
||||
err = jsoniter.ConfigFastest.UnmarshalFromString(value, &entry)
|
||||
err = json.UnmarshalFromString(value, &entry)
|
||||
user := key[len(PointsPrefix):]
|
||||
func() {
|
||||
m.mu.Lock()
|
||||
|
@ -205,7 +207,7 @@ func (m *Manager) handleRemote(key, value string) {
|
|||
case KVExLoyaltyRedeem:
|
||||
// Parse request
|
||||
var redeemRequest ExLoyaltyRedeem
|
||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &redeemRequest)
|
||||
err := json.UnmarshalFromString(value, &redeemRequest)
|
||||
if err != nil {
|
||||
m.logger.Warn("error decoding redeem request", zap.Error(err))
|
||||
break
|
||||
|
@ -229,7 +231,7 @@ func (m *Manager) handleRemote(key, value string) {
|
|||
case KVExLoyaltyContribute:
|
||||
// Parse request
|
||||
var contributeRequest ExLoyaltyContribute
|
||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &contributeRequest)
|
||||
err := json.UnmarshalFromString(value, &contributeRequest)
|
||||
if err != nil {
|
||||
m.logger.Warn("error decoding contribution request", zap.Error(err))
|
||||
break
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
|
||||
"github.com/Masterminds/sprig/v3"
|
||||
irc "github.com/gempir/go-twitch-irc/v3"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
type Bot struct {
|
||||
|
@ -167,7 +166,7 @@ func (b *Bot) updateCommands(key, value string) {
|
|||
err := func() error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
return jsoniter.ConfigFastest.UnmarshalFromString(value, &b.customCommands)
|
||||
return json.UnmarshalFromString(value, &b.customCommands)
|
||||
}()
|
||||
if err != nil {
|
||||
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"
|
||||
)
|
||||
|
||||
var json = jsoniter.ConfigFastest
|
||||
|
||||
type Client struct {
|
||||
Config Config
|
||||
Bot *Bot
|
||||
|
@ -97,7 +99,7 @@ func Register(manager *modules.Manager) error {
|
|||
go db.Subscribe(func(key, value string) {
|
||||
switch key {
|
||||
case ConfigKey:
|
||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &config)
|
||||
err := json.UnmarshalFromString(value, &config)
|
||||
if err != nil {
|
||||
logger.Error("failed to unmarshal config", zap.Error(err))
|
||||
return
|
||||
|
@ -111,7 +113,7 @@ func Register(manager *modules.Manager) error {
|
|||
logger.Info("reloaded/updated Twitch API")
|
||||
case BotConfigKey:
|
||||
var twitchBotConfig BotConfig
|
||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &twitchBotConfig)
|
||||
err := json.UnmarshalFromString(value, &twitchBotConfig)
|
||||
if err != nil {
|
||||
logger.Error("failed to unmarshal config", zap.Error(err))
|
||||
return
|
||||
|
@ -151,7 +153,7 @@ func (c *Client) runStatusPoll() {
|
|||
// Check if streamer is online, if possible
|
||||
func() {
|
||||
status, err := c.API.GetStreams(&helix.StreamsParams{
|
||||
UserLogins: []string{c.Bot.config.Channel}, //TODO Replace with something non bot dependant
|
||||
UserLogins: []string{c.Bot.config.Channel}, // TODO Replace with something non bot dependant
|
||||
})
|
||||
if err != nil {
|
||||
c.logger.Error("Error checking stream status", zap.Error(err))
|
||||
|
|
|
@ -20,9 +20,11 @@ type BotConfig struct {
|
|||
ChatHistory int `json:"chat_history"`
|
||||
}
|
||||
|
||||
const ChatEventKey = "twitch/ev/chat-message"
|
||||
const ChatHistoryKey = "twitch/chat-history"
|
||||
const ChatActivityKey = "twitch/chat-activity"
|
||||
const (
|
||||
ChatEventKey = "twitch/ev/chat-message"
|
||||
ChatHistoryKey = "twitch/chat-history"
|
||||
ChatActivityKey = "twitch/chat-activity"
|
||||
)
|
||||
|
||||
type BotCustomCommand struct {
|
||||
Description string `json:"description"`
|
||||
|
|
|
@ -2,7 +2,6 @@ package twitch
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"text/template"
|
||||
|
@ -20,7 +19,7 @@ const BotAlertsKey = "twitch/bot-modules/alerts/config"
|
|||
type eventSubNotification struct {
|
||||
Subscription helix.EventSubSubscription `json:"subscription"`
|
||||
Challenge string `json:"challenge"`
|
||||
Event json.RawMessage `json:"event"`
|
||||
Event jsoniter.RawMessage `json:"event"`
|
||||
}
|
||||
|
||||
type BotAlertsConfig struct {
|
||||
|
@ -114,7 +113,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
|||
|
||||
go bot.api.db.Subscribe(func(key, value string) {
|
||||
if key == BotAlertsKey {
|
||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &mod.Config)
|
||||
err := json.UnmarshalFromString(value, &mod.Config)
|
||||
if err != nil {
|
||||
bot.logger.Debug("error reloading timer config", zap.Error(err))
|
||||
} else {
|
||||
|
@ -238,7 +237,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
|||
go bot.api.db.Subscribe(func(key, value string) {
|
||||
if key == "stulbe/ev/webhook" {
|
||||
var ev eventSubNotification
|
||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &ev)
|
||||
err := json.UnmarshalFromString(value, &ev)
|
||||
if err != nil {
|
||||
bot.logger.Debug("error parsing webhook payload", zap.Error(err))
|
||||
return
|
||||
|
@ -251,7 +250,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
|||
}
|
||||
// Parse as follow event
|
||||
var followEv helix.EventSubChannelFollowEvent
|
||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &followEv)
|
||||
err := json.Unmarshal(ev.Event, &followEv)
|
||||
if err != nil {
|
||||
bot.logger.Debug("error parsing follow event", zap.Error(err))
|
||||
return
|
||||
|
@ -272,7 +271,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
|||
}
|
||||
// Parse as raid event
|
||||
var raidEv helix.EventSubChannelRaidEvent
|
||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &raidEv)
|
||||
err := json.Unmarshal(ev.Event, &raidEv)
|
||||
if err != nil {
|
||||
bot.logger.Debug("error parsing raid event", zap.Error(err))
|
||||
return
|
||||
|
@ -309,7 +308,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
|||
}
|
||||
// Parse as cheer event
|
||||
var cheerEv helix.EventSubChannelCheerEvent
|
||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &cheerEv)
|
||||
err := json.Unmarshal(ev.Event, &cheerEv)
|
||||
if err != nil {
|
||||
bot.logger.Debug("error parsing cheer event", zap.Error(err))
|
||||
return
|
||||
|
@ -346,7 +345,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
|||
}
|
||||
// Parse as subscription event
|
||||
var subEv helix.EventSubChannelSubscribeEvent
|
||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &subEv)
|
||||
err := json.Unmarshal(ev.Event, &subEv)
|
||||
if err != nil {
|
||||
bot.logger.Debug("error parsing sub event", zap.Error(err))
|
||||
return
|
||||
|
@ -359,7 +358,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
|||
}
|
||||
// Parse as subscription event
|
||||
var subEv helix.EventSubChannelSubscriptionMessageEvent
|
||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &subEv)
|
||||
err := json.Unmarshal(ev.Event, &subEv)
|
||||
if err != nil {
|
||||
bot.logger.Debug("error parsing sub event", zap.Error(err))
|
||||
return
|
||||
|
@ -372,7 +371,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
|||
}
|
||||
// Parse as gift event
|
||||
var giftEv helix.EventSubChannelSubscriptionGiftEvent
|
||||
err := jsoniter.ConfigFastest.Unmarshal(ev.Event, &giftEv)
|
||||
err := json.Unmarshal(ev.Event, &giftEv)
|
||||
if err != nil {
|
||||
bot.logger.Debug("error parsing raid event", zap.Error(err))
|
||||
return
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
"go.uber.org/zap"
|
||||
|
||||
irc "github.com/gempir/go-twitch-irc/v3"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
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) {
|
||||
if key == BotTimersKey {
|
||||
err := jsoniter.ConfigFastest.UnmarshalFromString(value, &mod.Config)
|
||||
err := json.UnmarshalFromString(value, &mod.Config)
|
||||
if err != nil {
|
||||
bot.logger.Debug("error reloading timer config", zap.Error(err))
|
||||
} else {
|
||||
|
|
Loading…
Reference in a new issue