2022-08-06 23:45:02 +02:00
|
|
|
package main
|
|
|
|
|
2022-08-07 01:34:31 +02:00
|
|
|
import (
|
2022-08-08 23:13:33 +02:00
|
|
|
"context"
|
|
|
|
"database/sql"
|
2023-12-03 16:35:34 +01:00
|
|
|
"fmt"
|
2022-08-08 23:13:33 +02:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2023-08-08 01:00:14 +02:00
|
|
|
"github.com/gempir/go-twitch-irc/v4"
|
2022-08-25 04:42:37 +02:00
|
|
|
"github.com/jakecoffman/cron"
|
2022-08-08 23:13:33 +02:00
|
|
|
"github.com/joho/godotenv"
|
|
|
|
_ "github.com/lib/pq"
|
2023-08-08 02:37:37 +02:00
|
|
|
"github.com/lyx0/nourybot/internal/common"
|
2023-09-07 22:34:53 +02:00
|
|
|
"github.com/lyx0/nourybot/internal/data"
|
2023-10-07 17:00:14 +02:00
|
|
|
"github.com/nicklaw5/helix/v2"
|
2023-08-04 20:57:40 +02:00
|
|
|
|
2022-08-07 03:09:09 +02:00
|
|
|
"go.uber.org/zap"
|
2022-08-07 01:34:31 +02:00
|
|
|
)
|
|
|
|
|
2022-08-20 15:14:05 +02:00
|
|
|
type config struct {
|
2022-08-29 21:24:30 +02:00
|
|
|
twitchUsername string
|
|
|
|
twitchOauth string
|
|
|
|
twitchClientId string
|
|
|
|
twitchClientSecret string
|
2023-10-10 16:49:00 +02:00
|
|
|
twitchID string
|
2023-12-17 17:17:55 +01:00
|
|
|
wolframAlphaAppID string
|
2022-08-29 21:24:30 +02:00
|
|
|
commandPrefix string
|
2023-12-07 17:49:32 +01:00
|
|
|
env string
|
2022-08-29 21:24:30 +02:00
|
|
|
db struct {
|
2022-08-20 15:14:05 +02:00
|
|
|
dsn string
|
|
|
|
maxOpenConns int
|
|
|
|
maxIdleConns int
|
|
|
|
maxIdleTime string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 20:57:40 +02:00
|
|
|
type application struct {
|
2022-08-20 15:14:05 +02:00
|
|
|
TwitchClient *twitch.Client
|
2022-08-29 21:24:30 +02:00
|
|
|
HelixClient *helix.Client
|
2023-08-04 20:57:40 +02:00
|
|
|
Log *zap.SugaredLogger
|
2022-08-20 15:14:05 +02:00
|
|
|
Db *sql.DB
|
2023-09-07 22:34:53 +02:00
|
|
|
Models data.Models
|
|
|
|
Scheduler *cron.Cron
|
2023-12-08 19:23:31 +01:00
|
|
|
Environment string
|
2023-12-17 17:17:55 +01:00
|
|
|
Config config
|
2023-08-04 20:57:40 +02:00
|
|
|
// Rdb *redis.Client
|
2022-08-20 15:14:05 +02:00
|
|
|
}
|
|
|
|
|
2022-08-06 23:45:02 +02:00
|
|
|
func main() {
|
2022-08-08 23:13:33 +02:00
|
|
|
var cfg config
|
2022-08-08 23:58:25 +02:00
|
|
|
// Initialize a new sugared logger that we'll pass on
|
|
|
|
// down through the application.
|
2023-03-04 05:34:46 +01:00
|
|
|
logger := zap.NewExample()
|
2023-08-08 02:37:37 +02:00
|
|
|
defer func() {
|
|
|
|
if err := logger.Sync(); err != nil {
|
|
|
|
logger.Sugar().Fatalw("error syncing logger",
|
|
|
|
"error", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}()
|
2023-03-04 05:34:46 +01:00
|
|
|
sugar := logger.Sugar()
|
2022-08-08 23:58:25 +02:00
|
|
|
|
2023-12-13 12:53:04 +01:00
|
|
|
common.StartTime()
|
|
|
|
|
2022-08-08 23:13:33 +02:00
|
|
|
err := godotenv.Load()
|
|
|
|
if err != nil {
|
2023-08-04 20:57:40 +02:00
|
|
|
sugar.Fatal("Error loading .env")
|
2022-08-08 23:13:33 +02:00
|
|
|
}
|
2022-08-07 03:09:09 +02:00
|
|
|
|
2022-08-30 20:27:20 +02:00
|
|
|
// Twitch config variables
|
2022-08-08 23:13:33 +02:00
|
|
|
cfg.twitchUsername = os.Getenv("TWITCH_USERNAME")
|
|
|
|
cfg.twitchOauth = os.Getenv("TWITCH_OAUTH")
|
2022-08-29 21:24:30 +02:00
|
|
|
cfg.twitchClientId = os.Getenv("TWITCH_CLIENT_ID")
|
|
|
|
cfg.twitchClientSecret = os.Getenv("TWITCH_CLIENT_SECRET")
|
2023-12-17 17:17:55 +01:00
|
|
|
cfg.wolframAlphaAppID = os.Getenv("WOLFRAMALPHA_APP_ID")
|
2023-10-10 16:49:00 +02:00
|
|
|
cfg.twitchID = os.Getenv("TWITCH_ID")
|
2023-12-07 17:49:32 +01:00
|
|
|
cfg.env = os.Getenv("ENV")
|
2022-08-08 23:58:25 +02:00
|
|
|
tc := twitch.NewClient(cfg.twitchUsername, cfg.twitchOauth)
|
|
|
|
|
2023-12-07 17:49:32 +01:00
|
|
|
switch cfg.env {
|
2023-03-03 22:16:32 +01:00
|
|
|
case "dev":
|
2023-12-03 23:56:25 +01:00
|
|
|
cfg.db.dsn = os.Getenv("DEV_DSN")
|
2023-12-17 22:53:23 +01:00
|
|
|
cfg.commandPrefix = "!!"
|
2023-03-03 22:16:32 +01:00
|
|
|
case "prod":
|
2023-12-03 23:56:25 +01:00
|
|
|
cfg.db.dsn = os.Getenv("PROD_DSN")
|
2023-12-17 22:53:23 +01:00
|
|
|
cfg.commandPrefix = "()"
|
2023-03-03 22:16:32 +01:00
|
|
|
}
|
2022-08-30 20:27:20 +02:00
|
|
|
// Database config variables
|
2022-08-08 23:13:33 +02:00
|
|
|
cfg.db.maxOpenConns = 25
|
|
|
|
cfg.db.maxIdleConns = 25
|
|
|
|
cfg.db.maxIdleTime = "15m"
|
|
|
|
|
2022-08-30 20:27:20 +02:00
|
|
|
// Initialize a new Helix Client, request a new AppAccessToken
|
|
|
|
// and set the token on the client.
|
2022-08-29 21:24:30 +02:00
|
|
|
helixClient, err := helix.NewClient(&helix.Options{
|
2022-08-30 20:27:20 +02:00
|
|
|
ClientID: cfg.twitchClientId,
|
|
|
|
ClientSecret: cfg.twitchClientSecret,
|
2022-08-29 21:24:30 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-08-30 20:27:20 +02:00
|
|
|
sugar.Fatalw("Error creating new helix client",
|
|
|
|
"helixClient", helixClient,
|
2022-08-29 21:24:30 +02:00
|
|
|
"err", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-08-30 20:27:20 +02:00
|
|
|
helixResp, err := helixClient.RequestAppAccessToken([]string{"user:read:email"})
|
|
|
|
if err != nil {
|
|
|
|
sugar.Fatalw("Helix: Error getting new helix AppAcessToken",
|
|
|
|
"resp", helixResp,
|
|
|
|
"err", err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the access token on the client
|
|
|
|
helixClient.SetAppAccessToken(helixResp.Data.AccessToken)
|
|
|
|
|
2022-08-12 00:08:39 +02:00
|
|
|
// Establish database connection
|
2022-08-08 23:13:33 +02:00
|
|
|
db, err := openDB(cfg)
|
|
|
|
if err != nil {
|
2023-08-04 20:57:40 +02:00
|
|
|
sugar.Fatalw("could not establish database connection",
|
|
|
|
"err", err,
|
|
|
|
)
|
2022-08-08 23:13:33 +02:00
|
|
|
}
|
2023-08-04 20:57:40 +02:00
|
|
|
app := &application{
|
2022-08-07 02:27:40 +02:00
|
|
|
TwitchClient: tc,
|
2022-08-29 21:24:30 +02:00
|
|
|
HelixClient: helixClient,
|
2023-08-04 20:57:40 +02:00
|
|
|
Log: sugar,
|
2022-08-08 23:13:33 +02:00
|
|
|
Db: db,
|
2023-09-07 22:34:53 +02:00
|
|
|
Models: data.NewModels(db),
|
|
|
|
Scheduler: cron.New(),
|
2023-12-17 17:17:55 +01:00
|
|
|
Config: cfg,
|
2023-12-08 19:23:31 +01:00
|
|
|
Environment: cfg.env,
|
2022-08-07 01:34:31 +02:00
|
|
|
}
|
2022-08-07 03:17:29 +02:00
|
|
|
|
2023-10-03 18:43:43 +02:00
|
|
|
app.Log.Infow("db.Stats",
|
|
|
|
"db.Stats", db.Stats(),
|
|
|
|
)
|
|
|
|
|
2022-08-07 21:43:40 +02:00
|
|
|
// Received a PrivateMessage (normal chat message).
|
2022-08-07 01:34:31 +02:00
|
|
|
app.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
|
2023-12-03 16:35:34 +01:00
|
|
|
// sugar.Infow("New Twitch PrivateMessage",
|
|
|
|
// "message.Channel", message.Channel,
|
|
|
|
// "message.User.DisplayName", message.User.DisplayName,
|
|
|
|
// "message.User.ID", message.User.ID,
|
|
|
|
// "message.Message", message.Message,
|
|
|
|
// )
|
2023-03-03 22:34:52 +01:00
|
|
|
|
2022-08-11 22:44:08 +02:00
|
|
|
// roomId is the Twitch UserID of the channel the message originated from.
|
|
|
|
// If there is no roomId something went really wrong.
|
|
|
|
roomId := message.Tags["room-id"]
|
|
|
|
if roomId == "" {
|
|
|
|
return
|
|
|
|
}
|
2023-08-08 02:37:37 +02:00
|
|
|
|
|
|
|
// Message was shorter than our prefix is therefore it's irrelevant for us.
|
|
|
|
if len(message.Message) >= 2 {
|
|
|
|
// This bots prefix is "()" configured above at cfg.commandPrefix,
|
|
|
|
// Check if the first 2 characters of the mesage were our prefix.
|
|
|
|
// if they were forward the message to the command handler.
|
|
|
|
if message.Message[:2] == cfg.commandPrefix {
|
2023-10-03 22:58:03 +02:00
|
|
|
go app.handleCommand(message)
|
2023-08-08 02:37:37 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special rule for #pajlada.
|
|
|
|
if message.Message == "!nourybot" {
|
2023-10-23 13:48:27 +02:00
|
|
|
app.Send(message.Channel, "Lidl Twitch bot made by @nouryxd. Prefix: ()", message)
|
2023-08-08 02:37:37 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-07 03:09:09 +02:00
|
|
|
})
|
2022-08-06 23:45:02 +02:00
|
|
|
|
2023-12-03 16:35:34 +01:00
|
|
|
app.TwitchClient.OnClearChatMessage(func(message twitch.ClearChatMessage) {
|
|
|
|
if message.BanDuration == 0 && message.Channel == "forsen" {
|
|
|
|
app.TwitchClient.Say("nouryxd", fmt.Sprintf("MODS https://logs.ivr.fi/?channel=forsen&username=%v", message.TargetUsername))
|
|
|
|
}
|
|
|
|
|
|
|
|
if message.BanDuration >= 28700 && message.Channel == "forsen" {
|
2023-12-03 19:13:52 +01:00
|
|
|
app.TwitchClient.Say("nouryxd", fmt.Sprintf("monkaS -%v https://logs.ivr.fi/?channel=forsen&username=%v", message.BanDuration, message.TargetUsername))
|
2023-12-03 16:35:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2023-12-03 19:13:52 +01:00
|
|
|
|
2023-12-13 12:53:04 +01:00
|
|
|
app.InitialJoin()
|
|
|
|
// Load the initial timers from the database.
|
|
|
|
app.InitialTimers()
|
|
|
|
|
|
|
|
// Start the timers.
|
|
|
|
app.Scheduler.Start()
|
2023-08-08 02:37:37 +02:00
|
|
|
|
2023-12-13 12:53:04 +01:00
|
|
|
app.TwitchClient.OnConnect(func() {
|
2023-11-01 19:40:00 +01:00
|
|
|
app.TwitchClient.Say("nouryxd", "gopherDance")
|
2023-10-10 17:51:12 +02:00
|
|
|
app.TwitchClient.Say("nourybot", "gopherDance")
|
2023-11-01 19:40:00 +01:00
|
|
|
app.TwitchClient.Say("xnoury", "alienPls")
|
|
|
|
app.TwitchClient.Say("uudelleenkykeytynyt", "pajaDink")
|
2023-08-08 02:37:37 +02:00
|
|
|
|
|
|
|
// Successfully connected to Twitch
|
|
|
|
app.Log.Infow("Successfully connected to Twitch Servers",
|
|
|
|
"Bot username", cfg.twitchUsername,
|
2023-12-07 17:49:32 +01:00
|
|
|
"Environment", cfg.env,
|
2023-08-08 02:37:37 +02:00
|
|
|
"Database Open Conns", cfg.db.maxOpenConns,
|
|
|
|
"Database Idle Conns", cfg.db.maxIdleConns,
|
|
|
|
"Database Idle Time", cfg.db.maxIdleTime,
|
|
|
|
"Database", db.Stats(),
|
|
|
|
"Helix", helixResp,
|
|
|
|
)
|
2023-09-07 22:34:53 +02:00
|
|
|
|
2022-08-18 18:57:29 +02:00
|
|
|
})
|
2023-12-10 20:13:06 +01:00
|
|
|
|
2023-12-13 12:01:44 +01:00
|
|
|
// Start status page
|
2023-12-17 19:07:05 +01:00
|
|
|
go app.startRouter()
|
2023-12-13 12:01:44 +01:00
|
|
|
|
2022-08-08 23:58:25 +02:00
|
|
|
// Actually connect to chat.
|
2022-08-08 23:13:33 +02:00
|
|
|
err = app.TwitchClient.Connect()
|
2022-08-06 23:45:02 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-10-03 18:43:43 +02:00
|
|
|
|
2022-08-06 23:45:02 +02:00
|
|
|
}
|
2022-08-08 23:13:33 +02:00
|
|
|
|
2022-08-08 23:58:25 +02:00
|
|
|
// openDB returns the sql.DB connection pool.
|
2022-08-08 23:13:33 +02:00
|
|
|
func openDB(cfg config) (*sql.DB, error) {
|
2022-08-08 23:58:25 +02:00
|
|
|
// sql.Open() creates an empty connection pool with the provided DSN
|
2022-08-08 23:13:33 +02:00
|
|
|
db, err := sql.Open("postgres", cfg.db.dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-08 23:58:25 +02:00
|
|
|
// Set database restraints.
|
2022-08-08 23:13:33 +02:00
|
|
|
db.SetMaxOpenConns(cfg.db.maxOpenConns)
|
|
|
|
db.SetMaxIdleConns(cfg.db.maxIdleConns)
|
|
|
|
|
2022-08-08 23:58:25 +02:00
|
|
|
// Parse the maxIdleTime string into an actual duration and set it.
|
2022-08-08 23:13:33 +02:00
|
|
|
duration, err := time.ParseDuration(cfg.db.maxIdleTime)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
db.SetConnMaxIdleTime(duration)
|
|
|
|
|
2022-08-08 23:58:25 +02:00
|
|
|
// Create a new context with a 5 second timeout.
|
2022-08-08 23:13:33 +02:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2022-08-08 23:58:25 +02:00
|
|
|
// db.PingContext() is needed to actually check if the
|
|
|
|
// connection to the database was successful.
|
2022-08-08 23:13:33 +02:00
|
|
|
err = db.PingContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return db, nil
|
|
|
|
}
|