First commit
This commit is contained in:
37
bot/bot.go
Normal file
37
bot/bot.go
Normal file
@ -0,0 +1,37 @@
|
||||
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)
|
||||
})
|
||||
}
|
15
bot/memory.go
Normal file
15
bot/memory.go
Normal file
@ -0,0 +1,15 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"git.ewnix.net/phlux/discord-gpt-bot/openai"
|
||||
)
|
||||
|
||||
var conversations = map[string][]openai.Message{}
|
||||
|
||||
func GetHistory(userID string) []openai.Message {
|
||||
return conversations[userID]
|
||||
}
|
||||
|
||||
func SetHistory(userID string, history []openai.Message) {
|
||||
conversations[userID] = history
|
||||
}
|
Reference in New Issue
Block a user