First commit
This commit is contained in:
commit
2732420294
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
|
||||||
|
}
|
6
config/config.go
Normal file
6
config/config.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
var (
|
||||||
|
BotToken = "REMOVED"
|
||||||
|
OpenAIKey = "REMOVED"
|
||||||
|
)
|
11
go.mod
Normal file
11
go.mod
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
module git.ewnix.net/phlux/discord-gpt-bot
|
||||||
|
|
||||||
|
go 1.24.2
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/bwmarrin/discordgo v0.28.1 // indirect
|
||||||
|
github.com/gorilla/websocket v1.4.2 // indirect
|
||||||
|
github.com/sashabaranov/go-openai v1.38.2 // indirect
|
||||||
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
|
||||||
|
)
|
14
go.sum
Normal file
14
go.sum
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
|
||||||
|
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||||
|
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||||
|
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/sashabaranov/go-openai v1.38.2 h1:akrssjj+6DY3lWuDwHv6cBvJ8Z+FZDM9XEaaYFt0Auo=
|
||||||
|
github.com/sashabaranov/go-openai v1.38.2/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
|
||||||
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||||
|
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||||
|
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
|
||||||
|
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
|
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||||
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
32
main.go
Executable file
32
main.go
Executable file
@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"git.ewnix.net/phlux/discord-gpt-bot/bot"
|
||||||
|
"git.ewnix.net/phlux/discord-gpt-bot/config"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
dg, err := discordgo.New("Bot " + config.BotToken)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error creating Discord session: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = dg.Open()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error opening connection: %v", err)
|
||||||
|
}
|
||||||
|
defer dg.Close()
|
||||||
|
|
||||||
|
bot.Start(dg)
|
||||||
|
|
||||||
|
log.Println("Bot is now running. Press CTRL+C to exit.")
|
||||||
|
stop := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
|
||||||
|
<-stop
|
||||||
|
}
|
38
openai/client.go
Normal file
38
openai/client.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package openai
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"git.ewnix.net/phlux/g/discord-gpt-bot/config"
|
||||||
|
"github.com/sashabaranov/go-openai"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
Role string `json:"role"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func Ask(messages []Message) (string, error) {
|
||||||
|
client := openai.NewClient(config.OpenAIKey)
|
||||||
|
|
||||||
|
var chatHistory []openai.ChatCompletionMessage
|
||||||
|
for _, m := range messages {
|
||||||
|
chatHistory = append(chatHistory, openai.ChatCompletionMessage{
|
||||||
|
Role: m.Role,
|
||||||
|
Content: m.Content,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.CreateChatCompletion(
|
||||||
|
context.Background(),
|
||||||
|
openai.ChatCompletionRequest{
|
||||||
|
Model: openai.GPT4,
|
||||||
|
Messages: chatHistory,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.Choices[0].Message.Content, nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user