From d7e38bb17cb85b19f9030691747f5536aa492088 Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sun, 7 Aug 2022 16:36:06 +0200 Subject: [PATCH] add ping command and uptime time helper --- cmd/bot/command.go | 2 ++ cmd/bot/main.go | 2 ++ go.mod | 1 + go.sum | 4 ++-- pkg/commands/ping.go | 16 ++++++++++++++++ pkg/common/uptime.go | 15 +++++++++++++++ 6 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 pkg/commands/ping.go create mode 100644 pkg/common/uptime.go diff --git a/cmd/bot/command.go b/cmd/bot/command.go index 29fed9c..8404393 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -65,5 +65,7 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) { } else { commands.Tweet(target, cmdParams[1], tc) } + case "ping": + commands.Ping(target, tc) } } diff --git a/cmd/bot/main.go b/cmd/bot/main.go index 9fc4bd1..79e1665 100644 --- a/cmd/bot/main.go +++ b/cmd/bot/main.go @@ -19,6 +19,8 @@ func main() { sugar := zap.NewExample().Sugar() defer sugar.Sync() + common.StartTime() + app := &Application{ TwitchClient: tc, Logger: sugar, diff --git a/go.mod b/go.mod index b67291e..5c4c9b6 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/lyx0/nourybot go 1.19 require ( + github.com/dustin/go-humanize v1.0.0 github.com/gempir/go-twitch-irc/v3 v3.2.0 github.com/joho/godotenv v1.4.0 go.uber.org/zap v1.21.0 diff --git a/go.sum b/go.sum index 8b284aa..1ce7637 100644 --- a/go.sum +++ b/go.sum @@ -3,8 +3,8 @@ github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gempir/go-twitch-irc/v3 v3.1.0 h1:bUVZ5mADhH7KidJVcl+z79kgLJ7sjdAk4b/ylAvaLy0= -github.com/gempir/go-twitch-irc/v3 v3.1.0/go.mod h1:/W9KZIiyizVecp4PEb7kc4AlIyXKiCmvlXrzlpPUytU= +github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= +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/go.mod h1:/W9KZIiyizVecp4PEb7kc4AlIyXKiCmvlXrzlpPUytU= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= diff --git a/pkg/commands/ping.go b/pkg/commands/ping.go new file mode 100644 index 0000000..5092459 --- /dev/null +++ b/pkg/commands/ping.go @@ -0,0 +1,16 @@ +package commands + +import ( + "fmt" + + "github.com/gempir/go-twitch-irc/v3" + "github.com/lyx0/nourybot/pkg/common" + "github.com/lyx0/nourybot/pkg/humanize" +) + +func Ping(target string, tc *twitch.Client) { + botUptime := humanize.Time(common.GetUptime()) + + reply := fmt.Sprintf("Pong! :) Last restart: %v", botUptime) + common.Send(target, reply, tc) +} diff --git a/pkg/common/uptime.go b/pkg/common/uptime.go new file mode 100644 index 0000000..a1d0086 --- /dev/null +++ b/pkg/common/uptime.go @@ -0,0 +1,15 @@ +package common + +import "time" + +var ( + uptime time.Time +) + +func StartTime() { + uptime = time.Now() +} + +func GetUptime() time.Time { + return uptime +}