2017-03-04 12:35:53 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-03-08 21:10:08 +01:00
|
|
|
"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
|
|
|
|
2017-04-15 09:46:39 +02:00
|
|
|
"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"
|
2017-04-15 09:46:39 +02:00
|
|
|
"github.com/gempir/gempbotgo/humanize"
|
2017-04-14 17:43:12 +02:00
|
|
|
"github.com/gempir/go-twitch-irc"
|
|
|
|
"strings"
|
2017-03-04 12:35:53 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-03-12 10:43:51 +01:00
|
|
|
cfg sysConfig
|
2017-03-10 18:09:13 +01:00
|
|
|
logger logging.Logger
|
2017-03-04 12:35:53 +01:00
|
|
|
)
|
|
|
|
|
2017-03-12 10:43:51 +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 (
|
2017-04-03 22:17:41 +02:00
|
|
|
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()
|
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-04-14 17:43:12 +02:00
|
|
|
apiServer := api.NewServer(cfg.APIPort, cfg.LogPath)
|
|
|
|
go apiServer.Init()
|
|
|
|
|
2017-03-05 16:26:24 +01:00
|
|
|
rClient := redis.NewClient(&redis.Options{
|
|
|
|
Addr: cfg.RedisAddress,
|
|
|
|
Password: cfg.RedisPassword,
|
|
|
|
DB: cfg.RedisDatabase,
|
|
|
|
})
|
|
|
|
|
2017-04-14 17:43:12 +02:00
|
|
|
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
|
|
|
|
2017-04-14 17:43:12 +02: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
|
|
|
|
2017-04-14 17:43:12 +02:00
|
|
|
if message.Type == twitch.PRIVMSG || message.Type == twitch.CLEARCHAT {
|
2017-03-11 20:27:48 +01:00
|
|
|
go func() {
|
2017-04-14 17:43:12 +02:00
|
|
|
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() {
|
2017-04-14 17:43:12 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-04-14 17:43:12 +02:00
|
|
|
if user.Username == cfg.Admin && strings.HasPrefix(message.Text, "!status") {
|
2017-04-15 09:46:39 +02:00
|
|
|
uptime := humanize.TimeSince(startTime)
|
2017-04-14 17:43:12 +02:00
|
|
|
twitchClient.Say(channel, cfg.Admin+", uptime: "+uptime)
|
2017-03-11 20:27:48 +01:00
|
|
|
}
|
|
|
|
}
|
2017-04-14 17:43:12 +02: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
|
|
|
|
}
|
|
|
|
|
2017-03-12 10:43:51 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-03-12 10:43:51 +01:00
|
|
|
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
|
|
|
}
|