From ab83e11624061c499b25b8e964f1bbadb028583b Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sat, 6 May 2023 23:38:07 +0200 Subject: [PATCH] add how many commands have been used to ping command --- cmd/nourybot/commands.go | 2 ++ internal/commands/ping.go | 9 ++++++++- internal/common/counter.go | 16 ++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 internal/common/counter.go diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index 0fbc215..0f1bba8 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -4,10 +4,12 @@ import ( "strings" "github.com/lyx0/nourybot-matrix/internal/commands" + "github.com/lyx0/nourybot-matrix/internal/common" "maunium.net/go/mautrix/event" ) func (app *Application) ParseCommand(evt *event.Event) { + common.CommandUsed() // commandName is the actual name of the command without the prefix. // e.g. `!ping` would be `ping`. commandName := strings.ToLower(strings.SplitN(evt.Content.AsMessage().Body, " ", 2)[0][1:]) diff --git a/internal/commands/ping.go b/internal/commands/ping.go index 519a901..1290ca3 100644 --- a/internal/commands/ping.go +++ b/internal/commands/ping.go @@ -1,6 +1,13 @@ package commands +import ( + "fmt" + + "github.com/lyx0/nourybot-matrix/internal/common" +) + func Ping() (string, error) { - resp := "Pong!" + n := common.GetCommandsUsed() + resp := fmt.Sprintf("Pong! Commands used: %v", n) return resp, nil } diff --git a/internal/common/counter.go b/internal/common/counter.go new file mode 100644 index 0000000..28451bc --- /dev/null +++ b/internal/common/counter.go @@ -0,0 +1,16 @@ +package common + +var ( + tempCommands = 0 +) + +// CommandUsed is called on every command incremenenting tempCommands. +func CommandUsed() { + tempCommands++ +} + +// GetCommandsUsed returns the amount of commands that have been used +// since the last restart. +func GetCommandsUsed() int { + return tempCommands +}