2022-08-07 01:34:31 +02:00
|
|
|
package main
|
2022-06-21 00:31:17 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gempir/go-twitch-irc/v3"
|
2022-08-07 01:34:31 +02:00
|
|
|
"github.com/lyx0/nourybot/pkg/commands"
|
2022-08-07 02:27:40 +02:00
|
|
|
"github.com/lyx0/nourybot/pkg/common"
|
|
|
|
"github.com/sirupsen/logrus"
|
2022-06-21 00:31:17 +02:00
|
|
|
)
|
|
|
|
|
2022-08-07 02:27:40 +02:00
|
|
|
func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) {
|
|
|
|
logrus.Info("[COMMAND HANDLER]", message)
|
|
|
|
logrus.Info(message)
|
2022-06-21 00:31:17 +02:00
|
|
|
|
|
|
|
// commandName is the actual name of the command without the prefix.
|
|
|
|
// e.g. `()ping` would be `ping`.
|
|
|
|
commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:])
|
2022-08-07 02:27:40 +02:00
|
|
|
logrus.Info(commandName)
|
2022-06-21 00:31:17 +02:00
|
|
|
|
|
|
|
// cmdParams are additional command parameters.
|
|
|
|
// e.g. `()weather san antonio`
|
|
|
|
// cmdParam[0] is `san` and cmdParam[1] = `antonio`.
|
|
|
|
//
|
|
|
|
// Since Twitch messages are at most 500 characters I use a
|
|
|
|
// maximum count of 500+10 just to be safe.
|
|
|
|
// https://discuss.dev.twitch.tv/t/missing-client-side-message-length-check/21316
|
2022-08-07 02:27:40 +02:00
|
|
|
cmdParams := strings.SplitN(message.Message, " ", 500)
|
2022-06-21 00:31:17 +02:00
|
|
|
_ = cmdParams
|
|
|
|
|
|
|
|
// msgLen is the amount of words in a message without the prefix.
|
|
|
|
// Useful to check if enough cmdParams are provided.
|
|
|
|
msgLen := len(strings.SplitN(message.Message, " ", -2))
|
2022-08-07 02:27:40 +02:00
|
|
|
logrus.Info(msgLen)
|
2022-06-21 00:31:17 +02:00
|
|
|
|
|
|
|
// target is the channelname the message originated from and
|
|
|
|
// where we are responding.
|
|
|
|
target := message.Channel
|
|
|
|
|
|
|
|
switch commandName {
|
|
|
|
case "":
|
|
|
|
if msgLen == 1 {
|
2022-08-07 02:27:40 +02:00
|
|
|
common.Send(target, "xd", tc)
|
2022-06-21 00:31:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case "echo":
|
|
|
|
if msgLen < 2 {
|
2022-08-07 02:27:40 +02:00
|
|
|
common.Send(target, "Not enough arguments provided.", tc)
|
2022-06-21 00:31:17 +02:00
|
|
|
return
|
|
|
|
} else {
|
2022-08-07 02:27:40 +02:00
|
|
|
commands.Echo(target, message.Message[7:len(message.Message)], tc)
|
2022-08-07 01:34:31 +02:00
|
|
|
// bot.Send(target, message.Message[7:len(message.Message)])
|
2022-06-21 00:31:17 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|