add some comments

This commit is contained in:
lyx0 2021-10-20 22:59:05 +02:00
parent 71a1f854cb
commit bf8f7943fb
3 changed files with 29 additions and 10 deletions

View file

@ -10,12 +10,16 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
// banphraseResponse is the data we receive back from
// the banphrase API
type banphraseResponse struct { type banphraseResponse struct {
Banned bool `json:"banned"` Banned bool `json:"banned"`
InputMessage string `json:"input_message"` InputMessage string `json:"input_message"`
BanphraseData banphraseData `json:"banphrase_data"` BanphraseData banphraseData `json:"banphrase_data"`
} }
// banphraseData contains details about why a message
// was banphrased.
type banphraseData struct { type banphraseData struct {
Id int `json:"id"` Id int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
@ -30,10 +34,12 @@ type banphraseData struct {
// false, "okay" // false, "okay"
// If a message is not allowed it returns: // If a message is not allowed it returns:
// true, "[banphrased] monkaS" // true, "[banphrased] monkaS"
// More information:
// https://gist.github.com/pajlada/57464e519ba8d195a97ddcd0755f9715
func CheckMessage(text string) (bool, string) { func CheckMessage(text string) (bool, string) {
log.Info("fn CheckMessage") log.Info("fn CheckMessage")
// {"message": "AHAHAHAHA LUL"}
reqBody, err := json.Marshal(map[string]string{ reqBody, err := json.Marshal(map[string]string{
"message": text, "message": text,
}) })
@ -41,8 +47,6 @@ func CheckMessage(text string) (bool, string) {
log.Error(err) log.Error(err)
} }
log.Info(reqBody)
resp, err := http.Post("https://pajlada.pajbot.com/api/v1/banphrases/test", "application/json", bytes.NewBuffer(reqBody)) resp, err := http.Post("https://pajlada.pajbot.com/api/v1/banphrases/test", "application/json", bytes.NewBuffer(reqBody))
if err != nil { if err != nil {
log.Error(err) log.Error(err)
@ -58,17 +62,21 @@ func CheckMessage(text string) (bool, string) {
var responseObject banphraseResponse var responseObject banphraseResponse
json.Unmarshal(body, &responseObject) json.Unmarshal(body, &responseObject)
// {"phrase": "No gyazo allowed"}
reason := responseObject.BanphraseData.Name reason := responseObject.BanphraseData.Name
log.Info("Bancheck: ", responseObject.Banned) // log.Info("Bancheck: ", responseObject.Banned)
log.Info("Reason: ", reason) // log.Info("Reason: ", reason)
// log.Info("Bancheck: ", responseObject.Banned)
log.Info("Bancheck: ", responseObject.Banned) // Bad message
if responseObject.Banned { if responseObject.Banned {
return true, fmt.Sprintf("Banphrased, reason: %s", reason) return true, fmt.Sprintf("Banphrased, reason: %s", reason)
} else { } else {
// Good message
return false, "okay" return false, "okay"
} }
return true, "couldnt check" // Couldn't contact api so assume it was a bad message
return true, "Banphrase API couldn't be reached monkaS"
} }

View file

@ -15,6 +15,9 @@ type Channel struct {
Name string Name string
} }
// Send checks the message against a banphrase api
// and also splits the message into two if the message
// is too long for a single twitch chat message.
func (b *Bot) Send(target, text string) { func (b *Bot) Send(target, text string) {
if len(text) == 0 { if len(text) == 0 {
return return
@ -23,15 +26,22 @@ func (b *Bot) Send(target, text string) {
// if text[0] == '.' || text[0] == '/' { // if text[0] == '.' || text[0] == '/' {
// text = ". " + text // text = ". " + text
// } // }
banned, reason := CheckMessage(text)
if banned { // check the message for bad words before we say it
b.TwitchClient.Say(target, reason) messageBanned, banReason := CheckMessage(text)
if messageBanned {
// Bad message, replace message with a small
// notice on why it's banned.
b.TwitchClient.Say(target, banReason)
return return
} else { } else {
// Message was okay.
b.TwitchClient.Say(target, text) b.TwitchClient.Say(target, text)
return return
} }
// If a message is too long for a single twitch
// message, split it into two messages.
if len(text) > 500 { if len(text) > 500 {
firstMessage := text[0:499] firstMessage := text[0:499]
secondMessage := text[499:] secondMessage := text[499:]

View file

@ -33,6 +33,7 @@ func main() {
return return
} }
// Forward the message for further processing.
handlers.PrivateMessage(message, nb) handlers.PrivateMessage(message, nb)
}) })