move command logic into main file

This commit is contained in:
lyx0 2024-03-05 21:44:44 +01:00
parent f31e75b73b
commit 779cc8650c
2 changed files with 33 additions and 50 deletions

View file

@ -1,44 +0,0 @@
package main
import (
"strings"
"github.com/gempir/go-twitch-irc/v4"
)
// handleCommand is called each time a message starts with "()"
func (app *application) handleCommand(message twitch.PrivateMessage) {
var reply string
// 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))
// commandName is the actual name of the command without the prefix.
// e.g. `()gpt` is `gpt`.
commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:])
switch commandName {
case "gpt":
if msgLen < 2 {
reply = "Not enough arguments provided. Usage: ()gpt <query>"
} else {
switch app.cfg.ollamaContext {
case "none":
app.generateNoContext(message.Channel, message.Message[6:len(message.Message)])
return
case "general":
app.chatGeneralContext(message.Channel, message.Message[6:len(message.Message)])
return
case "user":
app.chatUserContext(message.Channel, message.User.Name, message.Message[6:len(message.Message)])
return
}
}
if reply != "" {
go app.send(message.Channel, reply)
return
}
}
}

39
main.go
View file

@ -67,12 +67,39 @@ func main() {
} }
// Message was shorter than our prefix is therefore it's irrelevant for us. // Message was shorter than our prefix is therefore it's irrelevant for us.
if len(message.Message) >= 2 { if len(message.Message) >= 2 && message.Message[:2] == "()" {
// Check if the first 2 characters of the mesage were our prefix. var reply string
// if they were forward the message to the command handler.
if message.Message[:2] == "()" { // msgLen is the amount of words in a message without the prefix.
go app.handleCommand(message) msgLen := len(strings.SplitN(message.Message, " ", -2))
return
// commandName is the actual name of the command without the prefix.
// e.g. `()gpt` is `gpt`.
commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:])
switch commandName {
case "gpt":
if msgLen < 2 {
reply = "Not enough arguments provided. Usage: ()gpt <query>"
return
} else {
switch app.cfg.ollamaContext {
case "none":
app.generateNoContext(message.Channel, message.Message[6:len(message.Message)])
return
case "general":
app.chatGeneralContext(message.Channel, message.Message[6:len(message.Message)])
return
case "user":
app.chatUserContext(message.Channel, message.User.Name, message.Message[6:len(message.Message)])
return
}
}
if reply != "" {
go app.send(message.Channel, reply)
return
}
} }
} }
}) })