38 lines
856 B
Go

package bot
import (
"strings"
"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 := 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})
SetHistory(userID, history)
s.ChannelMessageSend(m.ChannelID, resp)
})
}