add comments, print a message on successful connection

This commit is contained in:
lyx0 2022-06-04 02:56:28 +02:00
parent 956b1a8614
commit 9293b6ff00
3 changed files with 45 additions and 8 deletions

View file

@ -5,10 +5,10 @@ run:
cd cmd/bot && ./Nourybot
dev:
cd cmd/bot && go build -o Nourybot && ./Nourybot -mode dev
cd cmd/bot && go build -o Nourybot && ./Nourybot -env development
prod:
cd cmd/bot && go build -o Nourybot && ./Nourybot -mode production
cd cmd/bot && go build -o Nourybot && ./Nourybot -env production
xd:
cd cmd/bot && go build -o Nourybot && ./Nourybot
cd cmd/bot && go build -o Nourybot && ./Nourybot

View file

@ -24,36 +24,64 @@ type application struct {
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.
flag.StringVar(&cfg.env, "env", "development", "Environment (development|string|production)")
flag.Parse()
// Initialize a new logger we attach to our application struct.
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
// Load the .env file and check for errors.
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
logger.Fatal("Error loading .env file")
}
// Load bot credentials from the .env file.
cfg.botUsername = os.Getenv("BOT_USER")
cfg.botOauth = os.Getenv("BOT_OAUTH")
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
// Initialize a new twitch client which we attach to our
// application struct.
twitchClient := twitch.NewClient(cfg.botUsername, cfg.botOauth)
// Finally Initialize a new application instance with our
// attached methods.
app := &application{
config: cfg,
twitchClient: twitchClient,
logger: logger,
}
// Received a PrivateMessage (normal chat message), pass it to
// the handler who checks for further action.
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.Printf("Successfully connected to Twitch Servers in %s mode!", app.config.env)
})
// Join test channels
app.twitchClient.Join("nourylul")
app.twitchClient.Join("nourybot")
app.twitchClient.Say("nourylul", "xd")
app.twitchClient.Say("nourybot", "xd")
// Say hello because we are nice :^)
app.twitchClient.Say("nourylul", "RaccAttack")
app.twitchClient.Say("nourybot", "RaccAttack")
// Connect to the twitch IRC servers.
err = app.twitchClient.Connect()
if err != nil {
panic(err)

View file

@ -0,0 +1,9 @@
package main
import "github.com/gempir/go-twitch-irc/v3"
func (app *application) handleWhisperMessage(message twitch.WhisperMessage) {
// Print the whisper message for now.
// TODO: Implement a basic whisper handler.
app.logger.Printf("[#whisper]:%s: %s", message.User.DisplayName, message.Message)
}