From 6029d495b265fce4e9c38f94f52344b6fd2aa2ec Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Mon, 22 Aug 2022 02:13:07 +0200 Subject: [PATCH] add a timer into the database --- cmd/api/command.go | 2 +- cmd/bot/commands.go | 11 ++++++++++ cmd/bot/timer.go | 50 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/cmd/api/command.go b/cmd/api/command.go index a6e8fae..0f67c61 100644 --- a/cmd/api/command.go +++ b/cmd/api/command.go @@ -25,8 +25,8 @@ func (app *application) showCommandHandler(w http.ResponseWriter, r *http.Reques return default: app.serverErrorResponse(w, r, err) + return } - return } app.Logger.Infow("GET Command", diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index 93b7238..688b58d 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -286,6 +286,17 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { app.AddCommand(cmdParams[1], message) return } + case "addtimer": + if userLevel < 1000 { + return + } else if msgLen < 4 { + common.Send(target, "Not enough arguments provided.", app.TwitchClient) + return + } else { + // ()addtimer gfuel 5m sponsor XD xD + app.AddTimer(cmdParams[1], message) + return + } case "adduser": if userLevel < 1000 { return diff --git a/cmd/bot/timer.go b/cmd/bot/timer.go index 06ab7d0..2c3d605 100644 --- a/cmd/bot/timer.go +++ b/cmd/bot/timer.go @@ -1 +1,51 @@ package main + +import ( + "fmt" + "strings" + + "github.com/gempir/go-twitch-irc/v3" + "github.com/lyx0/nourybot/internal/data" + "github.com/lyx0/nourybot/pkg/common" +) + +// AddCommand takes in a name parameter and a twitch.PrivateMessage. It slices the +// twitch.PrivateMessage after the name parameter and adds everything after to a text +// value. Then it calls the app.Models.Commands.Insert method with both name, and text +// values adding them to the database. +func (app *Application) AddTimer(name string, message twitch.PrivateMessage) { + // prefixLength is the length of `()addtimer` plus +2 (for the space and zero based) + cmdParams := strings.SplitN(message.Message, " ", 500) + prefixLength := 12 + repeat := cmdParams[2] + + // Split the twitch message at the length of the prefix + the length of the name of the command. + // prefixLength |name| text + // 0123456789012|4567| + // e.g. ()addcommand dank FeelsDankMan + // | part1 snip ^ part2 | + text := message.Message[prefixLength+len(name)+len(cmdParams[2]) : len(message.Message)] + + // ()addtimer gfuel 5m Yo buy my cool gfuel cause its cool n shit + // | | name | + timer := &data.Timer{ + Name: name, + Text: text, + Channel: message.Channel, + Repeat: repeat, + } + + app.Logger.Infow("timer", timer) + err := app.Models.Timers.Insert(timer) + + 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 timer %s repeating every %s", name, repeat) + common.Send(message.Channel, reply, app.TwitchClient) + return + } +}