From dbaa4ed180275b0f9aff278111863cbc48f4d55b Mon Sep 17 00:00:00 2001 From: lyx0 Date: Thu, 14 Oct 2021 19:02:07 +0200 Subject: [PATCH] add uptime --- cmd/bot/bot.go | 6 +++++- pkg/commands/ping.go | 8 ++++++-- pkg/handlers/command.go | 5 +++-- pkg/handlers/privatemessage.go | 6 ++++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cmd/bot/bot.go b/cmd/bot/bot.go index 8b1db43..39fd54a 100644 --- a/cmd/bot/bot.go +++ b/cmd/bot/bot.go @@ -1,6 +1,8 @@ package bot import ( + "time" + twitch "github.com/gempir/go-twitch-irc/v2" cfg "github.com/lyx0/nourybot/pkg/config" "github.com/lyx0/nourybot/pkg/handlers" @@ -10,6 +12,7 @@ import ( type Bot struct { twitchClient *twitch.Client cfg *cfg.Config + Uptime time.Time } func NewBot(cfg *cfg.Config) *Bot { @@ -20,6 +23,7 @@ func NewBot(cfg *cfg.Config) *Bot { return &Bot{ cfg: cfg, twitchClient: twitchClient, + Uptime: time.Now(), } } @@ -28,7 +32,7 @@ func (b *Bot) Connect() error { cfg := cfg.LoadConfig() b.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) { - handlers.HandlePrivateMessage(message, b.twitchClient, cfg) + handlers.HandlePrivateMessage(message, b.twitchClient, cfg, b.Uptime) }) b.twitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) { diff --git a/pkg/commands/ping.go b/pkg/commands/ping.go index e8b84c5..493ef80 100644 --- a/pkg/commands/ping.go +++ b/pkg/commands/ping.go @@ -2,13 +2,17 @@ package commands import ( "fmt" + "time" "github.com/gempir/go-twitch-irc/v2" + "github.com/lyx0/nourybot/pkg/humanize" "github.com/lyx0/nourybot/pkg/utils" ) -func Ping(channel string, client *twitch.Client) { +func Ping(channel string, client *twitch.Client, uptime time.Time) { commandCount := fmt.Sprint(utils.GetCommandsUsed()) - s := fmt.Sprintf("Pong! :) Commands used: %v", commandCount) + botUptime := humanize.HumanizeTime(uptime) + + s := fmt.Sprintf("Pong! :) Commands used: %v, Last restart: %v", commandCount, botUptime) client.Say(channel, s) } diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index 036878a..88925d2 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -2,6 +2,7 @@ package handlers import ( "strings" + "time" "github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/pkg/commands" @@ -13,7 +14,7 @@ import ( // HandlePrivateMessage where it found a command in it. // HandleCommand passes on the message to the specific // command handler for further action. -func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client) { +func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, uptime time.Time) { log.Info("fn HandleCommand") // Counter that increments on every command call. @@ -46,6 +47,6 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client) { twitchClient.Say(message.Channel, cmdParams[1]) return case "ping": - commands.Ping(message.Channel, twitchClient) + commands.Ping(message.Channel, twitchClient, uptime) } } diff --git a/pkg/handlers/privatemessage.go b/pkg/handlers/privatemessage.go index eb2745a..fabb72a 100644 --- a/pkg/handlers/privatemessage.go +++ b/pkg/handlers/privatemessage.go @@ -1,6 +1,8 @@ package handlers import ( + "time" + "github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/pkg/config" log "github.com/sirupsen/logrus" @@ -10,7 +12,7 @@ import ( // *twitch.Client and *config.Config and has the logic to decide if the provided // PrivateMessage is a command or not and passes it on accordingly. // Typical twitch message tags https://paste.ivr.fi/nopiradodo.lua -func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client, cfg *config.Config) { +func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client, cfg *config.Config, uptime time.Time) { log.Info("fn HandlePrivateMessage") // log.Info(message) @@ -38,7 +40,7 @@ func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client, // Message starts with (), pass it on to // the command handler. if message.Message[:2] == "()" { - HandleCommand(message, client) + HandleCommand(message, client, uptime) return } }