From 841025cac0c78b8a9998fde50e201a2640a6e02d Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Thu, 11 Aug 2022 01:18:51 +0200 Subject: [PATCH] implement adding commands to the database --- cmd/bot/channel.go | 1 - cmd/bot/command.go | 11 +++++ cmd/bot/commands.go | 29 ++++++++++++ internal/data/commands.go | 99 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 cmd/bot/commands.go create mode 100644 internal/data/commands.go diff --git a/cmd/bot/channel.go b/cmd/bot/channel.go index 965f2bb..0bace24 100644 --- a/cmd/bot/channel.go +++ b/cmd/bot/channel.go @@ -32,7 +32,6 @@ func (app *Application) AddChannel(login string, message twitch.PrivateMessage) common.Send(message.Channel, reply, app.TwitchClient) return } - } func (app *Application) GetAllChannels() { diff --git a/cmd/bot/command.go b/cmd/bot/command.go index a8cc6e5..59c1567 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -73,6 +73,17 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { app.AddChannel(cmdParams[1], message) return } + case "addcommand": + if userLevel < 1000 { // Limit to myself for now. + return + } else if msgLen < 3 { + common.Send(target, "Not enough arguments provided.", app.TwitchClient) + return + } else { + // ()addcommand dank FeelsDankMan xD + app.AddCommand(cmdParams[1], message) + return + } case "adduser": if userLevel < 1000 { // Limit to myself for now. return diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go new file mode 100644 index 0000000..cc8030e --- /dev/null +++ b/cmd/bot/commands.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + + "github.com/gempir/go-twitch-irc/v3" + "github.com/lyx0/nourybot/pkg/common" +) + +func (app *Application) AddCommand(name string, message twitch.PrivateMessage) { + // text1 := strings.Split(message.Message, name) + + text := message.Message[14+len(name) : len(message.Message)] + app.Logger.Infow("Message splits", + "Command Name:", name, + "Command Text:", text) + err := app.Models.Commands.Insert(name, text) + + if err != nil { + reply := fmt.Sprintf("Something went wrong FeelsBadMan %s", err) + common.Send(message.Channel, reply, app.TwitchClient) + return + } else { + + reply := fmt.Sprintf("Successfully added command: %s", name) + common.Send(message.Channel, reply, app.TwitchClient) + return + } +} diff --git a/internal/data/commands.go b/internal/data/commands.go new file mode 100644 index 0000000..41666c4 --- /dev/null +++ b/internal/data/commands.go @@ -0,0 +1,99 @@ +package data + +import ( + "database/sql" + "errors" +) + +type Command struct { + ID int `json:"id"` + Name string `json:"name"` + Text string `json:"text"` + Permission int `json:"permission"` +} + +type CommandModel struct { + DB *sql.DB +} + +func (c CommandModel) Get(name string) (*Command, error) { + query := ` + SELECT id, name, text, permission + FROM commands + WHERE name = $1` + + var command Command + + err := c.DB.QueryRow(query, name).Scan( + &command.ID, + &command.Name, + &command.Text, + &command.Permission, + ) + + if err != nil { + switch { + case errors.Is(err, sql.ErrNoRows): + return nil, ErrRecordNotFound + default: + return nil, err + } + } + + return &command, nil +} + +func (c CommandModel) Insert(name, text string) error { + perms := 0 + query := ` + INSERT into commands(name, text, permission) + VALUES ($1, $2, $3) + ON CONFLICT (name) + DO NOTHING + RETURNING id; + ` + + args := []interface{}{name, text, perms} + + result, err := c.DB.Exec(query, args...) + if err != nil { + return err + } + + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + + if rowsAffected == 0 { + return ErrRecordAlreadyExists + } + + return nil +} + +func (c CommandModel) Delete(name string) error { + // Prepare the statement. + query := ` + DELETE FROM commands + WHERE name = $1` + + // Execute the query returning the number of affected rows. + result, err := c.DB.Exec(query, name) + if err != nil { + return err + } + + // Check how many rows were affected. + rowsAffected, err := result.RowsAffected() + if err != nil { + return err + } + + // We want atleast 1, if it is 0 the entry did not exist. + if rowsAffected == 0 { + return ErrRecordNotFound + } + + return nil +}