mirror-nourybot/cmd/nourybot/main.go

203 lines
4.9 KiB
Go
Raw Normal View History

2022-08-06 23:45:02 +02:00
package main
2022-08-07 01:34:31 +02:00
import (
"context"
"database/sql"
2023-03-03 22:16:32 +01:00
"flag"
"os"
"time"
2023-08-08 01:00:14 +02:00
"github.com/gempir/go-twitch-irc/v4"
"github.com/jakecoffman/cron"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
2022-08-29 21:24:30 +02:00
"github.com/nicklaw5/helix"
2023-08-04 20:57:40 +02:00
"github.com/rs/zerolog/log"
2022-08-07 03:09:09 +02:00
"go.uber.org/zap"
2022-08-07 01:34:31 +02:00
)
type config struct {
2022-08-29 21:24:30 +02:00
twitchUsername string
twitchOauth string
twitchClientId string
twitchClientSecret string
commandPrefix string
db struct {
dsn string
maxOpenConns int
maxIdleConns int
maxIdleTime string
}
}
2023-08-04 20:57:40 +02:00
type application struct {
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
Db *sql.DB
2023-08-04 20:57:40 +02:00
// Models data.Models
Scheduler *cron.Cron
// Rdb *redis.Client
}
2023-03-03 22:16:32 +01:00
var envFlag string
2023-04-13 03:26:20 +02:00
var ctx = context.Background()
2023-03-03 22:16:32 +01:00
func init() {
flag.StringVar(&envFlag, "env", "dev", "database connection to use: (dev/prod)")
flag.Parse()
}
2022-08-06 23:45:02 +02:00
func main() {
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()
defer logger.Sync()
sugar := logger.Sugar()
2022-08-08 23:58:25 +02:00
err := godotenv.Load()
if err != nil {
2023-08-04 20:57:40 +02:00
sugar.Fatal("Error loading .env")
}
2022-08-07 03:09:09 +02:00
// Twitch config variables
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")
2022-08-12 00:08:39 +02:00
cfg.commandPrefix = os.Getenv("TWITCH_COMMAND_PREFIX")
2022-08-08 23:58:25 +02:00
tc := twitch.NewClient(cfg.twitchUsername, cfg.twitchOauth)
2023-03-03 22:16:32 +01:00
switch envFlag {
case "dev":
cfg.db.dsn = os.Getenv("LOCAL_DSN")
case "prod":
2023-08-08 01:22:49 +02:00
cfg.db.dsn = os.Getenv("REMOTE_DSN")
2023-03-03 22:16:32 +01:00
}
// Database config variables
cfg.db.maxOpenConns = 25
cfg.db.maxIdleConns = 25
cfg.db.maxIdleTime = "15m"
// 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{
ClientID: cfg.twitchClientId,
ClientSecret: cfg.twitchClientSecret,
2022-08-29 21:24:30 +02:00
})
if err != nil {
sugar.Fatalw("Error creating new helix client",
"helixClient", helixClient,
2022-08-29 21:24:30 +02:00
"err", err,
)
}
helixResp, err := helixClient.RequestAppAccessToken([]string{"user:read:email"})
if err != nil {
sugar.Fatalw("Helix: Error getting new helix AppAcessToken",
"resp", helixResp,
"err", err,
)
}
2023-01-04 15:24:40 +01:00
sugar.Infow("Got new helix AppAccessToken",
"helixClient", helixResp,
)
// Set the access token on the client
helixClient.SetAppAccessToken(helixResp.Data.AccessToken)
2022-08-12 00:08:39 +02:00
// Establish database connection
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:58:25 +02:00
2023-08-04 20:57:40 +02:00
sugar.Infow("db.Stats",
"db.Stats", db.Stats(),
2023-04-13 03:26:20 +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,
Db: db,
2022-08-07 01:34:31 +02:00
}
2022-08-07 03:17:29 +02:00
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-08-04 20:57:40 +02:00
// sugar.Infow("New Twitch PrivateMessage",
// "message.Channel", message.Channel,
// "message.User.DisplayName", message.User.DisplayName,
2023-08-04 20:57:40 +02:00
// "message.User.ID", message.User.ID,
// "message.Message", message.Message,
// )
2023-03-03 22:34:52 +01: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 == "" {
2023-08-04 20:57:40 +02:00
log.Error().Msgf("Missing room-id in message tag: %s", roomId)
return
}
2022-08-07 03:09:09 +02:00
})
2022-08-06 23:45:02 +02:00
2022-08-07 21:43:40 +02:00
// Successfully connected to Twitch
2023-08-04 20:57:40 +02:00
app.Log.Infow("Successfully connected to Twitch Servers",
"Bot username", cfg.twitchUsername,
"Environment", envFlag,
"Database Open Conns", cfg.db.maxOpenConns,
"Database Idle Conns", cfg.db.maxIdleConns,
"Database Idle Time", cfg.db.maxIdleTime,
"Database", db.Stats(),
"Helix", helixResp,
)
2022-08-07 01:34:31 +02:00
app.TwitchClient.OnConnect(func() {
2023-08-04 20:57:40 +02:00
app.TwitchClient.Join("nourylul")
app.TwitchClient.Say("nourylul", "xD!")
// sugar.Infow("db.Stats",
// "db.Stats", db.Stats(),
// )
2022-08-18 18:57:29 +02:00
})
2022-08-08 23:58:25 +02:00
// Actually connect to chat.
err = app.TwitchClient.Connect()
2022-08-06 23:45:02 +02:00
if err != nil {
panic(err)
}
}
2022-08-08 23:58:25 +02:00
// openDB returns the sql.DB connection pool.
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
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.
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.
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.
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.
err = db.PingContext(ctx)
if err != nil {
return nil, err
}
return db, nil
}