mirror-nourybot/pkg/common/send.go

135 lines
3.5 KiB
Go
Raw Normal View History

2022-08-07 02:27:40 +02:00
package common
2022-06-21 00:31:17 +02:00
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
2022-08-07 02:27:40 +02:00
"log"
2022-06-21 00:31:17 +02:00
"net/http"
2022-08-07 01:34:31 +02:00
2022-08-07 02:27:40 +02:00
"github.com/gempir/go-twitch-irc/v3"
2022-08-07 03:09:09 +02:00
"go.uber.org/zap"
2022-06-21 00:31:17 +02:00
)
// 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
2022-08-07 01:34:31 +02:00
func checkMessage(text string) (bool, string) {
2022-08-07 03:09:09 +02:00
sugar := zap.NewExample().Sugar()
defer sugar.Sync()
2022-06-21 00:31:17 +02:00
// {"message": "AHAHAHAHA LUL"}
reqBody, err := json.Marshal(map[string]string{
"message": text,
})
if err != nil {
2022-08-07 03:09:09 +02:00
log.Fatal(err)
2022-06-21 00:31:17 +02:00
}
resp, err := http.Post(banPhraseUrl, "application/json", bytes.NewBuffer(reqBody))
if err != nil {
2022-08-07 02:27:40 +02:00
log.Panic(err)
2022-06-21 00:31:17 +02:00
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
2022-08-07 02:27:40 +02:00
log.Panic(err)
2022-06-21 00:31:17 +02:00
}
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.
2022-08-07 02:27:40 +02:00
func Send(target, message string, tc *twitch.Client) {
2022-08-07 03:09:09 +02:00
sugar := zap.NewExample().Sugar()
defer sugar.Sync()
2022-06-21 00:31:17 +02:00
// Message we are trying to send is empty.
if len(message) == 0 {
return
}
// Since messages starting with `.` or `/` are used for special actions
2022-08-07 04:35:03 +02:00
// (ban, whisper, timeout) and so on, we place an emote infront of it so
2022-06-21 00:31:17 +02:00
// 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
2022-08-07 01:34:31 +02:00
messageBanned, banReason := checkMessage(message)
2022-06-21 00:31:17 +02:00
if messageBanned {
// Bad message, replace message and log it.
2022-08-07 02:27:40 +02:00
tc.Say(target, "[BANPHRASED] monkaS")
2022-08-07 03:09:09 +02:00
sugar.Infow("banned message detected",
2022-08-07 04:35:03 +02:00
"target channel", target,
"message", message,
"ban reason", banReason,
)
2022-06-21 00:31:17 +02:00
return
} else {
// In case the message we are trying to send is longer than the
2022-08-07 04:35:03 +02:00
// 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
2022-06-21 00:31:17 +02:00
if len(message) > 500 {
firstMessage := message[0:499]
secondMessage := message[499:]
2022-08-07 02:27:40 +02:00
tc.Say(target, firstMessage)
tc.Say(target, secondMessage)
2022-06-21 00:31:17 +02:00
return
}
// Message was fine.
2022-08-07 02:27:40 +02:00
tc.Say(target, message)
2022-06-21 00:31:17 +02:00
return
}
}