From c67f9739dfc1257c4b450df0475b61f148ce8980 Mon Sep 17 00:00:00 2001 From: lyx0 Date: Thu, 14 Oct 2021 01:30:29 +0200 Subject: [PATCH] add HandleCommand --- pkg/commands/commands.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index 83c6a26..712450c 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -1,14 +1,44 @@ package commands import ( + "strings" + "github.com/gempir/go-twitch-irc/v2" log "github.com/sirupsen/logrus" ) +// HandleCommand receives a twitch.PrivateMessage from +// 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) { log.Info("fn HandleCommand") - switch message.Message { + + // commandName is the actual command name without the prefix. + commandName := strings.SplitN(message.Message, " ", 3)[0][2:] + + // cmdParams are additional command inputs. + // example: + // weather san antonio + // is + // commandName cmdParams[0] cmdParams[1] + cmdParams := strings.SplitN(message.Message, " ", 500) + + // msgLen is the amount of words in the message without the prefix. + // Useful for checking if enough cmdParams are given. + msgLen := len(strings.SplitN(message.Message, " ", -2)) + + switch commandName { + case "": + if msgLen == 1 { + twitchClient.Say(message.Channel, "Why yes, that's my prefix :)") + } + return case "xd": twitchClient.Say(message.Channel, "xd") + return + case "echo": + twitchClient.Say(message.Channel, cmdParams[1]) + return } }