2017-03-04 12:35:53 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-03-08 21:10:08 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2017-03-04 12:35:53 +01:00
|
|
|
|
|
|
|
"github.com/op/go-logging"
|
2017-03-11 12:18:54 +01:00
|
|
|
"gopkg.in/redis.v5"
|
2017-03-08 21:10:08 +01:00
|
|
|
|
|
|
|
"github.com/gempir/gempbotgo/twitch"
|
2017-03-08 19:39:56 +01:00
|
|
|
"github.com/gempir/gempbotgo/command"
|
2017-03-08 20:08:44 +01:00
|
|
|
"github.com/gempir/gempbotgo/filelog"
|
2017-03-08 21:38:01 +01:00
|
|
|
"github.com/gempir/gempbotgo/api"
|
2017-03-10 18:09:13 +01:00
|
|
|
"github.com/gempir/gempbotgo/combo"
|
2017-03-04 12:35:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-03-10 18:09:13 +01:00
|
|
|
cfg config
|
|
|
|
logger logging.Logger
|
2017-03-04 12:35:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
2017-03-05 16:26:24 +01:00
|
|
|
IrcAddress string `json:"irc_address"`
|
|
|
|
IrcUser string `json:"irc_user"`
|
|
|
|
IrcToken string `json:"irc_token"`
|
2017-03-08 20:42:35 +01:00
|
|
|
Admin string `json:"admin"`
|
2017-03-08 20:08:44 +01:00
|
|
|
LogPath string `json:"log_path"`
|
2017-03-05 16:26:24 +01:00
|
|
|
APIPort string `json:"api_port"`
|
|
|
|
RedisAddress string `json:"redis_address"`
|
|
|
|
RedisPassword string `json:"redis_password"`
|
2017-03-11 12:18:54 +01:00
|
|
|
RedisDatabase int `json:"redis_database"`
|
2017-03-04 12:35:53 +01:00
|
|
|
}
|
|
|
|
|
2017-03-11 20:27:48 +01:00
|
|
|
var (
|
|
|
|
fileLogger filelog.Logger
|
|
|
|
cmdHandler command.Handler
|
|
|
|
comboHandler combo.Handler
|
|
|
|
)
|
|
|
|
|
2017-03-04 12:35:53 +01:00
|
|
|
func main() {
|
2017-03-08 20:42:35 +01:00
|
|
|
startTime := time.Now()
|
2017-03-10 18:09:13 +01:00
|
|
|
logger = initLogger()
|
2017-03-04 12:35:53 +01:00
|
|
|
var err error
|
|
|
|
cfg, err = readConfig("config.json")
|
|
|
|
if err != nil {
|
2017-03-10 18:09:13 +01:00
|
|
|
logger.Fatal(err)
|
2017-03-04 12:35:53 +01:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:26:24 +01:00
|
|
|
rClient := redis.NewClient(&redis.Options{
|
|
|
|
Addr: cfg.RedisAddress,
|
|
|
|
Password: cfg.RedisPassword,
|
|
|
|
DB: cfg.RedisDatabase,
|
|
|
|
})
|
|
|
|
|
2017-03-10 18:09:13 +01:00
|
|
|
apiServer := api.NewServer(cfg.APIPort, cfg.LogPath)
|
2017-03-08 21:38:01 +01:00
|
|
|
go apiServer.Init()
|
|
|
|
|
2017-03-11 12:18:54 +01:00
|
|
|
bot := twitch.NewBot(cfg.IrcAddress, cfg.IrcUser, cfg.IrcToken, *rClient, logger)
|
2017-03-11 13:49:12 +01:00
|
|
|
go func() {
|
|
|
|
err := bot.CreatePersistentConnection()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
2017-03-08 19:39:56 +01:00
|
|
|
|
2017-03-11 20:27:48 +01:00
|
|
|
fileLogger = filelog.NewFileLogger(cfg.LogPath)
|
|
|
|
cmdHandler = command.NewHandler(cfg.Admin, &bot, startTime, logger)
|
|
|
|
comboHandler = combo.NewHandler()
|
2017-03-08 20:08:44 +01:00
|
|
|
|
2017-03-08 19:39:56 +01:00
|
|
|
for msg := range bot.Messages {
|
2017-03-08 20:08:44 +01:00
|
|
|
|
2017-03-11 20:27:48 +01:00
|
|
|
if msg.Type == twitch.PRIVMSG || msg.Type == twitch.CLEARCHAT {
|
|
|
|
go func() {
|
|
|
|
err := fileLogger.LogMessageForUser(msg)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
2017-03-11 12:46:38 +01:00
|
|
|
|
|
|
|
go func() {
|
2017-03-11 20:27:48 +01:00
|
|
|
err := fileLogger.LogMessageForChannel(msg)
|
2017-03-11 12:46:38 +01:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
2017-03-08 20:08:44 +01:00
|
|
|
|
2017-03-11 20:27:48 +01:00
|
|
|
go comboHandler.HandleMessage(msg)
|
|
|
|
|
|
|
|
|
|
|
|
if strings.HasPrefix(msg.Text, "!") {
|
|
|
|
go func() {
|
|
|
|
err := cmdHandler.HandleCommand(msg)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 19:39:56 +01:00
|
|
|
}
|
2017-03-04 12:35:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func initLogger() logging.Logger {
|
|
|
|
var logger *logging.Logger
|
|
|
|
logger = logging.MustGetLogger("gempbotgo")
|
|
|
|
backend := logging.NewLogBackend(os.Stdout, "", 0)
|
|
|
|
|
|
|
|
format := logging.MustStringFormatter(
|
2017-03-11 21:45:52 +01:00
|
|
|
`%{color}%{level} %{shortfile}%{color:reset} %{message}`,
|
2017-03-04 12:35:53 +01:00
|
|
|
)
|
|
|
|
logging.SetFormatter(format)
|
|
|
|
backendLeveled := logging.AddModuleLevel(backend)
|
|
|
|
logging.SetBackend(backendLeveled)
|
|
|
|
return *logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func readConfig(path string) (config, error) {
|
|
|
|
file, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
return unmarshalConfig(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalConfig(file []byte) (config, error) {
|
|
|
|
err := json.Unmarshal(file, &cfg)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
return cfg, nil
|
|
|
|
}
|