From 801853cdb30eab1f0d897fafebd5a88a1dffc86f Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Tue, 10 Oct 2023 17:51:12 +0200 Subject: [PATCH] additionally add message context to each logged sent_message_logs in the database --- cmd/nourybot/command.go | 28 ++--- cmd/nourybot/commands.go | 10 +- cmd/nourybot/download.go | 82 +++++++------- cmd/nourybot/main.go | 6 +- cmd/nourybot/send.go | 66 +++++++++++- cmd/nourybot/timer.go | 22 ++-- cmd/nourybot/upload.go | 100 +++++++++--------- cmd/nourybot/user.go | 28 ++--- internal/data/models.go | 2 +- internal/data/sent_messages_logs.go | 21 ++-- ...007_create_sent_messages_logs_table.up.sql | 11 +- 11 files changed, 223 insertions(+), 153 deletions(-) diff --git a/cmd/nourybot/command.go b/cmd/nourybot/command.go index 544b9a9..58fbb7e 100644 --- a/cmd/nourybot/command.go +++ b/cmd/nourybot/command.go @@ -40,11 +40,11 @@ func (app *application) AddCommand(name string, message twitch.PrivateMessage) { if err != nil { reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } else { reply := fmt.Sprintf("Successfully added command: %s", name) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } } @@ -126,19 +126,19 @@ func (app *application) EditCommandLevel(name, lvl string, message twitch.Privat level, err := strconv.Atoi(lvl) if err != nil { app.Log.Error(err) - app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrCommandLevelNotInteger)) + app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrCommandLevelNotInteger), message) return } err = app.Models.Commands.SetLevel(name, level) if err != nil { - app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound)) + app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound), message) app.Log.Error(err) return } else { reply := fmt.Sprintf("Updated command %s to level %v", name, level) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } } @@ -149,12 +149,12 @@ func (app *application) EditCommandCategory(name, category string, message twitc err := app.Models.Commands.SetCategory(name, category) if err != nil { - app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound)) + app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound), message) app.Log.Error(err) return } else { reply := fmt.Sprintf("Updated command %s to category %v", name, category) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } } @@ -166,7 +166,7 @@ func (app *application) DebugCommand(name string, message twitch.PrivateMessage) cmd, err := app.Models.Commands.Get(name) if err != nil { reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } else { reply := fmt.Sprintf("id=%v\nname=%v\nlevel=%v\ncategory=%v\ntext=%v\nhelp=%v\n", @@ -182,10 +182,10 @@ func (app *application) DebugCommand(name string, message twitch.PrivateMessage) resp, err := app.uploadPaste(reply) if err != nil { app.Log.Errorln("Could not upload paste:", err) - app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %v", ErrDuringPasteUpload)) + app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %v", ErrDuringPasteUpload), message) return } - app.Send(message.Channel, resp) + app.Send(message.Channel, resp, message) //app.SendEmail(fmt.Sprintf("DEBUG for command %s", name), reply) return } @@ -213,12 +213,12 @@ func (app *application) EditCommandHelp(name string, message twitch.PrivateMessa err := app.Models.Commands.SetHelp(name, text) if err != nil { - app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound)) + app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound), message) app.Log.Error(err) return } else { reply := fmt.Sprintf("Updated help text for command %s to: %v", name, text) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } } @@ -227,13 +227,13 @@ func (app *application) EditCommandHelp(name string, message twitch.PrivateMessa func (app *application) DeleteCommand(name string, message twitch.PrivateMessage) { err := app.Models.Commands.Delete(name) if err != nil { - app.Send(message.Channel, "Something went wrong FeelsBadMan") + app.Send(message.Channel, "Something went wrong FeelsBadMan", message) app.Log.Error(err) return } reply := fmt.Sprintf("Deleted command %s", name) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) } func (app *application) LogCommand(msg twitch.PrivateMessage, commandName string, userLevel int) { diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index 964d9a6..c957551 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -145,7 +145,7 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { case "help": if msgLen > 1 { - app.commandHelp(target, cmdParams[1], message.User.Name) + app.commandHelp(target, cmdParams[1], message.User.Name, message) } case "nourybot": @@ -245,7 +245,7 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { reply = r } if reply != "" { - go app.Send(target, reply) + go app.Send(target, reply, message) return } } @@ -277,7 +277,7 @@ var helpText = map[string]string{ } // Help checks if a help text for a given command exists and replies with it. -func (app *application) commandHelp(target, name, username string) { +func (app *application) commandHelp(target, name, username string, message twitch.PrivateMessage) { // Check if the `helpText` map has an entry for `name`. If it does return it's value entry // and send that as a reply. i, ok := helpText[name] @@ -291,9 +291,9 @@ func (app *application) commandHelp(target, name, username string) { return } - app.Send(target, c) + app.Send(target, c, message) return } - app.Send(target, i) + app.Send(target, i, message) } diff --git a/cmd/nourybot/download.go b/cmd/nourybot/download.go index bd700af..092174f 100644 --- a/cmd/nourybot/download.go +++ b/cmd/nourybot/download.go @@ -22,51 +22,51 @@ func (app *application) NewDownload(destination, target, link string, msg twitch link, identifier, ) - app.Send(target, "xd") + app.Send(target, "xd", msg) switch destination { case "catbox": - app.CatboxDownload(target, link, identifier) + app.CatboxDownload(target, link, identifier, msg) case "yaf": - app.YafDownload(target, link, identifier) + app.YafDownload(target, link, identifier, msg) case "kappa": - app.KappaDownload(target, link, identifier) + app.KappaDownload(target, link, identifier, msg) case "gofile": - app.GofileDownload(target, link, identifier) + app.GofileDownload(target, link, identifier, msg) } } -func (app *application) YafDownload(target, link, identifier string) { +func (app *application) YafDownload(target, link, identifier string, msg twitch.PrivateMessage) { goutubedl.Path = "yt-dlp" - app.Send(target, "Downloading... dankCircle") + app.Send(target, "Downloading... dankCircle", msg) result, err := goutubedl.New(context.Background(), link, goutubedl.Options{}) if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } rExt := result.Info.Ext downloadResult, err := result.Download(context.Background(), "best") if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } - app.Send(target, "Downloaded.") + app.Send(target, "Downloaded.", msg) fileName := fmt.Sprintf("%s.%s", identifier, rExt) f, err := os.Create(fileName) - app.Send(target, fmt.Sprintf("Filename: %s", fileName)) + app.Send(target, fmt.Sprintf("Filename: %s", fileName), msg) if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } defer f.Close() if _, err = io.Copy(f, downloadResult); err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } @@ -76,41 +76,41 @@ func (app *application) YafDownload(target, link, identifier string) { // dl.twitchClient.Say(target, "ResidentSleeper ..") // time.Sleep(duration) - go app.NewUpload("yaf", fileName, target, identifier) + go app.NewUpload("yaf", fileName, target, identifier, msg) } -func (app *application) KappaDownload(target, link, identifier string) { +func (app *application) KappaDownload(target, link, identifier string, msg twitch.PrivateMessage) { goutubedl.Path = "yt-dlp" - app.Send(target, "Downloading... dankCircle") + app.Send(target, "Downloading... dankCircle", msg) result, err := goutubedl.New(context.Background(), link, goutubedl.Options{}) if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } rExt := result.Info.Ext downloadResult, err := result.Download(context.Background(), "best") if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } - app.Send(target, "Downloaded.") + app.Send(target, "Downloaded.", msg) fileName := fmt.Sprintf("%s.%s", identifier, rExt) f, err := os.Create(fileName) - app.Send(target, fmt.Sprintf("Filename: %s", fileName)) + app.Send(target, fmt.Sprintf("Filename: %s", fileName), msg) if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } defer f.Close() if _, err = io.Copy(f, downloadResult); err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } @@ -120,18 +120,18 @@ func (app *application) KappaDownload(target, link, identifier string) { // dl.twitchClient.Say(target, "ResidentSleeper ..") // time.Sleep(duration) - go app.NewUpload("kappa", fileName, target, identifier) + go app.NewUpload("kappa", fileName, target, identifier, msg) } -func (app *application) GofileDownload(target, link, identifier string) { +func (app *application) GofileDownload(target, link, identifier string, msg twitch.PrivateMessage) { goutubedl.Path = "yt-dlp" - app.Send(target, "Downloading... dankCircle") + app.Send(target, "Downloading... dankCircle", msg) result, err := goutubedl.New(context.Background(), link, goutubedl.Options{}) if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } safeFilename := fmt.Sprintf("download_%s", result.Info.Title) @@ -139,23 +139,23 @@ func (app *application) GofileDownload(target, link, identifier string) { downloadResult, err := result.Download(context.Background(), "best") if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } - app.Send(target, "Downloaded.") + app.Send(target, "Downloaded.", msg) fileName := fmt.Sprintf("%s.%s", safeFilename, rExt) f, err := os.Create(fileName) - app.Send(target, fmt.Sprintf("Filename: %s", fileName)) + app.Send(target, fmt.Sprintf("Filename: %s", fileName), msg) if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } defer f.Close() if _, err = io.Copy(f, downloadResult); err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } @@ -165,19 +165,19 @@ func (app *application) GofileDownload(target, link, identifier string) { // dl.twitchClient.Say(target, "ResidentSleeper ..") // time.Sleep(duration) - go app.NewUpload("gofile", fileName, target, identifier) + go app.NewUpload("gofile", fileName, target, identifier, msg) } -func (app *application) CatboxDownload(target, link, identifier string) { +func (app *application) CatboxDownload(target, link, identifier string, msg twitch.PrivateMessage) { goutubedl.Path = "yt-dlp" var fileName string - app.Send(target, "Downloading... dankCircle") + app.Send(target, "Downloading... dankCircle", msg) result, err := goutubedl.New(context.Background(), link, goutubedl.Options{}) if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } @@ -190,28 +190,28 @@ func (app *application) CatboxDownload(target, link, identifier string) { downloadResult, err := result.Download(context.Background(), "best") if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } - app.Send(target, "Downloaded.") + app.Send(target, "Downloaded.", msg) fileName = fmt.Sprintf("%s.%s", identifier, rExt) f, err := os.Create(fileName) - app.Send(target, fmt.Sprintf("Filename: %s", fileName)) + app.Send(target, fmt.Sprintf("Filename: %s", fileName), msg) if err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } defer f.Close() if _, err = io.Copy(f, downloadResult); err != nil { app.Log.Errorln(err) - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } downloadResult.Close() f.Close() - go app.NewUpload("catbox", fileName, target, identifier) + go app.NewUpload("catbox", fileName, target, identifier, msg) } diff --git a/cmd/nourybot/main.go b/cmd/nourybot/main.go index a3e0808..28d5d25 100644 --- a/cmd/nourybot/main.go +++ b/cmd/nourybot/main.go @@ -162,7 +162,7 @@ func main() { // Special rule for #pajlada. if message.Message == "!nourybot" { - app.Send(message.Channel, "Lidl Twitch bot made by @nourylul. Prefix: ()") + app.Send(message.Channel, "Lidl Twitch bot made by @nourylul. Prefix: ()", message) } } }) @@ -172,8 +172,8 @@ func main() { app.TwitchClient.Join("nourylul") app.TwitchClient.Join("nourybot") - app.Send("nourylul", "xD!") - app.Send("nourybot", "gopherDance") + app.TwitchClient.Say("nourylul", "xD!") + app.TwitchClient.Say("nourybot", "gopherDance") // Successfully connected to Twitch app.Log.Infow("Successfully connected to Twitch Servers", diff --git a/cmd/nourybot/send.go b/cmd/nourybot/send.go index fe7943c..f2b8f5c 100644 --- a/cmd/nourybot/send.go +++ b/cmd/nourybot/send.go @@ -6,7 +6,9 @@ import ( "fmt" "io" "net/http" + "strings" + "github.com/gempir/go-twitch-irc/v4" "github.com/google/uuid" ) @@ -80,14 +82,70 @@ func (app *application) checkMessage(text string) (bool, string) { // Send is used to send twitch replies and contains the necessary // safeguards and logic for that. -func (app *application) Send(target, message string) { +func (app *application) SendNoContext(target, message string) { // Message we are trying to send is empty. if len(message) == 0 { return } identifier := uuid.NewString() - go app.Models.SentMessagesLogs.Insert(target, message, identifier) + go app.Models.SentMessagesLogs.Insert(target, message, "unavailable", "unavailable", "unavailable", "unavailable", identifier, "unavailable") + + // Since messages starting with `.` or `/` are used for special actions + // (ban, whisper, timeout) and so on, we place an 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 { + // 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. + // Twitch has a maximum length for messages of 510 characters so to be safe + // we split and check at 500 characters. + // https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316 + // TODO: Make it so it splits at a space instead and not in the middle of a word. + if len(message) > 500 { + firstMessage := message[0:499] + secondMessage := message[499:] + + app.TwitchClient.Say(target, firstMessage) + app.TwitchClient.Say(target, secondMessage) + + return + } else { + // Message was fine. + go app.TwitchClient.Say(target, message) + return + } + } else { + // Bad message, replace message and log it. + app.TwitchClient.Say(target, "[BANPHRASED] monkaS") + app.Log.Infow("banned message detected", + "target channel", target, + "message", message, + "ban reason", banReason, + ) + + return + } +} + +// Send is used to send twitch replies and contains the necessary +// safeguards and logic for that. +func (app *application) Send(target, message string, msgContext twitch.PrivateMessage) { + // Message we are trying to send is empty. + if len(message) == 0 { + return + } + + commandName := strings.ToLower(strings.SplitN(msgContext.Message, " ", 3)[0][2:]) + identifier := uuid.NewString() + app.Log.Info("xd xd") + go app.Models.SentMessagesLogs.Insert(target, message, commandName, msgContext.User.Name, msgContext.User.ID, msgContext.Message, identifier, msgContext.Raw) // Since messages starting with `.` or `/` are used for special actions // (ban, whisper, timeout) and so on, we place an emote infront of it so @@ -141,7 +199,7 @@ func (app *application) SendNoBanphrase(target, message string) { } identifier := uuid.NewString() - go app.Models.SentMessagesLogs.Insert(target, message, identifier) + go app.Models.SentMessagesLogs.Insert(target, message, "unavailable", "unavailable", "unavailable", "unavailable", identifier, "unavailable") // Since messages starting with `.` or `/` are used for special actions // (ban, whisper, timeout) and so on, we place an emote infront of it so @@ -194,7 +252,7 @@ func (app *application) SendNoLimit(target, message string) { // TODO: Make it so it splits at a space instead and not in the middle of a word. // Message was fine. identifier := uuid.NewString() - go app.Models.SentMessagesLogs.Insert(target, message, identifier) + go app.Models.SentMessagesLogs.Insert(target, message, "unavailable", "unavailable", "unavailable", "unavailable", identifier, "unavailable") go app.TwitchClient.Say(target, message) return } diff --git a/cmd/nourybot/timer.go b/cmd/nourybot/timer.go index f1678c1..2d5d610 100644 --- a/cmd/nourybot/timer.go +++ b/cmd/nourybot/timer.go @@ -72,7 +72,7 @@ func (app *application) AddTimer(name, repeat string, message twitch.PrivateMess ) reply := fmt.Sprintln("Something went wrong FeelsBadMan") - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } else { // cronName is the internal, unique tag/name for the timer. @@ -84,7 +84,7 @@ func (app *application) AddTimer(name, repeat string, message twitch.PrivateMess ) reply := fmt.Sprintf("Successfully added timer %s repeating every %s", name, repeat) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } } else { @@ -93,7 +93,7 @@ func (app *application) AddTimer(name, repeat string, message twitch.PrivateMess "error", err, ) reply := "Something went wrong FeelsBadMan received wrong time format. Allowed formats: 30m, 10h, 10h30m" - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } } @@ -111,7 +111,7 @@ func (app *application) EditTimer(name, repeat string, message twitch.PrivateMes "error", err, ) reply := "Something went wrong FeelsBadMan" - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } @@ -130,7 +130,7 @@ func (app *application) EditTimer(name, repeat string, message twitch.PrivateMes ) reply := fmt.Sprintln("Something went wrong FeelsBadMan") - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } @@ -196,7 +196,7 @@ func (app *application) EditTimer(name, repeat string, message twitch.PrivateMes ) reply := fmt.Sprintln("Something went wrong FeelsBadMan") - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } else { // this is a bit scuffed. The else here is the end of a successful call. // cronName is the internal, unique tag/name for the timer. @@ -211,7 +211,7 @@ func (app *application) EditTimer(name, repeat string, message twitch.PrivateMes ) reply := fmt.Sprintf("Successfully updated timer %s", name) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } } else { @@ -220,7 +220,7 @@ func (app *application) EditTimer(name, repeat string, message twitch.PrivateMes "error", err, ) reply := "Something went wrong FeelsBadMan received wrong time format. Allowed formats: 30s, 30m, 10h, 10h30m" - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } } @@ -314,7 +314,7 @@ func (app *application) InitialTimers() { // newPrivateMessageTimer is a helper function to set timers // which trigger into sending a twitch PrivateMessage. func (app *application) newPrivateMessageTimer(channel, text string) { - app.Send(channel, text) + app.SendNoContext(channel, text) } // DeleteTimer takes in the name of a timer and tries to delete the timer from the database. @@ -346,10 +346,10 @@ func (app *application) DeleteTimer(name string, message twitch.PrivateMessage) ) reply := fmt.Sprintln("Something went wrong FeelsBadMan") - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } reply := fmt.Sprintf("Deleted timer with name %s", name) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) } diff --git a/cmd/nourybot/upload.go b/cmd/nourybot/upload.go index c7cf43a..3cf62ee 100644 --- a/cmd/nourybot/upload.go +++ b/cmd/nourybot/upload.go @@ -18,6 +18,8 @@ import ( "os" "path/filepath" "time" + + "github.com/gempir/go-twitch-irc/v4" ) const ( @@ -27,30 +29,30 @@ const ( YAF_ENDPOINT = "https://i.yaf.ee/upload" ) -func (app *application) NewUpload(destination, fileName, target, identifier string) { +func (app *application) NewUpload(destination, fileName, target, identifier string, msg twitch.PrivateMessage) { switch destination { case "catbox": - go app.CatboxUpload(target, fileName, identifier) + go app.CatboxUpload(target, fileName, identifier, msg) case "yaf": - go app.YafUpload(target, fileName, identifier) + go app.YafUpload(target, fileName, identifier, msg) case "kappa": - go app.KappaUpload(target, fileName, identifier) + go app.KappaUpload(target, fileName, identifier, msg) case "gofile": - go app.GofileUpload(target, fileName, identifier) + go app.GofileUpload(target, fileName, identifier, msg) } } -func (app *application) CatboxUpload(target, fileName, identifier string) { +func (app *application) CatboxUpload(target, fileName, identifier string, msg twitch.PrivateMessage) { defer os.Remove(fileName) file, err := os.Open(fileName) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } defer file.Close() - app.Send(target, "Uploading to catbox.moe... dankCircle") + app.Send(target, "Uploading to catbox.moe... dankCircle", msg) // if size := helper.FileSize(fileName); size > 209715200 { // return "", fmt.Errorf("file too large, size: %d MB", size/1024/1024) @@ -67,7 +69,7 @@ func (app *application) CatboxUpload(target, fileName, identifier string) { m.WriteField("time", "24h") part, err := m.CreateFormFile("fileToUpload", filepath.Base(file.Name())) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } @@ -85,26 +87,26 @@ func (app *application) CatboxUpload(target, fileName, identifier string) { resp, err := client.Do(req) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) return } reply := string(body) go app.Models.Uploads.UpdateUploadURL(identifier, reply) - app.Send(target, fmt.Sprintf("Removing file: %s", fileName)) - app.Send(target, reply) + app.Send(target, fmt.Sprintf("Removing file: %s", fileName), msg) + app.Send(target, reply, msg) } -func (app *application) GofileUpload(target, path, identifier string) { +func (app *application) GofileUpload(target, path, identifier string, msg twitch.PrivateMessage) { defer os.Remove(path) - app.Send(target, "Uploading to gofile.io... dankCircle") + app.Send(target, "Uploading to gofile.io... dankCircle", msg) pr, pw := io.Pipe() form := multipart.NewWriter(pw) @@ -127,21 +129,21 @@ func (app *application) GofileUpload(target, path, identifier string) { file, err := os.Open(path) // path to image file if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } w, err := form.CreateFormFile("file", path) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } _, err = io.Copy(w, file) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } @@ -151,7 +153,7 @@ func (app *application) GofileUpload(target, path, identifier string) { req, err := http.NewRequest(http.MethodPost, GOFILE_ENDPOINT, pr) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } @@ -162,18 +164,18 @@ func (app *application) GofileUpload(target, path, identifier string) { } resp, err := httpClient.Do(req) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) app.Log.Errorln("Error while sending HTTP request:", err) return } defer resp.Body.Close() - app.Send(target, "Uploaded PogChamp") + app.Send(target, "Uploaded PogChamp", msg) body, err := io.ReadAll(resp.Body) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) app.Log.Errorln("Error while reading response:", err) return @@ -181,7 +183,7 @@ func (app *application) GofileUpload(target, path, identifier string) { jsonResponse := new(gofileResponse) if err := json.Unmarshal(body, jsonResponse); err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) app.Log.Errorln("Error while unmarshalling JSON response:", err) return } @@ -189,13 +191,13 @@ func (app *application) GofileUpload(target, path, identifier string) { var reply = jsonResponse.Data.DownloadPage go app.Models.Uploads.UpdateUploadURL(identifier, reply) - app.Send(target, fmt.Sprintf("Removing file: %s", path)) - app.Send(target, reply) + app.Send(target, fmt.Sprintf("Removing file: %s", path), msg) + app.Send(target, reply, msg) } -func (app *application) KappaUpload(target, path, identifier string) { +func (app *application) KappaUpload(target, path, identifier string, msg twitch.PrivateMessage) { defer os.Remove(path) - app.Send(target, "Uploading to kappa.lol... dankCircle") + app.Send(target, "Uploading to kappa.lol... dankCircle", msg) pr, pw := io.Pipe() form := multipart.NewWriter(pw) @@ -208,28 +210,28 @@ func (app *application) KappaUpload(target, path, identifier string) { err := form.WriteField("name", "xd") if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } file, err := os.Open(path) // path to image file if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } w, err := form.CreateFormFile("file", path) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } _, err = io.Copy(w, file) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } @@ -239,7 +241,7 @@ func (app *application) KappaUpload(target, path, identifier string) { req, err := http.NewRequest(http.MethodPost, KAPPA_ENDPOINT, pr) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } @@ -250,18 +252,18 @@ func (app *application) KappaUpload(target, path, identifier string) { } resp, err := httpClient.Do(req) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) app.Log.Errorln("Error while sending HTTP request:", err) return } defer resp.Body.Close() - app.Send(target, "Uploaded PogChamp") + app.Send(target, "Uploaded PogChamp", msg) body, err := io.ReadAll(resp.Body) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) app.Log.Errorln("Error while reading response:", err) return @@ -269,7 +271,7 @@ func (app *application) KappaUpload(target, path, identifier string) { jsonResponse := new(kappaResponse) if err := json.Unmarshal(body, jsonResponse); err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) app.Log.Errorln("Error while unmarshalling JSON response:", err) return } @@ -277,13 +279,13 @@ func (app *application) KappaUpload(target, path, identifier string) { var reply = jsonResponse.Link go app.Models.Uploads.UpdateUploadURL(identifier, reply) - app.Send(target, fmt.Sprintf("Removing file: %s", path)) - app.Send(target, reply) + app.Send(target, fmt.Sprintf("Removing file: %s", path), msg) + app.Send(target, reply, msg) } -func (app *application) YafUpload(target, path, identifier string) { +func (app *application) YafUpload(target, path, identifier string, msg twitch.PrivateMessage) { defer os.Remove(path) - app.Send(target, "Uploading to yaf.ee... dankCircle") + app.Send(target, "Uploading to yaf.ee... dankCircle", msg) pr, pw := io.Pipe() form := multipart.NewWriter(pw) @@ -292,28 +294,28 @@ func (app *application) YafUpload(target, path, identifier string) { err := form.WriteField("name", "xd") if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } file, err := os.Open(path) // path to image file if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } w, err := form.CreateFormFile("file", path) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } _, err = io.Copy(w, file) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } @@ -323,7 +325,7 @@ func (app *application) YafUpload(target, path, identifier string) { req, err := http.NewRequest(http.MethodPost, YAF_ENDPOINT, pr) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) return } @@ -334,7 +336,7 @@ func (app *application) YafUpload(target, path, identifier string) { } resp, err := httpClient.Do(req) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) app.Log.Errorln("Error while sending HTTP request:", err) @@ -344,7 +346,7 @@ func (app *application) YafUpload(target, path, identifier string) { body, err := io.ReadAll(resp.Body) if err != nil { - app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err)) + app.Send(target, fmt.Sprintf("Something went wrong FeelsBadMan: %q", err), msg) os.Remove(path) app.Log.Errorln("Error while reading response:", err) return @@ -353,6 +355,6 @@ func (app *application) YafUpload(target, path, identifier string) { var reply = string(body[:]) go app.Models.Uploads.UpdateUploadURL(identifier, reply) - app.Send(target, fmt.Sprintf("Removing file: %s", path)) - app.Send(target, reply) + app.Send(target, fmt.Sprintf("Removing file: %s", path), msg) + app.Send(target, reply, msg) } diff --git a/cmd/nourybot/user.go b/cmd/nourybot/user.go index ecfd832..29d7d5e 100644 --- a/cmd/nourybot/user.go +++ b/cmd/nourybot/user.go @@ -27,7 +27,7 @@ func (app *application) DebugUser(login string, message twitch.PrivateMessage) { if err != nil { reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } else { // subject := fmt.Sprintf("DEBUG for user %v", login) @@ -42,10 +42,10 @@ func (app *application) DebugUser(login string, message twitch.PrivateMessage) { resp, err := app.uploadPaste(body) if err != nil { app.Log.Errorln("Could not upload paste:", err) - app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %v", ErrDuringPasteUpload)) + app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %v", ErrDuringPasteUpload), message) return } - app.Send(message.Channel, resp) + app.Send(message.Channel, resp, message) // app.SendEmail(subject, body) return } @@ -56,13 +56,13 @@ func (app *application) DebugUser(login string, message twitch.PrivateMessage) { func (app *application) DeleteUser(login string, message twitch.PrivateMessage) { err := app.Models.Users.Delete(login) if err != nil { - app.Send(message.Channel, "Something went wrong FeelsBadMan") + app.Send(message.Channel, "Something went wrong FeelsBadMan", message) app.Log.Error(err) return } reply := fmt.Sprintf("Deleted user %s", login) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) } // EditUserLevel tries to update the database record for the supplied @@ -73,18 +73,18 @@ func (app *application) EditUserLevel(login, lvl string, message twitch.PrivateM level, err := strconv.Atoi(lvl) if err != nil { app.Log.Error(err) - app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrUserLevelNotInteger)) + app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrUserLevelNotInteger), message) return } err = app.Models.Users.SetLevel(login, level) if err != nil { - app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound)) + app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound), message) app.Log.Error(err) return } else { reply := fmt.Sprintf("Updated user %s to level %v", login, level) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } } @@ -106,12 +106,12 @@ func (app *application) SetUserLocation(message twitch.PrivateMessage) { err := app.Models.Users.SetLocation(twitchId, location) if err != nil { - app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound)) + app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound), message) app.Log.Error(err) return } else { reply := fmt.Sprintf("Successfully set your location to %v", location) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } } @@ -123,12 +123,12 @@ func (app *application) SetUserLastFM(lastfmUser string, message twitch.PrivateM err := app.Models.Users.SetLastFM(login, lastfmUser) if err != nil { - app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound)) + app.Send(message.Channel, fmt.Sprintf("Something went wrong FeelsBadMan %s", ErrRecordNotFound), message) app.Log.Error(err) return } else { reply := fmt.Sprintf("Successfully set your lastfm username to %v", lastfmUser) - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } } @@ -157,12 +157,12 @@ func (app *application) UserCheckWeather(message twitch.PrivateMessage) { "twitchId:", twitchId, ) reply := "No location for your account set in my database. Use ()set location to register. Otherwise use ()weather without registering." - app.Send(message.Channel, reply) + app.Send(message.Channel, reply, message) return } reply, _ := commands.Weather(location) - app.Send(target, reply) + app.Send(target, reply, message) } func (app *application) UserCheckLastFM(message twitch.PrivateMessage) string { diff --git a/internal/data/models.go b/internal/data/models.go index 63bdffe..8293a4a 100644 --- a/internal/data/models.go +++ b/internal/data/models.go @@ -61,7 +61,7 @@ type Models struct { Insert(twitchLogin, twitchId, twitchChannel, twitchMessage, commandName string, uLvl int, identifier, rawMsg string) } SentMessagesLogs interface { - Insert(twitchChannel, twitchMessage, identifier string) + Insert(twitchChannel, twitchMessage, ctxCommandName, ctxUser, ctxUserID, ctxMsg, identifier, ctxRaw string) } } diff --git a/internal/data/sent_messages_logs.go b/internal/data/sent_messages_logs.go index efe844a..a0c0d2c 100644 --- a/internal/data/sent_messages_logs.go +++ b/internal/data/sent_messages_logs.go @@ -5,10 +5,15 @@ import ( ) type SentMessagesLog struct { - ID int `json:"id"` - TwitchChannel string `json:"twitch_channel,omitempty"` - TwitchMessage string `json:"twitch_message,omitempty"` - Identifier string `json:"identifier,omitempty"` + ID int `json:"id"` + TwitchChannel string `json:"twitch_channel,omitempty"` + TwitchMessage string `json:"twitch_message,omitempty"` + ContextCommandName string `json:"context_command_name"` + ContextUsername string `json:"context_user"` + ContextMessage string `json:"context_message"` + ContextUserID string `json:"context_user_id"` + Identifier string `json:"identifier,omitempty"` + ContextRawMsg string `json:"context_raw"` } type SentMessagesLogModel struct { @@ -16,14 +21,14 @@ type SentMessagesLogModel struct { } // Get tries to find a command in the database with the provided name. -func (s SentMessagesLogModel) Insert(twitchChannel, twitchMessage, identifier string) { +func (s SentMessagesLogModel) Insert(twitchChannel, twitchMessage, ctxCommandName, ctxUser, ctxUserID, ctxMsg, identifier, ctxRaw string) { query := ` - INSERT into sent_messages_logs(twitch_channel, twitch_message, identifier) - VALUES ($1, $2, $3) + INSERT into sent_messages_logs(twitch_channel, twitch_message, context_command_name, context_username, context_user_id, context_message, identifier, context_raw) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING id; ` - args := []interface{}{twitchChannel, twitchMessage, identifier} + args := []interface{}{twitchChannel, twitchMessage, ctxCommandName, ctxUser, ctxUserID, ctxMsg, identifier, ctxRaw} result, err := s.DB.Exec(query, args...) if err != nil { diff --git a/migrations/000007_create_sent_messages_logs_table.up.sql b/migrations/000007_create_sent_messages_logs_table.up.sql index afe0aa7..d671c7f 100644 --- a/migrations/000007_create_sent_messages_logs_table.up.sql +++ b/migrations/000007_create_sent_messages_logs_table.up.sql @@ -3,8 +3,13 @@ CREATE TABLE IF NOT EXISTS sent_messages_logs ( added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(), twitch_channel text NOT NULL, twitch_message text NOT NULL, - identifier text NOT NULL + context_command_name text, + context_username text, + context_message text, + context_user_id text, + identifier text, + context_raw text ); -INSERT INTO sent_messages_logs (added_at,twitch_channel,twitch_message,identifier) VALUES - (NOW(),'nourybot','Weather for Vilnius, LT: Feels like: 9.3°C. Currently 10.85°C with a high of 12.07°C and a low of 10.49°C, humidity: 50%, wind: 2.57m/s.','04fbd9c0-47da-466f-b966-44d1d04de11c'); +INSERT INTO sent_messages_logs (added_at,twitch_channel,twitch_message,context_command_name,context_username,context_message,context_user_id,identifier,context_raw) VALUES + (NOW(),'nourybot','Weather for Vilnius, LT: Feels like: 8.07°C. Currently 8.65°C with a high of 9.29°C and a low of 8.49°C, humidity: 66%, wind: 1.54m/s.','weather','nourylul','()weather Vilnius','31437432','654f9761-b2d4-4975-a4fd-84c6ec7f2eb8','@badge-info=;badges=moderator/1,game-developer/1;color=#00F2FB;display-name=nourylul;emotes=;first-msg=0;flags=;id=357d94a4-024e-49ea-ab3d-d97286cd0492;mod=1;returning-chatter=0;room-id=596581605;subscriber=0;tmi-sent-ts=1696952295788;turbo=0;user-id=31437432;user-type=mod :nourylul!nourylul@nourylul.tmi.twitch.tv PRIVMSG #nourybot :()weather Vilnius');