-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
161 lines (135 loc) · 4.33 KB
/
main.go
File metadata and controls
161 lines (135 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"hashbot/backend"
"hashbot/command"
"hashbot/config"
"hashbot/database"
"hashbot/hashbot"
"hashbot/seventvapi"
"hashbot/twitchapi"
"os"
"sort"
"time"
flag "github.com/spf13/pflag"
"github.com/gempir/go-twitch-irc/v4"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func main() {
// parse command-line arguments
cfgPath := flag.String("cfg", "config.hb", "path to config file")
debug := flag.Bool("debug", false, "sets log level to debug")
cmdListPrefix := flag.String("cmd-list-prefix", "\\", "sets the bot's prefix used in the command list generation")
generateCmdList := flag.String("cmd-list", "", "ignores all other args and generates command list json to the specified path")
flag.Parse()
// set up logging
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(
zerolog.ConsoleWriter{
Out: os.Stderr,
TimeFormat: time.DateTime,
},
)
if *debug {
log.Debug().Msg("debug mode on")
zerolog.SetGlobalLevel(zerolog.DebugLevel)
// Enable caller info
log.Logger = log.With().Caller().Logger()
}
// generate command list json
if *generateCmdList != "" {
log.Info().Str("path", *generateCmdList).Msg("generating command list json")
var (
commandListData []byte
err error
)
// show commands on the list with the prefix
for i := range command.Commands {
if !command.Commands[i].NoPrefix {
command.Commands[i].Name = *cmdListPrefix + command.Commands[i].Name
}
}
sort.Sort(command.SortByPrefixAndName(command.Commands))
commandListData, err = json.MarshalIndent(command.Commands, "", " ")
if err != nil {
log.Fatal().Err(err).Msg("failed to generate command list json")
}
if err := os.WriteFile(*generateCmdList, commandListData, 0644); err != nil {
log.Fatal().Str("path", *generateCmdList).Err(err).Msg("failed to write command list json")
}
log.Info().Str("path", *generateCmdList).Msg("command list json generated successfully")
os.Exit(0)
}
_, err := os.Stat(*cfgPath)
if os.IsNotExist(err) {
log.Warn().Str("path", *cfgPath).Msg("config file does not exist, creating from template")
var file *os.File
file, err = os.Create(*cfgPath)
if err != nil {
log.Fatal().Str("path", *cfgPath).Err(err).Msg("failed to create temaplate")
}
defer file.Close()
var templateJSONBytes []byte
templateJSONBytes, err = config.ConfigTemplateJSON()
if err != nil {
log.Fatal().Err(err).Msg("failed to generate template")
}
_, err = file.Write(templateJSONBytes)
if err != nil {
log.Fatal().Str("path", *cfgPath).Err(err).Msg("failed to create template file")
}
log.Info().Str("path", *cfgPath).Msgf("template created successfully, please edit the file and run the bot again")
os.Exit(0)
}
if err != nil {
log.Fatal().Err(err).Msg("failed to stat config file")
}
var (
cfg *config.Config
data []byte
)
data, err = os.ReadFile(*cfgPath)
if err != nil {
log.Fatal().Err(err).Msg("failed to read config file")
}
cfg, err = config.LoadConfig(data)
if err != nil {
log.Fatal().Err(err).Msg("failed to load config file")
}
reader := new(bytes.Buffer)
reader.Write(data)
db, err := database.InitDB(cfg.DBConfig.Driver, cfg.DBConfig.DataSourceName, reader)
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize database")
}
defer db.Close()
for {
var (
mb *hashbot.HashBot
invalidatedTokenCh = make(chan bool, 1)
)
appCtx, cancelFn := context.WithCancel(context.Background())
backend.RunServer(appCtx, cfg)
hashbot.RunTokenValidator(appCtx, cfg, invalidatedTokenCh, twitchapi.RefreshTwitchToken, twitchapi.ValidateToken)
hashbot.RunSevenTVEditorReqAccepter(appCtx, cfg, invalidatedTokenCh, twitchapi.GetUserByName, seventvapi.GetUserByConnection, seventvapi.AcceptEditorRequest)
mb, err := hashbot.NewHashBot(cfg, db, invalidatedTokenCh)
if err != nil {
log.Fatal().Err(err).Msg("failed to initialize hashbot")
}
connectWithBreaker := hashbot.Breaker(mb.ReconnectClient, 5)
err = connectWithBreaker()
if errors.Is(err, hashbot.CircuitBreakerErr) {
time.Sleep(time.Second / 4)
continue
} else if errors.Is(err, twitch.ErrClientDisconnected) {
log.Warn().Msg("client disconnected")
} else if err != nil {
log.Error().Err(err).Msg("failed to connect to Twitch")
}
cancelFn()
}
}