diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index 688b58d..c81c1bb 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -377,6 +377,17 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { app.DeleteUser(cmdParams[1], message) return } + case "deletetimer": + if userLevel < 1000 { // Limit to myself for now. + return + } else if msgLen < 2 { + common.Send(target, "Not enough arguments provided.", app.TwitchClient) + return + } else { + // ()deletetimer dank + app.DeleteTimer(cmdParams[1], message) + return + } case "bttvemotes": if userLevel < 1000 { diff --git a/cmd/bot/timer.go b/cmd/bot/timer.go index 4240488..c5b489d 100644 --- a/cmd/bot/timer.go +++ b/cmd/bot/timer.go @@ -62,7 +62,7 @@ func (app *Application) InitialTimers() { app.Logger.Info(timer) - // Iterate over the slice of channels and join each. + // Iterate over each timer and add them to the scheduler. for _, v := range timer { app.Logger.Infow("Initial timers:", "Name", v.Name, @@ -80,3 +80,16 @@ func (app *Application) InitialTimers() { func (app *Application) newTimer(channel, text string) { common.Send(channel, text, app.TwitchClient) } + +// DeleteCommand takes in a name value and deletes the command from the database if it exists. +func (app *Application) DeleteTimer(name string, message twitch.PrivateMessage) { + err := app.Models.Timers.Delete(name) + if err != nil { + common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient) + app.Logger.Error(err) + return + } + + reply := fmt.Sprintf("Deleted timer %s", name) + common.Send(message.Channel, reply, app.TwitchClient) +} diff --git a/internal/data/models.go b/internal/data/models.go index 66dd08d..63d100a 100644 --- a/internal/data/models.go +++ b/internal/data/models.go @@ -41,6 +41,7 @@ type Models struct { Get(name string) (*Timer, error) Insert(timer *Timer) error GetAll() ([]*Timer, error) + Delete(name string) error } } diff --git a/internal/data/timers.go b/internal/data/timers.go index 6c40f84..1b0ed00 100644 --- a/internal/data/timers.go +++ b/internal/data/timers.go @@ -127,3 +127,31 @@ func (t TimerModel) GetAll() ([]*Timer, error) { return timers, nil } + +// Delete takes in a command name and queries the database for an entry with +// the same name and tries to delete that entry. +func (t TimerModel) Delete(name string) error { + // Prepare the statement. + query := ` + DELETE FROM timers + WHERE name = $1` + + // Execute the query returning the number of affected rows. + result, err := t.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 +} diff --git a/migrations/000004_create_timers_table.up.sql b/migrations/000004_create_timers_table.up.sql index 311abf3..699f17b 100644 --- a/migrations/000004_create_timers_table.up.sql +++ b/migrations/000004_create_timers_table.up.sql @@ -7,11 +7,6 @@ CREATE TABLE IF NOT EXISTS timers ( ); INSERT INTO timers (name,"text",channel,repeat) VALUES - ('nourylul-2m','2m timer','nourylul','2m'), - ('nourylul-3m','3 minute timer xD','nourylul','3m'), - ('nourylul-5m','5minute timer lulw','nourylul','5m'), - ('nourylul-10m','10 minute timer xD','nourylul','10m'), - ('nourylul-15m',' every 15 minutes :)','nourylul','15m'), ('nourybot-4m',' 4 minute timer xD','nourybot','4m'), ('nourybot-10m','10 minute timer xd','nourybot','10m'), ('nourybot-20m',' 20 minutes XD','nourybot','20m'),