From 28b318dc9261c5de660b0a43a24e59f80ae2490f Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Thu, 11 Aug 2022 22:44:08 +0200 Subject: [PATCH] move basic message routing into main function --- cmd/bot/main.go | 35 +++++++++++++++++++++++++++++++-- cmd/bot/message.go | 49 ---------------------------------------------- 2 files changed, 33 insertions(+), 51 deletions(-) delete mode 100644 cmd/bot/message.go diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 54b6711..3cb712c 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -57,12 +57,43 @@ func main() { // Received a PrivateMessage (normal chat message). app.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) { - app.handlePrivateMessage(message) + // 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 { + // Check if the first 2 characters of the mesage were our prefix. + if message.Message[:2] == "()" { + app.handleCommand(message) + return + } + } + }) // Received a WhisperMessage (Twitch DM). app.TwitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) { - app.handleWhisperMessage(message) + // Print the whisper message for now. + app.Logger.Infow("Whisper Message received", + "message", message, + "message.User.DisplayName", message.User.DisplayName, + "message.Message", message.Message, + ) }) // Successfully connected to Twitch diff --git a/cmd/bot/message.go b/cmd/bot/message.go deleted file mode 100644 index 3b63392..0000000 --- a/cmd/bot/message.go +++ /dev/null @@ -1,49 +0,0 @@ -package main - -import ( - "github.com/gempir/go-twitch-irc/v3" - "go.uber.org/zap" -) - -func (app *Application) handlePrivateMessage(message twitch.PrivateMessage) { - sugar := zap.NewExample().Sugar() - defer sugar.Sync() - - // 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 { - // Strip the `()` prefix - if message.Message[:2] == "()" { - app.handleCommand(message) - return - } - } - - // app.Logger.Infow("Private Message received", - // // "message", message, - // "message.Channel", message.Channel, - // "message.User", message.User.DisplayName, - // "message.Message", message.Message, - // ) - -} - -func (app *Application) handleWhisperMessage(message twitch.WhisperMessage) { - // Print the whisper message for now. - // TODO: Implement a basic whisper handler. - app.Logger.Infow("Whisper Message received", - "message", message, - "message.User.DisplayName", message.User.DisplayName, - "message.Message", message.Message, - ) -}