mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
implement adding commands to the database
This commit is contained in:
parent
b625d46258
commit
841025cac0
|
@ -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() {
|
||||
|
|
|
@ -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
|
||||
|
|
29
cmd/bot/commands.go
Normal file
29
cmd/bot/commands.go
Normal file
|
@ -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
|
||||
}
|
||||
}
|
99
internal/data/commands.go
Normal file
99
internal/data/commands.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue