mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
add basic command handling skeleton
This commit is contained in:
parent
2a54f591c8
commit
7c06a638b0
76
cmd/nourybot/commands.go
Normal file
76
cmd/nourybot/commands.go
Normal file
|
@ -0,0 +1,76 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gempir/go-twitch-irc/v4"
|
||||
"github.com/lyx0/nourybot/internal/commands"
|
||||
"github.com/lyx0/nourybot/internal/common"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
|
||||
// Increments the counter how many commands have been used, called in the ping command.
|
||||
common.CommandUsed()
|
||||
|
||||
// commandName is the actual name of the command without the prefix.
|
||||
// e.g. `()ping` would be `ping`.
|
||||
commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:])
|
||||
|
||||
// cmdParams are additional command parameters.
|
||||
// e.g. `()weather san antonio`
|
||||
// cmdParam[0] is `san` and cmdParam[1] = `antonio`.
|
||||
//
|
||||
// Since Twitch messages are at most 500 characters I use a
|
||||
// maximum count of 500+10 just to be safe.
|
||||
// https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316
|
||||
cmdParams := strings.SplitN(message.Message, " ", 500)
|
||||
|
||||
// msgLen is the amount of words in a message without the prefix.
|
||||
// Useful to check if enough cmdParams are provided.
|
||||
msgLen := len(strings.SplitN(message.Message, " ", -2))
|
||||
|
||||
// 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,
|
||||
"message.Channel", target,
|
||||
"commandName", commandName,
|
||||
"cmdParams", cmdParams,
|
||||
"msgLen", msgLen,
|
||||
)
|
||||
|
||||
// A `commandName` is every message starting with `()`.
|
||||
// Hardcoded commands have a priority over database commands.
|
||||
// Switch over the commandName and see if there is a hardcoded case for it.
|
||||
// If there was no switch case satisfied, query the database if there is
|
||||
// a data.CommandModel.Name equal to the `commandName`
|
||||
// If there is return the data.CommandModel.Text entry.
|
||||
// Otherwise we ignore the message.
|
||||
switch commandName {
|
||||
case "":
|
||||
if msgLen == 1 {
|
||||
common.Send(target, "xd", app.TwitchClient)
|
||||
return
|
||||
}
|
||||
|
||||
case "nourybot":
|
||||
common.Send(target, "Lidl Twitch bot made by @nourylul. Prefix: ()", app.TwitchClient)
|
||||
return
|
||||
|
||||
case "ping":
|
||||
commands.Ping(target, app.TwitchClient)
|
||||
return
|
||||
// ()bttv <emote name>
|
||||
|
||||
// ##################
|
||||
// Check if the commandName exists as the "name" of a command in the database.
|
||||
// if it doesnt then ignore it.
|
||||
// ##################
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/jakecoffman/cron"
|
||||
"github.com/joho/godotenv"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/lyx0/nourybot/internal/common"
|
||||
"github.com/nicklaw5/helix"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
|
@ -53,7 +54,13 @@ func main() {
|
|||
// Initialize a new sugared logger that we'll pass on
|
||||
// down through the application.
|
||||
logger := zap.NewExample()
|
||||
defer logger.Sync()
|
||||
defer func() {
|
||||
if err := logger.Sync(); err != nil {
|
||||
logger.Sugar().Fatalw("error syncing logger",
|
||||
"error", err,
|
||||
)
|
||||
}
|
||||
}()
|
||||
sugar := logger.Sugar()
|
||||
|
||||
err := godotenv.Load()
|
||||
|
@ -100,9 +107,6 @@ func main() {
|
|||
"err", err,
|
||||
)
|
||||
}
|
||||
sugar.Infow("Got new helix AppAccessToken",
|
||||
"helixClient", helixResp,
|
||||
)
|
||||
|
||||
// Set the access token on the client
|
||||
helixClient.SetAppAccessToken(helixResp.Data.AccessToken)
|
||||
|
@ -142,8 +146,30 @@ func main() {
|
|||
log.Error().Msgf("Missing room-id in message tag: %s", roomId)
|
||||
return
|
||||
}
|
||||
|
||||
// Message was shorter than our prefix is therefore it's irrelevant for us.
|
||||
if len(message.Message) >= 2 {
|
||||
// This bots prefix is "()" configured above at cfg.commandPrefix,
|
||||
// Check if the first 2 characters of the mesage were our prefix.
|
||||
// if they were forward the message to the command handler.
|
||||
if message.Message[:2] == cfg.commandPrefix {
|
||||
app.handleCommand(message)
|
||||
return
|
||||
}
|
||||
|
||||
// Special rule for #pajlada.
|
||||
if message.Message == "!nourybot" {
|
||||
common.Send(message.Channel, "Lidl Twitch bot made by @nourylul. Prefix: ()", app.TwitchClient)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
app.TwitchClient.OnConnect(func() {
|
||||
common.StartTime()
|
||||
|
||||
app.TwitchClient.Join("nourylul")
|
||||
app.TwitchClient.Say("nourylul", "xD!")
|
||||
|
||||
// Successfully connected to Twitch
|
||||
app.Log.Infow("Successfully connected to Twitch Servers",
|
||||
"Bot username", cfg.twitchUsername,
|
||||
|
@ -154,12 +180,6 @@ func main() {
|
|||
"Database", db.Stats(),
|
||||
"Helix", helixResp,
|
||||
)
|
||||
app.TwitchClient.OnConnect(func() {
|
||||
app.TwitchClient.Join("nourylul")
|
||||
app.TwitchClient.Say("nourylul", "xD!")
|
||||
// sugar.Infow("db.Stats",
|
||||
// "db.Stats", db.Stats(),
|
||||
// )
|
||||
})
|
||||
// Actually connect to chat.
|
||||
err = app.TwitchClient.Connect()
|
||||
|
|
1
go.mod
1
go.mod
|
@ -17,6 +17,7 @@ require (
|
|||
)
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.1
|
||||
github.com/golang-jwt/jwt v3.2.1+incompatible // indirect
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -3,6 +3,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
|
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/gempir/go-twitch-irc/v4 v4.0.0 h1:sHVIvbWOv9nHXGEErilclxASv0AaQEr/r/f9C0B9aO8=
|
||||
github.com/gempir/go-twitch-irc/v4 v4.0.0/go.mod h1:QsOMMAk470uxQ7EYD9GJBGAVqM/jDrXBNbuePfTauzg=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
|
|
17
internal/commands/ping.go
Normal file
17
internal/commands/ping.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
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) {
|
||||
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)
|
||||
}
|
17
internal/common/counter.go
Normal file
17
internal/common/counter.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package common
|
||||
|
||||
var (
|
||||
tempCommands = 0
|
||||
)
|
||||
|
||||
// CommandUsed is called on every command incremenenting tempCommands.
|
||||
func CommandUsed() {
|
||||
tempCommands++
|
||||
}
|
||||
|
||||
// GetCommandsUsed returns the amount of commands that have been used
|
||||
// since the last restart.
|
||||
func GetCommandsUsed() int {
|
||||
return tempCommands
|
||||
}
|
||||
|
180
internal/common/send.go
Normal file
180
internal/common/send.go
Normal file
|
@ -0,0 +1,180 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gempir/go-twitch-irc/v4"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// banphraseResponse is the data we receive back from
|
||||
// the banphrase API
|
||||
type banphraseResponse struct {
|
||||
Banned bool `json:"banned"`
|
||||
InputMessage string `json:"input_message"`
|
||||
BanphraseData banphraseData `json:"banphrase_data"`
|
||||
}
|
||||
|
||||
// banphraseData contains details about why a message
|
||||
// was banphrased.
|
||||
type banphraseData struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Phrase string `json:"phrase"`
|
||||
Length int `json:"length"`
|
||||
Permanent bool `json:"permanent"`
|
||||
}
|
||||
|
||||
var (
|
||||
banPhraseUrl = "https://pajlada.pajbot.com/api/v1/banphrases/test"
|
||||
)
|
||||
|
||||
// CheckMessage checks a given message against the banphrase api.
|
||||
// returns false, "okay" if a message is allowed
|
||||
// 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) {
|
||||
sugar := zap.NewExample().Sugar()
|
||||
defer sugar.Sync()
|
||||
|
||||
// {"message": "AHAHAHAHA LUL"}
|
||||
reqBody, err := json.Marshal(map[string]string{
|
||||
"message": text,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
resp, err := http.Post(banPhraseUrl, "application/json", bytes.NewBuffer(reqBody))
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
var responseObject banphraseResponse
|
||||
json.Unmarshal(body, &responseObject)
|
||||
|
||||
// Bad Message
|
||||
//
|
||||
// {"phrase": "No gyazo allowed"}
|
||||
reason := responseObject.BanphraseData.Name
|
||||
if responseObject.Banned {
|
||||
return true, fmt.Sprint(reason)
|
||||
} else if !responseObject.Banned {
|
||||
// Good message
|
||||
return false, "okay"
|
||||
}
|
||||
|
||||
// Couldn't contact api so assume it was a bad message
|
||||
return true, "Banphrase API couldn't be reached monkaS"
|
||||
}
|
||||
|
||||
// Send is used to send twitch replies and contains the necessary
|
||||
// safeguards and logic for that.
|
||||
func Send(target, message string, tc *twitch.Client) {
|
||||
sugar := zap.NewExample().Sugar()
|
||||
defer sugar.Sync()
|
||||
|
||||
// Message we are trying to send is empty.
|
||||
if len(message) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// Since messages starting with `.` or `/` are used for special actions
|
||||
// (ban, whisper, timeout) and so on, we place an emote infront of it so
|
||||
// the actions wouldn't execute. `!` and `$` are common bot prefixes so we
|
||||
// don't allow them either.
|
||||
if message[0] == '.' || message[0] == '/' || message[0] == '!' || message[0] == '$' {
|
||||
message = ":tf: " + message
|
||||
}
|
||||
|
||||
// check the message for bad words before we say it
|
||||
messageBanned, banReason := checkMessage(message)
|
||||
if messageBanned {
|
||||
// Bad message, replace message and log it.
|
||||
tc.Say(target, "[BANPHRASED] monkaS")
|
||||
sugar.Infow("banned message detected",
|
||||
"target channel", target,
|
||||
"message", message,
|
||||
"ban reason", banReason,
|
||||
)
|
||||
|
||||
return
|
||||
} else {
|
||||
// In case the message we are trying to send is longer than the
|
||||
// maximum allowed message length on twitch we split the message in two parts.
|
||||
// Twitch has a maximum length for messages of 510 characters so to be safe
|
||||
// we split and check at 500 characters.
|
||||
// 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.
|
||||
if len(message) > 500 {
|
||||
firstMessage := message[0:499]
|
||||
secondMessage := message[499:]
|
||||
|
||||
tc.Say(target, firstMessage)
|
||||
tc.Say(target, secondMessage)
|
||||
|
||||
return
|
||||
}
|
||||
// Message was fine.
|
||||
tc.Say(target, message)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
sugar := zap.NewExample().Sugar()
|
||||
defer sugar.Sync()
|
||||
|
||||
// Message we are trying to send is empty.
|
||||
if len(message) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// Since messages starting with `.` or `/` are used for special actions
|
||||
// (ban, whisper, timeout) and so on, we place an emote infront of it so
|
||||
// the actions wouldn't execute. `!` and `$` are common bot prefixes so we
|
||||
// don't allow them either.
|
||||
if message[0] == '.' || message[0] == '/' || message[0] == '!' || message[0] == '$' {
|
||||
message = ":tf: " + message
|
||||
}
|
||||
|
||||
// check the message for bad words before we say it
|
||||
messageBanned, banReason := checkMessage(message)
|
||||
if messageBanned {
|
||||
// Bad message, replace message and log it.
|
||||
tc.Say(target, "[BANPHRASED] monkaS")
|
||||
sugar.Infow("banned message detected",
|
||||
"target channel", target,
|
||||
"message", message,
|
||||
"ban reason", banReason,
|
||||
)
|
||||
|
||||
return
|
||||
} else {
|
||||
// In case the message we are trying to send is longer than the
|
||||
// maximum allowed message length on twitch we split the message in two parts.
|
||||
// Twitch has a maximum length for messages of 510 characters so to be safe
|
||||
// we split and check at 500 characters.
|
||||
// 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)
|
||||
return
|
||||
}
|
||||
}
|
15
internal/common/uptime.go
Normal file
15
internal/common/uptime.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package common
|
||||
|
||||
import "time"
|
||||
|
||||
var (
|
||||
uptime time.Time
|
||||
)
|
||||
|
||||
func StartTime() {
|
||||
uptime = time.Now()
|
||||
}
|
||||
|
||||
func GetUptime() time.Time {
|
||||
return uptime
|
||||
}
|
11
internal/humanize/time.go
Normal file
11
internal/humanize/time.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package humanize
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
humanize "github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
func Time(t time.Time) string {
|
||||
return humanize.Time(t)
|
||||
}
|
Loading…
Reference in a new issue