From d4d15a82e32533b1518ef5d0231468d326d479f6 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Tue, 21 Jun 2022 00:08:37 +0200 Subject: [PATCH] asd --- cmd/bot/command.go | 49 ---------------- cmd/bot/main.go | 89 ---------------------------- cmd/bot/privatemessage.go | 28 --------- cmd/bot/send.go | 119 -------------------------------------- cmd/bot/whispermessage.go | 9 --- 5 files changed, 294 deletions(-) delete mode 100644 cmd/bot/command.go delete mode 100644 cmd/bot/main.go delete mode 100644 cmd/bot/privatemessage.go delete mode 100644 cmd/bot/send.go delete mode 100644 cmd/bot/whispermessage.go diff --git a/cmd/bot/command.go b/cmd/bot/command.go deleted file mode 100644 index 57f49b7..0000000 --- a/cmd/bot/command.go +++ /dev/null @@ -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 - } - } -} diff --git a/cmd/bot/main.go b/cmd/bot/main.go deleted file mode 100644 index 3d78e97..0000000 --- a/cmd/bot/main.go +++ /dev/null @@ -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) - } -} diff --git a/cmd/bot/privatemessage.go b/cmd/bot/privatemessage.go deleted file mode 100644 index 9be2caa..0000000 --- a/cmd/bot/privatemessage.go +++ /dev/null @@ -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) - -} diff --git a/cmd/bot/send.go b/cmd/bot/send.go deleted file mode 100644 index c02dad5..0000000 --- a/cmd/bot/send.go +++ /dev/null @@ -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 - } -} diff --git a/cmd/bot/whispermessage.go b/cmd/bot/whispermessage.go deleted file mode 100644 index f491ecd..0000000 --- a/cmd/bot/whispermessage.go +++ /dev/null @@ -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) -}