add how many commands have been used to ping command

This commit is contained in:
lyx0 2023-05-06 23:38:07 +02:00
parent f78e83e6f1
commit ab83e11624
3 changed files with 26 additions and 1 deletions

View file

@ -4,10 +4,12 @@ import (
"strings" "strings"
"github.com/lyx0/nourybot-matrix/internal/commands" "github.com/lyx0/nourybot-matrix/internal/commands"
"github.com/lyx0/nourybot-matrix/internal/common"
"maunium.net/go/mautrix/event" "maunium.net/go/mautrix/event"
) )
func (app *Application) ParseCommand(evt *event.Event) { func (app *Application) ParseCommand(evt *event.Event) {
common.CommandUsed()
// commandName is the actual name of the command without the prefix. // commandName is the actual name of the command without the prefix.
// e.g. `!ping` would be `ping`. // e.g. `!ping` would be `ping`.
commandName := strings.ToLower(strings.SplitN(evt.Content.AsMessage().Body, " ", 2)[0][1:]) commandName := strings.ToLower(strings.SplitN(evt.Content.AsMessage().Body, " ", 2)[0][1:])

View file

@ -1,6 +1,13 @@
package commands package commands
import (
"fmt"
"github.com/lyx0/nourybot-matrix/internal/common"
)
func Ping() (string, error) { func Ping() (string, error) {
resp := "Pong!" n := common.GetCommandsUsed()
resp := fmt.Sprintf("Pong! Commands used: %v", n)
return resp, nil return resp, nil
} }

View file

@ -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
}