From b2d7860cc238e03b03359bb59721f9527f5fb581 Mon Sep 17 00:00:00 2001 From: lyx0 Date: Thu, 7 Sep 2023 20:12:09 +0200 Subject: [PATCH] refactor send/command functionality --- cmd/nourybot/commands.go | 16 ++++++++------- cmd/nourybot/main.go | 4 ++-- {internal/common => cmd/nourybot}/send.go | 25 +++++++++++------------ internal/commands/ping.go | 5 ++--- 4 files changed, 25 insertions(+), 25 deletions(-) rename {internal/common => cmd/nourybot}/send.go (88%) diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index cc13b7e..ccaa36f 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -11,6 +11,7 @@ import ( // handleCommand takes in a twitch.PrivateMessage and then routes the message to // the function that is responsible for each command and knows how to deal with it accordingly. func (app *application) handleCommand(message twitch.PrivateMessage) { + var reply string // Increments the counter how many commands have been used, called in the ping command. common.CommandUsed() @@ -35,7 +36,6 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { // target is the channelname the message originated from and // where the TwitchClient should send the response target := message.Channel - app.Log.Infow("Command received", // "message", message, // Pretty taxing "message.Message", message.Message, @@ -55,17 +55,14 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { switch commandName { case "": if msgLen == 1 { - common.Send(target, "xd", app.TwitchClient) - return + reply = "xd" } case "nourybot": - common.Send(target, "Lidl Twitch bot made by @nourylul. Prefix: ()", app.TwitchClient) - return + reply = "Lidl Twitch bot made by @nourylul. Prefix: ()" case "ping": - commands.Ping(target, app.TwitchClient) - return + reply = commands.Ping() // ()bttv // ################## @@ -73,4 +70,9 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { // if it doesnt then ignore it. // ################## } + + if reply != "" { + app.Send(target, reply) + return + } } diff --git a/cmd/nourybot/main.go b/cmd/nourybot/main.go index 3e8da61..13cc74a 100644 --- a/cmd/nourybot/main.go +++ b/cmd/nourybot/main.go @@ -159,7 +159,7 @@ func main() { // Special rule for #pajlada. if message.Message == "!nourybot" { - common.Send(message.Channel, "Lidl Twitch bot made by @nourylul. Prefix: ()", app.TwitchClient) + app.Send(message.Channel, "Lidl Twitch bot made by @nourylul. Prefix: ()") } } }) @@ -168,7 +168,7 @@ func main() { common.StartTime() app.TwitchClient.Join("nourylul") - app.TwitchClient.Say("nourylul", "xD!") + app.Send("nourylul", "xD!") // Successfully connected to Twitch app.Log.Infow("Successfully connected to Twitch Servers", diff --git a/internal/common/send.go b/cmd/nourybot/send.go similarity index 88% rename from internal/common/send.go rename to cmd/nourybot/send.go index 67e8a1e..63b72e4 100644 --- a/internal/common/send.go +++ b/cmd/nourybot/send.go @@ -1,4 +1,4 @@ -package common +package main import ( "bytes" @@ -8,7 +8,6 @@ import ( "log" "net/http" - "github.com/gempir/go-twitch-irc/v4" "go.uber.org/zap" ) @@ -39,7 +38,7 @@ var ( // returns true and a string with the reason if it was banned. // More information: // https://gist.github.com/pajlada/57464e519ba8d195a97ddcd0755f9715 -func checkMessage(text string) (bool, string) { +func (app *application) checkMessage(text string) (bool, string) { sugar := zap.NewExample().Sugar() defer sugar.Sync() @@ -83,7 +82,7 @@ func checkMessage(text string) (bool, string) { // Send is used to send twitch replies and contains the necessary // safeguards and logic for that. -func Send(target, message string, tc *twitch.Client) { +func (app *application) Send(target, message string) { sugar := zap.NewExample().Sugar() defer sugar.Sync() @@ -101,10 +100,10 @@ func Send(target, message string, tc *twitch.Client) { } // check the message for bad words before we say it - messageBanned, banReason := checkMessage(message) + messageBanned, banReason := app.checkMessage(message) if messageBanned { // Bad message, replace message and log it. - tc.Say(target, "[BANPHRASED] monkaS") + app.TwitchClient.Say(target, "[BANPHRASED] monkaS") sugar.Infow("banned message detected", "target channel", target, "message", message, @@ -123,13 +122,13 @@ func Send(target, message string, tc *twitch.Client) { firstMessage := message[0:499] secondMessage := message[499:] - tc.Say(target, firstMessage) - tc.Say(target, secondMessage) + app.TwitchClient.Say(target, firstMessage) + app.TwitchClient.Say(target, secondMessage) return } // Message was fine. - tc.Say(target, message) + app.TwitchClient.Say(target, message) return } } @@ -137,7 +136,7 @@ func Send(target, message string, tc *twitch.Client) { // SendNoLimit does not check for the maximum message size. // Used in sending commands from the database since the command has to have // been gotten in there somehow. So it fits. Still checks for banphrases. -func SendNoLimit(target, message string, tc *twitch.Client) { +func (app *application) SendNoLimit(target, message string) { sugar := zap.NewExample().Sugar() defer sugar.Sync() @@ -155,10 +154,10 @@ func SendNoLimit(target, message string, tc *twitch.Client) { } // check the message for bad words before we say it - messageBanned, banReason := checkMessage(message) + messageBanned, banReason := app.checkMessage(message) if messageBanned { // Bad message, replace message and log it. - tc.Say(target, "[BANPHRASED] monkaS") + app.TwitchClient.Say(target, "[BANPHRASED] monkaS") sugar.Infow("banned message detected", "target channel", target, "message", message, @@ -174,7 +173,7 @@ func SendNoLimit(target, message string, tc *twitch.Client) { // https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316 // TODO: Make it so it splits at a space instead and not in the middle of a word. // Message was fine. - tc.Say(target, message) + app.TwitchClient.Say(target, message) return } } diff --git a/internal/commands/ping.go b/internal/commands/ping.go index 9799d2c..bf092ac 100644 --- a/internal/commands/ping.go +++ b/internal/commands/ping.go @@ -3,15 +3,14 @@ package commands import ( "fmt" - "github.com/gempir/go-twitch-irc/v4" "github.com/lyx0/nourybot/internal/common" "github.com/lyx0/nourybot/internal/humanize" ) -func Ping(target string, tc *twitch.Client) { +func Ping() string { botUptime := humanize.Time(common.GetUptime()) commandsUsed := common.GetCommandsUsed() reply := fmt.Sprintf("Pong! :) Commands used: %v, Last restart: %v", commandsUsed, botUptime) - common.Send(target, reply, tc) + return reply }