add lidl banphrase

This commit is contained in:
lyx0 2021-10-20 22:13:19 +02:00
parent 8359a525c3
commit 61fc2fb652
3 changed files with 77 additions and 3 deletions

2
.gitignore vendored
View file

@ -15,5 +15,5 @@
# vendor/
.env
Nourybot
nourybot
cmd

68
cmd/bot/banphrase.go Normal file
View file

@ -0,0 +1,68 @@
package bot
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
log "github.com/sirupsen/logrus"
)
type banphraseResponse struct {
Banned bool `json:"banned"`
InputMessage string `json:"input_message"`
BanphraseData banphraseData `json:"banphrase_data"`
}
type banphraseData struct {
Id int `json:"id"`
Name string `json:"name"`
Phrase string `json:"phrase"`
Length int `json:"length"`
Permanent bool `json:"permanent"`
}
// CheckMessage checks if a message contains
// banphrased content.
// If a message is allowed it returns
// false, "okay"
// If a message is not allowed it returns:
// true, "[banphrased] monkaS"
func CheckMessage(text string) (bool, string) {
log.Info("fn CheckMessage")
reqBody, err := json.Marshal(map[string]string{
"message": text,
})
if err != nil {
log.Error(err)
}
log.Info(reqBody)
resp, err := http.Post("https://forsen.tv/api/v1/banphrases/test", "application/json", bytes.NewBuffer(reqBody))
if err != nil {
log.Error(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error(err)
}
var responseObject banphraseResponse
json.Unmarshal(body, &responseObject)
log.Info("Bancheck: ", responseObject.Banned)
if responseObject.Banned {
return true, "[banphrased] monkaS"
} else {
return false, "okay"
}
return true, "couldnt check"
}

View file

@ -32,5 +32,11 @@ func (b *Bot) Send(target, text string) {
return
}
b.TwitchClient.Say(target, text)
banned, reason := CheckMessage(text)
if banned {
b.TwitchClient.Say(target, reason)
} else {
b.TwitchClient.Say(target, text)
}
}