add commands

This commit is contained in:
lyx0 2021-10-19 22:33:08 +02:00
parent 833eee0c99
commit cf4e4b64e4
3 changed files with 31 additions and 5 deletions

View file

@ -35,4 +35,7 @@ func main() {
handlers.PrivateMessage(message, nb) handlers.PrivateMessage(message, nb)
}) })
nb.TwitchClient.Join("nourybot")
nb.TwitchClient.Connect()
} }

View file

@ -1,6 +1,8 @@
package handlers package handlers
import ( import (
"strings"
"github.com/gempir/go-twitch-irc/v2" "github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/cmd/bot"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -9,6 +11,30 @@ import (
func Command(message twitch.PrivateMessage, nb *bot.Bot) { func Command(message twitch.PrivateMessage, nb *bot.Bot) {
logrus.Info("fn Command") logrus.Info("fn Command")
nb.Send("nourybot", "xd") // commandName is the actual command name without the prefix.
commandName := strings.ToLower(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 {
nb.Send(message.Channel, "xd")
}
case "echo":
if msgLen != 1 {
nb.Send(message.Channel, cmdParams[1])
return
}
}
} }

View file

@ -11,7 +11,7 @@ import (
// PrivateMessage is a command or not and passes it on accordingly. // PrivateMessage is a command or not and passes it on accordingly.
// Typical twitch message tags https://paste.ivr.fi/nopiradodo.lua // Typical twitch message tags https://paste.ivr.fi/nopiradodo.lua
func PrivateMessage(message twitch.PrivateMessage, nb *bot.Bot) { func PrivateMessage(message twitch.PrivateMessage, nb *bot.Bot) {
log.Info("fn HandlePrivateMessage") log.Info("fn PrivateMessage")
// log.Info(message) // log.Info(message)
// roomId is the Twitch UserID of the channel the message // roomId is the Twitch UserID of the channel the message
@ -24,9 +24,6 @@ func PrivateMessage(message twitch.PrivateMessage, nb *bot.Bot) {
return return
} }
// Message was sent from the Bot. Don't act on it
// so that we don't repeat ourself.
// Since our command prefix is () ignore every message // Since our command prefix is () ignore every message
// that is less than 2 // that is less than 2
if len(message.Message) >= 2 { if len(message.Message) >= 2 {