justlog/main.go

133 lines
3.1 KiB
Go
Raw Normal View History

2017-03-04 12:35:53 +01:00
package main
import (
"encoding/json"
"io/ioutil"
"os"
"time"
2017-03-04 12:35:53 +01:00
"github.com/op/go-logging"
"gopkg.in/redis.v5"
"fmt"
2017-03-08 21:38:01 +01:00
"github.com/gempir/gempbotgo/api"
2017-03-11 21:51:08 +01:00
"github.com/gempir/gempbotgo/filelog"
"github.com/gempir/gempbotgo/humanize"
"github.com/gempir/go-twitch-irc"
"strings"
2017-03-04 12:35:53 +01:00
)
var (
cfg sysConfig
logger logging.Logger
2017-03-04 12:35:53 +01:00
)
type sysConfig struct {
2017-03-11 21:51:08 +01:00
IrcAddress string `json:"irc_address"`
IrcUser string `json:"irc_user"`
IrcToken string `json:"irc_token"`
Admin string `json:"admin"`
LogPath string `json:"log_path"`
APIPort string `json:"api_port"`
RedisAddress string `json:"redis_address"`
RedisPassword string `json:"redis_password"`
RedisDatabase int `json:"redis_database"`
2017-03-19 13:01:49 +01:00
CleverBotUser string `json:"cleverbot_user"`
CleverBotKey string `json:"cleverbot_key"`
2017-03-04 12:35:53 +01:00
}
2017-03-11 20:27:48 +01:00
var (
fileLogger filelog.Logger
2017-03-11 20:27:48 +01:00
)
2017-03-04 12:35:53 +01:00
func main() {
2017-03-08 20:42:35 +01:00
startTime := time.Now()
logger = initLogger()
2017-03-04 12:35:53 +01:00
var err error
cfg, err = readConfig("config.json")
if err != nil {
logger.Fatal(err)
2017-03-04 12:35:53 +01:00
}
apiServer := api.NewServer(cfg.APIPort, cfg.LogPath)
go apiServer.Init()
rClient := redis.NewClient(&redis.Options{
Addr: cfg.RedisAddress,
Password: cfg.RedisPassword,
DB: cfg.RedisDatabase,
})
twitchClient := twitch.NewClient(cfg.IrcUser, cfg.IrcToken)
twitchClient.SetIrcAddress(cfg.IrcAddress)
2017-03-08 19:39:56 +01:00
2017-03-11 20:27:48 +01:00
fileLogger = filelog.NewFileLogger(cfg.LogPath)
2017-03-08 20:08:44 +01:00
val, _ := rClient.HGetAll("channels").Result()
for channelStr := range val {
fmt.Println("Joining " + channelStr)
go twitchClient.Join(strings.TrimPrefix(channelStr, "#"))
}
twitchClient.OnNewMessage(func(channel string, user twitch.User, message twitch.Message) {
2017-03-08 20:08:44 +01:00
if message.Type == twitch.PRIVMSG || message.Type == twitch.CLEARCHAT {
2017-03-11 20:27:48 +01:00
go func() {
err := fileLogger.LogMessageForUser(channel, user, message)
2017-03-11 20:27:48 +01:00
if err != nil {
logger.Error(err.Error())
}
}()
2017-03-11 12:46:38 +01:00
go func() {
err := fileLogger.LogMessageForChannel(channel, user, message)
2017-03-11 12:46:38 +01:00
if err != nil {
logger.Error(err.Error())
}
}()
2017-03-08 20:08:44 +01:00
2017-04-30 18:54:34 +02:00
if strings.HasPrefix(message.Text, "!pingall") {
uptime := humanize.TimeSince(startTime)
2017-05-06 11:22:40 +02:00
twitchClient.Say(channel, "uptime: "+uptime)
2017-04-30 18:54:34 +02:00
}
if user.Username == cfg.Admin && strings.HasPrefix(message.Text, "!status") {
uptime := humanize.TimeSince(startTime)
twitchClient.Say(channel, cfg.Admin+", uptime: "+uptime)
2017-03-11 20:27:48 +01:00
}
}
})
fmt.Println(twitchClient.Connect())
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) (sysConfig, error) {
2017-03-04 12:35:53 +01:00
file, err := ioutil.ReadFile(path)
if err != nil {
return cfg, err
}
return unmarshalConfig(file)
}
func unmarshalConfig(file []byte) (sysConfig, error) {
2017-03-04 12:35:53 +01:00
err := json.Unmarshal(file, &cfg)
if err != nil {
return cfg, err
}
return cfg, nil
2017-03-11 21:51:08 +01:00
}