commit 273242029426f404b71fe563cbc107062092b1fb Author: phlux Date: Wed Apr 23 00:04:00 2025 +0000 First commit diff --git a/bot/bot.go b/bot/bot.go new file mode 100644 index 0000000..7a8da5d --- /dev/null +++ b/bot/bot.go @@ -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) + }) +} diff --git a/bot/memory.go b/bot/memory.go new file mode 100644 index 0000000..53fc2ae --- /dev/null +++ b/bot/memory.go @@ -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 +} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..9639e23 --- /dev/null +++ b/config/config.go @@ -0,0 +1,6 @@ +package config + +var ( + BotToken = "REMOVED" + OpenAIKey = "REMOVED" +) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..dcc078d --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..c461a4b --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go new file mode 100755 index 0000000..0041d3e --- /dev/null +++ b/main.go @@ -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 +} diff --git a/openai/client.go b/openai/client.go new file mode 100644 index 0000000..0c5dce9 --- /dev/null +++ b/openai/client.go @@ -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 +}