mirror of
https://github.com/lyx0/nourybot.git
synced 2024-11-13 19:49:55 +01:00
start implementing basic command handler
This commit is contained in:
parent
2ee85c47d7
commit
a7ab565b24
2 changed files with 43 additions and 1 deletions
41
cmd/bot/command.go
Normal file
41
cmd/bot/command.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gempir/go-twitch-irc/v3"
|
||||
)
|
||||
|
||||
func (app *application) handleCommand(message twitch.PrivateMessage) {
|
||||
app.logger.Info("[COMMAND HANDLER]", message)
|
||||
|
||||
// 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:])
|
||||
|
||||
// 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
|
||||
cmdParams := strings.SplitN(message.Message, " ", 510)
|
||||
_ = 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))
|
||||
|
||||
// target is the channelname the message originated from and
|
||||
// where we are responding.
|
||||
target := message.Channel
|
||||
|
||||
switch commandName {
|
||||
case "":
|
||||
if msgLen == 1 {
|
||||
app.twitchClient.Say(target, "xd")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
|
@ -16,7 +16,8 @@ func (app *application) handlePrivateMessage(message twitch.PrivateMessage) {
|
|||
if len(message.Message) >= 2 {
|
||||
if message.Message[:2] == "()" {
|
||||
// TODO: Command Handling
|
||||
app.logger.Infof("[Command detected]: ", message.Message)
|
||||
app.handleCommand(message)
|
||||
// app.logger.Infof("[Command detected]: ", message.Message)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue