mirror-nourybot/cmd/bot/main.go

90 lines
2.3 KiB
Go
Raw Normal View History

2022-06-04 02:21:20 +02:00
package main
import (
"flag"
"os"
2022-06-04 02:29:05 +02:00
"github.com/gempir/go-twitch-irc/v3"
2022-06-04 02:21:20 +02:00
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
2022-06-04 02:21:20 +02:00
)
type config struct {
env string
botUsername string
botOauth string
}
type application struct {
config config
twitchClient *twitch.Client
logger *logrus.Logger
2022-06-04 02:21:20 +02:00
}
func main() {
var cfg config
// Parse which environment we are running in. This will decide in
// the future how many channels we join or which database we are
// connecting to for example.
2022-06-04 02:21:20 +02:00
flag.StringVar(&cfg.env, "env", "development", "Environment (development|string|production)")
flag.Parse()
// Initialize a new logger we attach to our application struct.
lgr := logrus.New()
// Load the .env file and check for errors.
2022-06-04 02:21:20 +02:00
err := godotenv.Load()
if err != nil {
lgr.Fatal("Error loading .env file")
2022-06-04 02:21:20 +02:00
}
// Load bot credentials from the .env file.
2022-06-04 02:29:05 +02:00
cfg.botUsername = os.Getenv("BOT_USER")
cfg.botOauth = os.Getenv("BOT_OAUTH")
2022-06-04 02:21:20 +02:00
// Initialize a new twitch client which we attach to our
// application struct.
2022-06-04 02:21:20 +02:00
twitchClient := twitch.NewClient(cfg.botUsername, cfg.botOauth)
// Finally Initialize a new application instance with our
// attached methods.
2022-06-04 02:21:20 +02:00
app := &application{
config: cfg,
twitchClient: twitchClient,
logger: lgr,
2022-06-04 02:21:20 +02:00
}
// Received a PrivateMessage (normal chat message), pass it to
// the handler who checks for further action.
2022-06-04 02:40:43 +02:00
app.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
app.handlePrivateMessage(message)
})
// Received a WhisperMessage (Twitch DM), pass it to
// the handler who checks for further action.
app.twitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) {
app.handleWhisperMessage(message)
})
// Successfully connected to Twitch so we log a message with the
// mode we are currently running in..
app.twitchClient.OnConnect(func() {
app.logger.Infof("Successfully connected to Twitch Servers in %s mode!", app.config.env)
})
// Join test channels
2022-06-04 02:29:05 +02:00
app.twitchClient.Join("nourylul")
app.twitchClient.Join("nourybot")
// Say hello because we are nice :^)
app.twitchClient.Say("nourylul", "RaccAttack")
app.twitchClient.Say("nourybot", "RaccAttack")
2022-06-04 02:29:05 +02:00
// Connect to the twitch IRC servers.
2022-06-04 02:29:05 +02:00
err = app.twitchClient.Connect()
if err != nil {
panic(err)
}
2022-06-04 02:21:20 +02:00
}