add uptime

This commit is contained in:
lyx0 2021-10-14 19:02:07 +02:00
parent b9d7dcbeea
commit dbaa4ed180
4 changed files with 18 additions and 7 deletions

View file

@ -1,6 +1,8 @@
package bot package bot
import ( import (
"time"
twitch "github.com/gempir/go-twitch-irc/v2" twitch "github.com/gempir/go-twitch-irc/v2"
cfg "github.com/lyx0/nourybot/pkg/config" cfg "github.com/lyx0/nourybot/pkg/config"
"github.com/lyx0/nourybot/pkg/handlers" "github.com/lyx0/nourybot/pkg/handlers"
@ -10,6 +12,7 @@ import (
type Bot struct { type Bot struct {
twitchClient *twitch.Client twitchClient *twitch.Client
cfg *cfg.Config cfg *cfg.Config
Uptime time.Time
} }
func NewBot(cfg *cfg.Config) *Bot { func NewBot(cfg *cfg.Config) *Bot {
@ -20,6 +23,7 @@ func NewBot(cfg *cfg.Config) *Bot {
return &Bot{ return &Bot{
cfg: cfg, cfg: cfg,
twitchClient: twitchClient, twitchClient: twitchClient,
Uptime: time.Now(),
} }
} }
@ -28,7 +32,7 @@ func (b *Bot) Connect() error {
cfg := cfg.LoadConfig() cfg := cfg.LoadConfig()
b.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) { 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) { b.twitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) {

View file

@ -2,13 +2,17 @@ package commands
import ( import (
"fmt" "fmt"
"time"
"github.com/gempir/go-twitch-irc/v2" "github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/pkg/humanize"
"github.com/lyx0/nourybot/pkg/utils" "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()) 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) client.Say(channel, s)
} }

View file

@ -2,6 +2,7 @@ package handlers
import ( import (
"strings" "strings"
"time"
"github.com/gempir/go-twitch-irc/v2" "github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/pkg/commands" "github.com/lyx0/nourybot/pkg/commands"
@ -13,7 +14,7 @@ import (
// HandlePrivateMessage where it found a command in it. // HandlePrivateMessage where it found a command in it.
// HandleCommand passes on the message to the specific // HandleCommand passes on the message to the specific
// command handler for further action. // 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") log.Info("fn HandleCommand")
// Counter that increments on every command call. // 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]) twitchClient.Say(message.Channel, cmdParams[1])
return return
case "ping": case "ping":
commands.Ping(message.Channel, twitchClient) commands.Ping(message.Channel, twitchClient, uptime)
} }
} }

View file

@ -1,6 +1,8 @@
package handlers package handlers
import ( import (
"time"
"github.com/gempir/go-twitch-irc/v2" "github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/pkg/config" "github.com/lyx0/nourybot/pkg/config"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -10,7 +12,7 @@ import (
// *twitch.Client and *config.Config and has the logic to decide if the provided // *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. // PrivateMessage is a command or not and passes it on accordingly.
// Typical twitch message tags https://paste.ivr.fi/nopiradodo.lua // 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("fn HandlePrivateMessage")
// log.Info(message) // log.Info(message)
@ -38,7 +40,7 @@ func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client,
// Message starts with (), pass it on to // Message starts with (), pass it on to
// the command handler. // the command handler.
if message.Message[:2] == "()" { if message.Message[:2] == "()" {
HandleCommand(message, client) HandleCommand(message, client, uptime)
return return
} }
} }