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
// 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 <emote name>
// ##################
@ -73,4 +70,9 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
// 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.
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",

View file

@ -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
}
}

View file

@ -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
}