38 lines
939 B
Go
38 lines
939 B
Go
package bot
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"git.ewnix.net/phlux/discord-gpt-bot/bot/memory"
|
|
"git.ewnix.net/phlux/discord-gpt-bot/openai"
|
|
"github.com/bwmarrin/discordgo"
|
|
)
|
|
|
|
func Start(session *discordgo.Session) {
|
|
session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
if m.Author.Bot {
|
|
return
|
|
}
|
|
|
|
if !strings.Contains(m.Content, "<@"+s.State.User.ID+">") {
|
|
return
|
|
}
|
|
|
|
userID := m.Author.ID
|
|
content := strings.ReplaceAll(m.Content, "<@"+s.State.User.ID+">", "")
|
|
history := memory.GetHistory(userID)
|
|
|
|
history = append(history, openai.Message{Role: "user", Content: strings.TrimSpace(content)})
|
|
resp, err := openai.Ask(history)
|
|
if err != nil {
|
|
s.ChannelMessageSend(m.ChannelID, "Error talking to ChatGPT: "+err.Error())
|
|
return
|
|
}
|
|
|
|
history = append(history, openai.Message{Role: "assistant", Content: resp})
|
|
memory.SetHistory(userID, history)
|
|
|
|
s.ChannelMessageSend(m.ChannelID, resp)
|
|
})
|
|
}
|