mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
asd
This commit is contained in:
parent
40da7b90f8
commit
d4d15a82e3
5 changed files with 0 additions and 294 deletions
|
@ -1,49 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/gempir/go-twitch-irc/v3"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (app *application) handleCommand(message twitch.PrivateMessage) {
|
|
||||||
app.logger.Info("[COMMAND HANDLER]", message)
|
|
||||||
|
|
||||||
// commandName is the actual name of the command without the prefix.
|
|
||||||
// e.g. `()ping` would be `ping`.
|
|
||||||
commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:])
|
|
||||||
|
|
||||||
// cmdParams are additional command parameters.
|
|
||||||
// e.g. `()weather san antonio`
|
|
||||||
// cmdParam[0] is `san` and cmdParam[1] = `antonio`.
|
|
||||||
//
|
|
||||||
// Since Twitch messages are at most 500 characters I use a
|
|
||||||
// maximum count of 500+10 just to be safe.
|
|
||||||
// https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316
|
|
||||||
cmdParams := strings.SplitN(message.Message, " ", 510)
|
|
||||||
_ = cmdParams
|
|
||||||
|
|
||||||
// msgLen is the amount of words in a message without the prefix.
|
|
||||||
// Useful to check if enough cmdParams are provided.
|
|
||||||
msgLen := len(strings.SplitN(message.Message, " ", -2))
|
|
||||||
|
|
||||||
// target is the channelname the message originated from and
|
|
||||||
// where we are responding.
|
|
||||||
target := message.Channel
|
|
||||||
|
|
||||||
switch commandName {
|
|
||||||
case "":
|
|
||||||
if msgLen == 1 {
|
|
||||||
app.Send(target, "xd")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
case "echo":
|
|
||||||
if msgLen < 2 {
|
|
||||||
app.Send(target, "Not enough arguments provided.")
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
app.Send(target, message.Message[7:len(message.Message)])
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,89 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/gempir/go-twitch-irc/v3"
|
|
||||||
"github.com/joho/godotenv"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
type config struct {
|
|
||||||
env string
|
|
||||||
botUsername string
|
|
||||||
botOauth string
|
|
||||||
}
|
|
||||||
|
|
||||||
type application struct {
|
|
||||||
config config
|
|
||||||
twitchClient *twitch.Client
|
|
||||||
logger *logrus.Logger
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
var cfg config
|
|
||||||
|
|
||||||
// Parse which environment we are running in. This will decide in
|
|
||||||
// the future how many channels we join or which database we are
|
|
||||||
// connecting to for example.
|
|
||||||
flag.StringVar(&cfg.env, "env", "development", "Environment (development|string|production)")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
// Initialize a new logger we attach to our application struct.
|
|
||||||
lgr := logrus.New()
|
|
||||||
|
|
||||||
// Load the .env file and check for errors.
|
|
||||||
err := godotenv.Load()
|
|
||||||
if err != nil {
|
|
||||||
lgr.Fatal("Error loading .env file")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load bot credentials from the .env file.
|
|
||||||
cfg.botUsername = os.Getenv("BOT_USER")
|
|
||||||
cfg.botOauth = os.Getenv("BOT_OAUTH")
|
|
||||||
|
|
||||||
// Initialize a new twitch client which we attach to our
|
|
||||||
// application struct.
|
|
||||||
twitchClient := twitch.NewClient(cfg.botUsername, cfg.botOauth)
|
|
||||||
|
|
||||||
// Finally Initialize a new application instance with our
|
|
||||||
// attached methods.
|
|
||||||
app := &application{
|
|
||||||
config: cfg,
|
|
||||||
twitchClient: twitchClient,
|
|
||||||
logger: lgr,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Received a PrivateMessage (normal chat message), pass it to
|
|
||||||
// the handler who checks for further action.
|
|
||||||
app.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
|
|
||||||
app.handlePrivateMessage(message)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Received a WhisperMessage (Twitch DM), pass it to
|
|
||||||
// the handler who checks for further action.
|
|
||||||
app.twitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) {
|
|
||||||
app.handleWhisperMessage(message)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Successfully connected to Twitch so we log a message with the
|
|
||||||
// mode we are currently running in..
|
|
||||||
app.twitchClient.OnConnect(func() {
|
|
||||||
app.logger.Infof("Successfully connected to Twitch Servers in %s mode!", app.config.env)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Join test channels
|
|
||||||
app.twitchClient.Join("nourylul")
|
|
||||||
app.twitchClient.Join("nourybot")
|
|
||||||
|
|
||||||
// Say hello because we are nice :^)
|
|
||||||
app.twitchClient.Say("nourylul", "RaccAttack")
|
|
||||||
app.twitchClient.Say("nourybot", "RaccAttack")
|
|
||||||
|
|
||||||
// Connect to the twitch IRC servers.
|
|
||||||
err = app.twitchClient.Connect()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,28 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import "github.com/gempir/go-twitch-irc/v3"
|
|
||||||
|
|
||||||
func (app *application) handlePrivateMessage(message twitch.PrivateMessage) {
|
|
||||||
// roomId is the Twitch UserID of the channel the
|
|
||||||
// message originated from.
|
|
||||||
roomId := message.Tags["room-id"]
|
|
||||||
|
|
||||||
// If there is no roomId something went wrong.
|
|
||||||
if roomId == "" {
|
|
||||||
app.logger.Error("Missing room-id in message tag ", roomId)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(message.Message) >= 2 {
|
|
||||||
if message.Message[:2] == "()" {
|
|
||||||
// TODO: Command Handling
|
|
||||||
app.handleCommand(message)
|
|
||||||
// app.logger.Infof("[Command detected]: ", message.Message)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Message was no command so we just print it.
|
|
||||||
app.logger.Infof("[#%s]:%s: %s", message.Channel, message.User.DisplayName, message.Message)
|
|
||||||
|
|
||||||
}
|
|
119
cmd/bot/send.go
119
cmd/bot/send.go
|
@ -1,119 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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
|
|
||||||
func (app *application) checkMessage(text string) (bool, string) {
|
|
||||||
|
|
||||||
// {"message": "AHAHAHAHA LUL"}
|
|
||||||
reqBody, err := json.Marshal(map[string]string{
|
|
||||||
"message": text,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
app.logger.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := http.Post(banPhraseUrl, "application/json", bytes.NewBuffer(reqBody))
|
|
||||||
if err != nil {
|
|
||||||
app.logger.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
app.logger.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
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.
|
|
||||||
func (app *application) Send(target, message string) {
|
|
||||||
// Message we are trying to send is empty.
|
|
||||||
if len(message) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Since messages starting with `.` or `/` are used for special actions
|
|
||||||
// (ban, whisper, timeout) and so on, we place a emote infront of it so
|
|
||||||
// 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
|
|
||||||
messageBanned, banReason := app.checkMessage(message)
|
|
||||||
if messageBanned {
|
|
||||||
// Bad message, replace message and log it.
|
|
||||||
app.twitchClient.Say(target, "[BANPHRASED] monkaS")
|
|
||||||
app.logger.Info("Banned message detected: ", banReason)
|
|
||||||
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
// In case the message we are trying to send is longer than the
|
|
||||||
// maximum allowed message length on twitch. We split the message
|
|
||||||
// in two parts.
|
|
||||||
if len(message) > 500 {
|
|
||||||
firstMessage := message[0:499]
|
|
||||||
secondMessage := message[499:]
|
|
||||||
|
|
||||||
app.twitchClient.Say(target, firstMessage)
|
|
||||||
app.twitchClient.Say(target, secondMessage)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// Message was fine.
|
|
||||||
app.twitchClient.Say(target, message)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import "github.com/gempir/go-twitch-irc/v3"
|
|
||||||
|
|
||||||
func (app *application) handleWhisperMessage(message twitch.WhisperMessage) {
|
|
||||||
// Print the whisper message for now.
|
|
||||||
// TODO: Implement a basic whisper handler.
|
|
||||||
app.logger.Infof("[#whisper]:%s: %s", message.User.DisplayName, message.Message)
|
|
||||||
}
|
|
Loading…
Reference in a new issue