mirror-nourybot/cmd/bot/main.go

193 lines
4.7 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"
"log"
"os"
"time"
2022-08-07 01:34:31 +02:00
"github.com/gempir/go-twitch-irc/v3"
"github.com/jakecoffman/cron"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
2022-08-09 00:07:32 +02:00
"github.com/lyx0/nourybot/internal/data"
2022-08-07 02:27:40 +02:00
"github.com/lyx0/nourybot/pkg/common"
2022-08-07 03:09:09 +02:00
"go.uber.org/zap"
2022-08-07 01:34:31 +02:00
)
type config struct {
twitchUsername string
twitchOauth string
commandPrefix string
environment string
db struct {
dsn string
maxOpenConns int
maxIdleConns int
maxIdleTime string
}
}
type Application struct {
TwitchClient *twitch.Client
Logger *zap.SugaredLogger
Db *sql.DB
Models data.Models
Scheduler *cron.Cron
}
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.
sugar := zap.NewExample().Sugar()
defer sugar.Sync()
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
2022-08-07 03:09:09 +02:00
2022-08-08 23:58:25 +02:00
// Twitch
cfg.twitchUsername = os.Getenv("TWITCH_USERNAME")
cfg.twitchOauth = os.Getenv("TWITCH_OAUTH")
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)
2022-08-12 00:08:39 +02:00
// Will be used someday Copesen
cfg.environment = "Development"
2022-08-08 23:58:25 +02:00
// Database
cfg.db.dsn = os.Getenv("DB_DSN")
cfg.db.maxOpenConns = 25
cfg.db.maxIdleConns = 25
cfg.db.maxIdleTime = "15m"
2022-08-12 00:08:39 +02:00
// Establish database connection
db, err := openDB(cfg)
if err != nil {
sugar.Fatal(err)
}
2022-08-08 23:58:25 +02:00
c := cron.New()
//s := gocron.NewScheduler(time.UTC)
2022-08-08 23:58:25 +02:00
// Initialize Application
2022-08-07 02:27:40 +02:00
app := &Application{
TwitchClient: tc,
2022-08-07 03:09:09 +02:00
Logger: sugar,
Db: db,
2022-08-09 01:09:53 +02:00
Models: data.NewModels(db),
Scheduler: c,
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) {
2022-08-14 20:33:49 +02:00
// app.Logger.Infow("Private Message received",
// // "message", message,
// "message.Channel", message.Channel,
// "message.User", message.User.DisplayName,
// "message.Message", message.Message,
// )
// 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 == "" {
app.Logger.Errorw("Missing room-id in message tag",
"roomId", roomId,
)
return
}
// Message was shorter than our prefix is therefore it's irrelevant for us.
if len(message.Message) >= 2 {
2022-08-11 23:34:00 +02:00
// This bots prefix is "()" configured above at `cfg.commandPrefix`,
// Check if the first 2 characters of the mesage were our prefix.
2022-08-11 23:34:00 +02:00
// if they were forward the message to the command handler.
if message.Message[:2] == cfg.commandPrefix {
app.handleCommand(message)
return
}
}
2022-08-07 01:34:31 +02:00
})
2022-08-07 21:43:40 +02:00
// Received a WhisperMessage (Twitch DM).
2022-08-07 03:09:09 +02:00
app.TwitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) {
// Print the whisper message for now.
app.Logger.Infow("Whisper Message received",
"message", message,
"message.User.DisplayName", message.User.DisplayName,
"message.Message", message.Message,
)
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
2022-08-07 01:34:31 +02:00
app.TwitchClient.OnConnect(func() {
2022-08-07 04:35:03 +02:00
app.Logger.Infow("Successfully connected to Twitch Servers",
"Bot username", cfg.twitchUsername,
"Environment", cfg.environment,
2022-08-08 23:58:25 +02:00
"Database Open Conns", cfg.db.maxOpenConns,
"Database Idle Conns", cfg.db.maxIdleConns,
"Database Idle Time", cfg.db.maxIdleTime,
"Database", db.Stats(),
2022-08-07 04:35:03 +02:00
)
2022-08-07 03:09:09 +02:00
2022-08-08 23:58:25 +02:00
// Start time
common.StartTime()
2022-08-12 00:08:39 +02:00
// Join the channels in the database.
app.InitialJoin()
2022-08-12 00:08:39 +02:00
app.InitialTimers()
2022-08-08 23:58:25 +02:00
common.Send("nourylul", "dankCircle", app.TwitchClient)
2022-08-22 03:37:22 +02:00
common.Send("nourybot", "gopherDance", app.TwitchClient)
common.Send("xnoury", "pajaDink", app.TwitchClient)
common.Send("uudelleenkytkeytynyt", "PepeS", app.TwitchClient)
2022-08-18 18:57:29 +02:00
})
c.Start()
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
}