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"
)
// 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"`
@ -30,10 +34,12 @@ type banphraseData struct {
// false, "okay"
// If a message is not allowed it returns:
// true, "[banphrased] monkaS"
// More information:
// https://gist.github.com/pajlada/57464e519ba8d195a97ddcd0755f9715
func CheckMessage(text string) (bool, string) {
log.Info("fn CheckMessage")
// {"message": "AHAHAHAHA LUL"}
reqBody, err := json.Marshal(map[string]string{
"message": text,
})
@ -41,8 +47,6 @@ func CheckMessage(text string) (bool, string) {
log.Error(err)
}
log.Info(reqBody)
resp, err := http.Post("https://pajlada.pajbot.com/api/v1/banphrases/test", "application/json", bytes.NewBuffer(reqBody))
if err != nil {
log.Error(err)
@ -58,17 +62,21 @@ func CheckMessage(text string) (bool, string) {
var responseObject banphraseResponse
json.Unmarshal(body, &responseObject)
// {"phrase": "No gyazo allowed"}
reason := responseObject.BanphraseData.Name
log.Info("Bancheck: ", responseObject.Banned)
log.Info("Reason: ", reason)
// log.Info("Bancheck: ", responseObject.Banned)
// log.Info("Reason: ", reason)
// log.Info("Bancheck: ", responseObject.Banned)
log.Info("Bancheck: ", responseObject.Banned)
// Bad message
if responseObject.Banned {
return true, fmt.Sprintf("Banphrased, reason: %s", reason)
} else {
// Good message
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
}
// 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) {
if len(text) == 0 {
return
@ -23,15 +26,22 @@ func (b *Bot) Send(target, text string) {
// if text[0] == '.' || text[0] == '/' {
// text = ". " + text
// }
banned, reason := CheckMessage(text)
if banned {
b.TwitchClient.Say(target, reason)
// check the message for bad words before we say it
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
} else {
// Message was okay.
b.TwitchClient.Say(target, text)
return
}
// If a message is too long for a single twitch
// message, split it into two messages.
if len(text) > 500 {
firstMessage := text[0:499]
secondMessage := text[499:]

View file

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