From cd79c346435bc88c2f67212d2e00df395a77fa77 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Tue, 10 Oct 2023 16:07:35 +0200 Subject: [PATCH] implement database logging for used commands --- cmd/nourybot/command.go | 12 +++++ cmd/nourybot/commands.go | 1 + cmd/nourybot/main.go | 2 + internal/data/commands_logs.go | 46 +++++++++++++++++++ internal/data/models.go | 14 ++++-- .../000006_create_command_logs_table.down.sql | 1 + .../000006_create_command_logs_table.up.sql | 16 +++++++ 7 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 internal/data/commands_logs.go create mode 100644 migrations/000006_create_command_logs_table.down.sql create mode 100644 migrations/000006_create_command_logs_table.up.sql diff --git a/cmd/nourybot/command.go b/cmd/nourybot/command.go index ad08be1..e99edba 100644 --- a/cmd/nourybot/command.go +++ b/cmd/nourybot/command.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/gempir/go-twitch-irc/v4" + "github.com/google/uuid" "github.com/lyx0/nourybot/internal/data" ) @@ -234,3 +235,14 @@ func (app *application) DeleteCommand(name string, message twitch.PrivateMessage reply := fmt.Sprintf("Deleted command %s", name) app.Send(message.Channel, reply) } + +func (app *application) LogCommand(msg twitch.PrivateMessage, commandName string, userLevel int) { + twitchLogin := msg.User.Name + twitchID := msg.User.ID + twitchMessage := msg.Message + twitchChannel := msg.Channel + identifier := uuid.NewString() + rawMsg := msg.Raw + + go app.Models.CommandsLogs.Insert(twitchLogin, twitchID, twitchChannel, twitchMessage, commandName, userLevel, identifier, rawMsg) +} diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index d9aae67..964d9a6 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -49,6 +49,7 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { "userLevel", userLevel, ) + go app.LogCommand(message, commandName, userLevel) // A `commandName` is every message starting with `()`. // Hardcoded commands have a priority over database commands. // Switch over the commandName and see if there is a hardcoded case for it. diff --git a/cmd/nourybot/main.go b/cmd/nourybot/main.go index d3d1e3a..d0a20ae 100644 --- a/cmd/nourybot/main.go +++ b/cmd/nourybot/main.go @@ -169,7 +169,9 @@ func main() { common.StartTime() app.TwitchClient.Join("nourylul") + app.TwitchClient.Join("nourybot") app.Send("nourylul", "xD!") + app.Send("nourybot", "gopherDance") // Successfully connected to Twitch app.Log.Infow("Successfully connected to Twitch Servers", diff --git a/internal/data/commands_logs.go b/internal/data/commands_logs.go new file mode 100644 index 0000000..0eb4b28 --- /dev/null +++ b/internal/data/commands_logs.go @@ -0,0 +1,46 @@ +package data + +import ( + "database/sql" +) + +type CommandsLog struct { + ID int `json:"id"` + TwitchLogin string `json:"twitch_login"` + TwitchID string `json:"twitch_id,omitempty"` + TwitchChannel string `json:"twitch_channel,omitempty"` + TwitchMessage string `json:"twitch_message,omitempty"` + CommandName string `json:"command_name,omitempty"` + UserLevel int `json:"user_level,omitempty"` + Identifier string `json:"identifier,omitempty"` + RawMessage string `json:"raw_message,omitempty"` +} + +type CommandsLogModel struct { + DB *sql.DB +} + +// Get tries to find a command in the database with the provided name. +func (c CommandsLogModel) Insert(twitchLogin, twitchId, twitchChannel, twitchMessage, commandName string, uLvl int, identifier, rawMsg string) { + query := ` + INSERT into commands_logs(twitch_login, twitch_id, twitch_channel, twitch_message, command_name, user_level, identifier, raw_message) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8) + RETURNING id; + ` + + args := []interface{}{twitchLogin, twitchId, twitchChannel, twitchMessage, commandName, uLvl, identifier, rawMsg} + + result, err := c.DB.Exec(query, args...) + if err != nil { + return + } + + rowsAffected, err := result.RowsAffected() + if err != nil { + return + } + + if rowsAffected == 0 { + return + } +} diff --git a/internal/data/models.go b/internal/data/models.go index d59658e..8cb72c7 100644 --- a/internal/data/models.go +++ b/internal/data/models.go @@ -57,14 +57,18 @@ type Models struct { Insert(twitchLogin, twitchID, twitchMessage, twitchChannel, filehoster, downloadURL, identifier string) UpdateUploadURL(identifier, uploadURL string) } + CommandsLogs interface { + Insert(twitchLogin, twitchId, twitchChannel, twitchMessage, commandName string, uLvl int, identifier, rawMsg string) + } } func NewModels(db *sql.DB) Models { return Models{ - Channels: ChannelModel{DB: db}, - Users: UserModel{DB: db}, - Commands: CommandModel{DB: db}, - Timers: TimerModel{DB: db}, - Uploads: UploadModel{DB: db}, + Channels: ChannelModel{DB: db}, + Users: UserModel{DB: db}, + Commands: CommandModel{DB: db}, + Timers: TimerModel{DB: db}, + Uploads: UploadModel{DB: db}, + CommandsLogs: CommandsLogModel{DB: db}, } } diff --git a/migrations/000006_create_command_logs_table.down.sql b/migrations/000006_create_command_logs_table.down.sql new file mode 100644 index 0000000..dddff0a --- /dev/null +++ b/migrations/000006_create_command_logs_table.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS commands_logs; diff --git a/migrations/000006_create_command_logs_table.up.sql b/migrations/000006_create_command_logs_table.up.sql new file mode 100644 index 0000000..cfe497a --- /dev/null +++ b/migrations/000006_create_command_logs_table.up.sql @@ -0,0 +1,16 @@ +CREATE TABLE IF NOT EXISTS commands_logs ( + id bigserial PRIMARY KEY, + added_at timestamp(0) with time zone NOT NULL DEFAULT NOW(), + twitch_login text NOT NULL, + twitch_id text NOT NULL, + twitch_channel text NOT NULL, + twitch_message text NOT NULL, + command_name text NOT NULL, + user_level integer NOT NULL, + identifier text NOT NULL, + raw_message text NOT NULL +); + +INSERT INTO commands_logs (added_at,twitch_login,twitch_id,twitch_channel,twitch_message,command_name,user_level,identifier,raw_message) VALUES + (NOW(),'nourylul','31437432','nourybot','()weather Vilnius','weather',1000,'8441e97b-f622-4c42-b9b1-9bf22ba0d0bd','@badge-info=;badges=moderator/1,game-developer/1;color=#00F2FB;display-name=nourylul;emotes=;first-msg=0;flags=;id=87d40f5c-8c7c-4105-9f57-b1a953bb42d0;mod=1;returning-chatter=0;room-id=596581605;subscriber=0;tmi-sent-ts=1696945359165;turbo=0;user-id=31437432;user-type=mod :nourylul!nourylul@nourylul.tmi.twitch.tv PRIVMSG #nourybot :()weather Vilnius'); +