mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
84 lines
2 KiB
Go
84 lines
2 KiB
Go
package bot
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
|
|
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"`
|
|
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 message against a banphrase api..
|
|
// returns false, "okay" if a message was allowed
|
|
// returns true, "[banphrased] monkaS" and the reason if not.
|
|
// 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,
|
|
})
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
|
|
resp, err := http.Post(banPhraseUrl, "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)
|
|
|
|
// {"phrase": "No gyazo allowed"}
|
|
reason := responseObject.BanphraseData.Name
|
|
|
|
// log.Info("Bancheck: ", responseObject.Banned)
|
|
// log.Info("Reason: ", reason)
|
|
// log.Info("Bancheck: ", responseObject.Banned)
|
|
|
|
// Bad message
|
|
if responseObject.Banned {
|
|
return true, fmt.Sprintf("Banphrased, reason: %s", 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"
|
|
}
|