From 9293b6ff00771e5e72ba11e21e9e3e1534a716ff Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sat, 4 Jun 2022 02:56:28 +0200 Subject: [PATCH] add comments, print a message on successful connection --- Makefile | 6 +++--- cmd/bot/main.go | 38 +++++++++++++++++++++++++++++++++----- cmd/bot/whispermessage.go | 9 +++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 cmd/bot/whispermessage.go diff --git a/Makefile b/Makefile index 35a50d4..c4aed9b 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file + cd cmd/bot && go build -o Nourybot && ./Nourybot diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 4d767d3..e3b7ef7 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -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) diff --git a/cmd/bot/whispermessage.go b/cmd/bot/whispermessage.go new file mode 100644 index 0000000..75fe3af --- /dev/null +++ b/cmd/bot/whispermessage.go @@ -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) +}