mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
implement database logging for used commands
This commit is contained in:
parent
11f084978d
commit
cd79c34643
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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",
|
||||
|
|
46
internal/data/commands_logs.go
Normal file
46
internal/data/commands_logs.go
Normal file
|
@ -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
|
||||
}
|
||||
}
|
|
@ -57,6 +57,9 @@ 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 {
|
||||
|
@ -66,5 +69,6 @@ func NewModels(db *sql.DB) Models {
|
|||
Commands: CommandModel{DB: db},
|
||||
Timers: TimerModel{DB: db},
|
||||
Uploads: UploadModel{DB: db},
|
||||
CommandsLogs: CommandsLogModel{DB: db},
|
||||
}
|
||||
}
|
||||
|
|
1
migrations/000006_create_command_logs_table.down.sql
Normal file
1
migrations/000006_create_command_logs_table.down.sql
Normal file
|
@ -0,0 +1 @@
|
|||
DROP TABLE IF EXISTS commands_logs;
|
16
migrations/000006_create_command_logs_table.up.sql
Normal file
16
migrations/000006_create_command_logs_table.up.sql
Normal file
|
@ -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');
|
||||
|
Loading…
Reference in a new issue