mirror-nourybot/pkg/handlers/command.go

73 lines
1.9 KiB
Go
Raw Normal View History

package handlers
2021-10-14 00:19:35 +02:00
import (
2021-10-14 01:30:29 +02:00
"strings"
2021-10-14 19:02:07 +02:00
"time"
2021-10-14 01:30:29 +02:00
2021-10-14 00:19:35 +02:00
"github.com/gempir/go-twitch-irc/v2"
2021-10-14 18:46:16 +02:00
"github.com/lyx0/nourybot/pkg/commands"
"github.com/lyx0/nourybot/pkg/utils"
2021-10-14 00:19:35 +02:00
log "github.com/sirupsen/logrus"
)
2021-10-14 01:30:29 +02:00
// 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.
2021-10-14 19:02:07 +02:00
func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, uptime time.Time) {
2021-10-14 00:19:35 +02:00
log.Info("fn HandleCommand")
2021-10-14 01:30:29 +02:00
2021-10-14 18:46:16 +02:00
// Counter that increments on every command call.
utils.CommandUsed()
2021-10-14 01:30:29 +02:00
// commandName is the actual command name without the prefix.
2021-10-14 22:22:05 +02:00
commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:])
2021-10-14 01:30:29 +02:00
// 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
2021-10-14 00:19:35 +02:00
case "xd":
twitchClient.Say(message.Channel, "xd")
2021-10-14 01:30:29 +02:00
return
case "echo":
twitchClient.Say(message.Channel, cmdParams[1])
return
2021-10-14 18:46:16 +02:00
case "ping":
2021-10-14 19:02:07 +02:00
commands.Ping(message.Channel, twitchClient, uptime)
2021-10-14 22:05:12 +02:00
return
case "cf":
commands.Coinflip(message.Channel, twitchClient)
return
case "coin":
commands.Coinflip(message.Channel, twitchClient)
return
case "coinflip":
commands.Coinflip(message.Channel, twitchClient)
return
2021-10-14 22:18:31 +02:00
case "color":
commands.Color(message, twitchClient)
return
2021-10-14 22:22:05 +02:00
case "mycolor":
commands.Color(message, twitchClient)
return
2021-10-14 22:32:00 +02:00
case "8ball":
commands.EightBall(message, twitchClient)
return
2021-10-14 22:05:12 +02:00
2021-10-14 00:19:35 +02:00
}
}