mirror-ollama-twitch-bot/main.go

130 lines
3.2 KiB
Go
Raw Normal View History

2024-03-03 00:07:33 +01:00
package main
import (
"os"
"strings"
2024-03-03 00:07:33 +01:00
"github.com/gempir/go-twitch-irc/v4"
"github.com/joho/godotenv"
"go.uber.org/zap"
)
type config struct {
2024-03-05 18:15:03 +01:00
twitchUsername string
twitchOauth string
2024-03-05 21:02:12 +01:00
ollamaModel string
ollamaContext string
ollamaSystem string
2024-03-03 00:07:33 +01:00
}
type application struct {
2024-03-05 21:02:12 +01:00
twitchClient *twitch.Client
log *zap.SugaredLogger
2024-03-05 21:17:23 +01:00
cfg config
2024-03-05 21:02:12 +01:00
userMsgStore map[string][]ollamaMessage
msgStore []ollamaMessage
2024-03-03 00:07:33 +01:00
}
func main() {
logger := zap.NewExample()
defer func() {
if err := logger.Sync(); err != nil {
logger.Sugar().Errorw("error syncing logger",
"error", err,
)
}
}()
sugar := logger.Sugar()
err := godotenv.Load()
if err != nil {
sugar.Fatal("Error loading .env")
}
2024-03-05 18:35:46 +01:00
userMsgStore := make(map[string][]ollamaMessage)
2024-03-03 00:07:33 +01:00
app := &application{
2024-03-05 21:02:12 +01:00
log: sugar,
userMsgStore: userMsgStore,
2024-03-03 00:07:33 +01:00
}
2024-03-05 21:17:23 +01:00
app.cfg.twitchUsername = os.Getenv("TWITCH_USERNAME")
app.cfg.twitchOauth = os.Getenv("TWITCH_OAUTH")
app.cfg.ollamaModel = os.Getenv("OLLAMA_MODEL")
app.cfg.ollamaContext = os.Getenv("OLLAMA_CONTEXT")
app.cfg.ollamaSystem = os.Getenv("OLLAMA_SYSTEM")
tc := twitch.NewClient(app.cfg.twitchUsername, app.cfg.twitchOauth)
app.twitchClient = tc
2024-03-03 00:07:33 +01:00
// Received a PrivateMessage (normal chat message).
2024-03-05 21:02:12 +01:00
app.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
2024-03-03 00:07:33 +01:00
// roomId is the Twitch UserID of the channel the message originated from.
// If there is no roomId something went really wrong.
roomId := message.Tags["room-id"]
if roomId == "" {
return
}
// Message was shorter than our prefix is therefore it's irrelevant for us.
2024-03-05 21:44:44 +01:00
if len(message.Message) >= 2 && message.Message[:2] == "()" {
var reply string
// msgLen is the amount of words in a message without the prefix.
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>"
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
}
2024-03-03 00:07:33 +01:00
}
}
})
2024-03-05 21:02:12 +01:00
app.twitchClient.OnConnect(func() {
app.log.Info("Successfully connected to Twitch Servers")
2024-03-05 21:17:23 +01:00
app.log.Infow("Ollama",
"Context: ", app.cfg.ollamaContext,
"Model: ", app.cfg.ollamaModel,
"System: ", app.cfg.ollamaSystem,
)
2024-03-03 00:07:33 +01:00
})
channels := os.Getenv("TWITCH_CHANNELS")
channel := strings.Split(channels, ",")
for i := 0; i < len(channel); i++ {
2024-03-05 21:02:12 +01:00
app.twitchClient.Join(channel[i])
app.twitchClient.Say(channel[i], "MrDestructoid")
app.log.Infof("Joining channel: %s", channel[i])
}
2024-03-03 00:07:33 +01:00
// Actually connect to chat.
2024-03-05 21:02:12 +01:00
err = app.twitchClient.Connect()
2024-03-03 00:07:33 +01:00
if err != nil {
panic(err)
}
}