refactor send/command functionality

This commit is contained in:
lyx0 2023-09-07 20:12:09 +02:00
parent 7c06a638b0
commit b2d7860cc2
4 changed files with 25 additions and 25 deletions

View file

@ -11,6 +11,7 @@ import (
// handleCommand takes in a twitch.PrivateMessage and then routes the message to // 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. // the function that is responsible for each command and knows how to deal with it accordingly.
func (app *application) handleCommand(message twitch.PrivateMessage) { func (app *application) handleCommand(message twitch.PrivateMessage) {
var reply string
// Increments the counter how many commands have been used, called in the ping command. // Increments the counter how many commands have been used, called in the ping command.
common.CommandUsed() common.CommandUsed()
@ -35,7 +36,6 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
// target is the channelname the message originated from and // target is the channelname the message originated from and
// where the TwitchClient should send the response // where the TwitchClient should send the response
target := message.Channel target := message.Channel
app.Log.Infow("Command received", app.Log.Infow("Command received",
// "message", message, // Pretty taxing // "message", message, // Pretty taxing
"message.Message", message.Message, "message.Message", message.Message,
@ -55,17 +55,14 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
switch commandName { switch commandName {
case "": case "":
if msgLen == 1 { if msgLen == 1 {
common.Send(target, "xd", app.TwitchClient) reply = "xd"
return
} }
case "nourybot": case "nourybot":
common.Send(target, "Lidl Twitch bot made by @nourylul. Prefix: ()", app.TwitchClient) reply = "Lidl Twitch bot made by @nourylul. Prefix: ()"
return
case "ping": case "ping":
commands.Ping(target, app.TwitchClient) reply = commands.Ping()
return
// ()bttv <emote name> // ()bttv <emote name>
// ################## // ##################
@ -73,4 +70,9 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
// if it doesnt then ignore it. // if it doesnt then ignore it.
// ################## // ##################
} }
if reply != "" {
app.Send(target, reply)
return
}
} }

View file

@ -159,7 +159,7 @@ func main() {
// Special rule for #pajlada. // Special rule for #pajlada.
if message.Message == "!nourybot" { 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() common.StartTime()
app.TwitchClient.Join("nourylul") app.TwitchClient.Join("nourylul")
app.TwitchClient.Say("nourylul", "xD!") app.Send("nourylul", "xD!")
// Successfully connected to Twitch // Successfully connected to Twitch
app.Log.Infow("Successfully connected to Twitch Servers", app.Log.Infow("Successfully connected to Twitch Servers",

View file

@ -1,4 +1,4 @@
package common package main
import ( import (
"bytes" "bytes"
@ -8,7 +8,6 @@ import (
"log" "log"
"net/http" "net/http"
"github.com/gempir/go-twitch-irc/v4"
"go.uber.org/zap" "go.uber.org/zap"
) )
@ -39,7 +38,7 @@ var (
// returns true and a string with the reason if it was banned. // returns true and a string with the reason if it was banned.
// More information: // More information:
// https://gist.github.com/pajlada/57464e519ba8d195a97ddcd0755f9715 // https://gist.github.com/pajlada/57464e519ba8d195a97ddcd0755f9715
func checkMessage(text string) (bool, string) { func (app *application) checkMessage(text string) (bool, string) {
sugar := zap.NewExample().Sugar() sugar := zap.NewExample().Sugar()
defer sugar.Sync() defer sugar.Sync()
@ -83,7 +82,7 @@ func checkMessage(text string) (bool, string) {
// Send is used to send twitch replies and contains the necessary // Send is used to send twitch replies and contains the necessary
// safeguards and logic for that. // safeguards and logic for that.
func Send(target, message string, tc *twitch.Client) { func (app *application) Send(target, message string) {
sugar := zap.NewExample().Sugar() sugar := zap.NewExample().Sugar()
defer sugar.Sync() 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 // check the message for bad words before we say it
messageBanned, banReason := checkMessage(message) messageBanned, banReason := app.checkMessage(message)
if messageBanned { if messageBanned {
// Bad message, replace message and log it. // Bad message, replace message and log it.
tc.Say(target, "[BANPHRASED] monkaS") app.TwitchClient.Say(target, "[BANPHRASED] monkaS")
sugar.Infow("banned message detected", sugar.Infow("banned message detected",
"target channel", target, "target channel", target,
"message", message, "message", message,
@ -123,13 +122,13 @@ func Send(target, message string, tc *twitch.Client) {
firstMessage := message[0:499] firstMessage := message[0:499]
secondMessage := message[499:] secondMessage := message[499:]
tc.Say(target, firstMessage) app.TwitchClient.Say(target, firstMessage)
tc.Say(target, secondMessage) app.TwitchClient.Say(target, secondMessage)
return return
} }
// Message was fine. // Message was fine.
tc.Say(target, message) app.TwitchClient.Say(target, message)
return return
} }
} }
@ -137,7 +136,7 @@ func Send(target, message string, tc *twitch.Client) {
// SendNoLimit does not check for the maximum message size. // SendNoLimit does not check for the maximum message size.
// Used in sending commands from the database since the command has to have // 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. // 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() sugar := zap.NewExample().Sugar()
defer sugar.Sync() 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 // check the message for bad words before we say it
messageBanned, banReason := checkMessage(message) messageBanned, banReason := app.checkMessage(message)
if messageBanned { if messageBanned {
// Bad message, replace message and log it. // Bad message, replace message and log it.
tc.Say(target, "[BANPHRASED] monkaS") app.TwitchClient.Say(target, "[BANPHRASED] monkaS")
sugar.Infow("banned message detected", sugar.Infow("banned message detected",
"target channel", target, "target channel", target,
"message", message, "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 // 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. // TODO: Make it so it splits at a space instead and not in the middle of a word.
// Message was fine. // Message was fine.
tc.Say(target, message) app.TwitchClient.Say(target, message)
return return
} }
} }

View file

@ -3,15 +3,14 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v4"
"github.com/lyx0/nourybot/internal/common" "github.com/lyx0/nourybot/internal/common"
"github.com/lyx0/nourybot/internal/humanize" "github.com/lyx0/nourybot/internal/humanize"
) )
func Ping(target string, tc *twitch.Client) { func Ping() string {
botUptime := humanize.Time(common.GetUptime()) botUptime := humanize.Time(common.GetUptime())
commandsUsed := common.GetCommandsUsed() commandsUsed := common.GetCommandsUsed()
reply := fmt.Sprintf("Pong! :) Commands used: %v, Last restart: %v", commandsUsed, botUptime) reply := fmt.Sprintf("Pong! :) Commands used: %v, Last restart: %v", commandsUsed, botUptime)
common.Send(target, reply, tc) return reply
} }