actually delete and stop a timer. to test ()addtimer xD 5s xD and then ()deletetimer xD

This commit is contained in:
lyx0 2022-08-25 04:42:37 +02:00
parent b55b3ed0cb
commit f7c3d4659c
4 changed files with 42 additions and 13 deletions

View file

@ -8,7 +8,7 @@ import (
"time" "time"
"github.com/gempir/go-twitch-irc/v3" "github.com/gempir/go-twitch-irc/v3"
"github.com/go-co-op/gocron" "github.com/jakecoffman/cron"
"github.com/joho/godotenv" "github.com/joho/godotenv"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"github.com/lyx0/nourybot/internal/data" "github.com/lyx0/nourybot/internal/data"
@ -34,7 +34,7 @@ type Application struct {
Logger *zap.SugaredLogger Logger *zap.SugaredLogger
Db *sql.DB Db *sql.DB
Models data.Models Models data.Models
Scheduler *gocron.Scheduler Scheduler *cron.Cron
} }
func main() { func main() {
@ -71,7 +71,8 @@ func main() {
sugar.Fatal(err) sugar.Fatal(err)
} }
s := gocron.NewScheduler(time.UTC) c := cron.New()
//s := gocron.NewScheduler(time.UTC)
// Initialize Application // Initialize Application
app := &Application{ app := &Application{
@ -79,7 +80,7 @@ func main() {
Logger: sugar, Logger: sugar,
Db: db, Db: db,
Models: data.NewModels(db), Models: data.NewModels(db),
Scheduler: s, Scheduler: c,
} }
// Received a PrivateMessage (normal chat message). // Received a PrivateMessage (normal chat message).
@ -148,7 +149,7 @@ func main() {
common.Send("uudelleenkytkeytynyt", "PepeS", app.TwitchClient) common.Send("uudelleenkytkeytynyt", "PepeS", app.TwitchClient)
}) })
s.StartAsync() c.Start()
// Actually connect to chat. // Actually connect to chat.
err = app.TwitchClient.Connect() err = app.TwitchClient.Connect()

View file

@ -3,7 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/gempir/go-twitch-irc/v3" "github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/internal/data" "github.com/lyx0/nourybot/internal/data"
@ -43,8 +42,14 @@ func (app *Application) AddTimer(name string, message twitch.PrivateMessage) {
common.Send(message.Channel, reply, app.TwitchClient) common.Send(message.Channel, reply, app.TwitchClient)
return return
} else { } else {
app.Scheduler.Tag("pipelines", fmt.Sprintf("%s-%s", message.Channel, name)).Every(repeat).StartAt(time.Now()).Do(app.newTimer, message.Channel, text) // app.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
reply := fmt.Sprintf("Successfully added timer %s repeating every %s", name, repeat) // app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), (func() {
// app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func(message.Channel, text) { app.newTimer(message.Channel, text) }))
cronName := fmt.Sprintf("%s-%s", message.Channel, name)
app.Scheduler.AddFunc(fmt.Sprintf("@every %s", repeat), func() { app.newTimer(message.Channel, text) }, cronName)
//app.Scheduler.Tag(fmt.Sprintf("%s-%s", message.Channel, name)).Every(repeat).StartAt(time.Now()).Do(app.newTimer, message.Channel, text
reply := fmt.Sprintf("Successfully added timer %s repeating every %s (ID: xd)", name, repeat)
common.Send(message.Channel, reply, app.TwitchClient) common.Send(message.Channel, reply, app.TwitchClient)
return return
} }
@ -64,17 +69,26 @@ func (app *Application) InitialTimers() {
// Iterate over each timer and add them to the scheduler. // Iterate over each timer and add them to the scheduler.
for _, v := range timer { for _, v := range timer {
cronName := fmt.Sprintf("%s-%s", v.Channel, v.Name)
app.Scheduler.AddFunc(fmt.Sprintf("@every %s", v.Repeat), func() { app.newTimer(v.Channel, v.Text) }, cronName)
app.Logger.Infow("Initial timers:", app.Logger.Infow("Initial timers:",
"Name", v.Name, "Name", v.Name,
"Channel", v.Channel, "Channel", v.Channel,
"Text", v.Text, "Text", v.Text,
"Repeat", v.Repeat, "Repeat", v.Repeat,
"V", v, "V", v,
"cronName", cronName,
) )
app.Scheduler.Tag("pipelines", fmt.Sprintf("%s-%s", v.Channel, v.Name)).Every(v.Repeat).StartAt(time.Now()).Do(app.newTimer, v.Channel, v.Text) // app.Scheduler.Tag(fmt.Sprintf("%s-%s", v.Channel, v.Name)).Every(v.Repeat).StartAt(time.Now()).Do(app.newTimer, v.Channel, v.Text)
} }
for _, v := range app.Scheduler.Entries() {
app.Logger.Info(v)
}
//ent := app.Scheduler.Enries()
app.Logger.Infow("Entries", app.Scheduler.Entries())
} }
func (app *Application) newTimer(channel, text string) { func (app *Application) newTimer(channel, text string) {
@ -83,11 +97,15 @@ func (app *Application) newTimer(channel, text string) {
// DeleteCommand takes in a name value and deletes the command from the database if it exists. // 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) { func (app *Application) DeleteTimer(name string, message twitch.PrivateMessage) {
cronName := fmt.Sprintf("%s-%s", message.Channel, name)
app.Scheduler.RemoveJob(cronName)
// app.Scheduler.Remove(timer.ID)
err := app.Models.Timers.Delete(name) err := app.Models.Timers.Delete(name)
if err != nil { if err != nil {
common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient)
app.Logger.Error(err) app.Logger.Error(err)
return common.Send(message.Channel, "Something went wrong FeelsBadMan", app.TwitchClient)
} }
reply := fmt.Sprintf("Deleted timer %s", name) reply := fmt.Sprintf("Deleted timer %s", name)

6
go.mod
View file

@ -6,7 +6,7 @@ require (
github.com/briandowns/openweathermap v0.18.0 github.com/briandowns/openweathermap v0.18.0
github.com/dustin/go-humanize v1.0.0 github.com/dustin/go-humanize v1.0.0
github.com/gempir/go-twitch-irc/v3 v3.2.0 github.com/gempir/go-twitch-irc/v3 v3.2.0
github.com/go-co-op/gocron v1.16.2 github.com/go-co-op/gocron v1.3.1
github.com/joho/godotenv v1.4.0 github.com/joho/godotenv v1.4.0
github.com/julienschmidt/httprouter v1.3.0 github.com/julienschmidt/httprouter v1.3.0
github.com/lib/pq v1.10.6 github.com/lib/pq v1.10.6
@ -14,7 +14,9 @@ require (
) )
require ( require (
github.com/robfig/cron/v3 v3.0.1 // indirect github.com/18211167516/robfig-cron v1.2.0 // indirect
github.com/jakecoffman/cron v0.0.0-20190106200828-7e2009c226a5 // indirect
github.com/robfig/cron/v3 v3.0.0 // indirect
github.com/rs/cors v1.8.2 // indirect github.com/rs/cors v1.8.2 // indirect
go.uber.org/atomic v1.9.0 // indirect go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect go.uber.org/multierr v1.8.0 // indirect

8
go.sum
View file

@ -1,3 +1,5 @@
github.com/18211167516/robfig-cron v1.2.0 h1:wKBprwgvcuTrj3aHM0MlD+Z6S9Wi2BxA/Pc1FHVf6zA=
github.com/18211167516/robfig-cron v1.2.0/go.mod h1:/vYGCSWmNcjZ2feXhRebbxfaRehqM4sqoqbx070e6Hc=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/briandowns/openweathermap v0.18.0 h1:JYTtJ4bKjXZRmDTe7huJ5+dZ7CsjPUw10GUzMASkNV8= github.com/briandowns/openweathermap v0.18.0 h1:JYTtJ4bKjXZRmDTe7huJ5+dZ7CsjPUw10GUzMASkNV8=
@ -9,8 +11,12 @@ github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/gempir/go-twitch-irc/v3 v3.2.0 h1:ENhsa7RgBE1GMmDqe0iMkvcSYfgw6ZsXilt+sAg32/U= github.com/gempir/go-twitch-irc/v3 v3.2.0 h1:ENhsa7RgBE1GMmDqe0iMkvcSYfgw6ZsXilt+sAg32/U=
github.com/gempir/go-twitch-irc/v3 v3.2.0/go.mod h1:/W9KZIiyizVecp4PEb7kc4AlIyXKiCmvlXrzlpPUytU= github.com/gempir/go-twitch-irc/v3 v3.2.0/go.mod h1:/W9KZIiyizVecp4PEb7kc4AlIyXKiCmvlXrzlpPUytU=
github.com/go-co-op/gocron v1.3.1 h1:jmt+rjqHznH+nfOPzrDRDt8Tac6P6sNC1xJcKkwvF3k=
github.com/go-co-op/gocron v1.3.1/go.mod h1:mZULUJnJbY7jWCAbva4/cngRVV80FxBizfPjezQxyZA=
github.com/go-co-op/gocron v1.16.2 h1:p9ghzsN5PqqPyWXYDO2JlvD1DOUNT8pPSyGYC62XBcY= github.com/go-co-op/gocron v1.16.2 h1:p9ghzsN5PqqPyWXYDO2JlvD1DOUNT8pPSyGYC62XBcY=
github.com/go-co-op/gocron v1.16.2/go.mod h1:W/N9G7bntRo5fVQlmjncvqSt74jxCxHfjyHlgcB33T8= github.com/go-co-op/gocron v1.16.2/go.mod h1:W/N9G7bntRo5fVQlmjncvqSt74jxCxHfjyHlgcB33T8=
github.com/jakecoffman/cron v0.0.0-20190106200828-7e2009c226a5 h1:kCvm3G3u+eTRbjfLPyfsfznJtraYEfZer/UvQ6CaQhI=
github.com/jakecoffman/cron v0.0.0-20190106200828-7e2009c226a5/go.mod h1:6DM2KNNK69jRu0lAHmYK9LYxmqpNjYHOaNp/ZxttD4U=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
@ -24,6 +30,8 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U= github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=