gpt
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
.env
|
||||
node_modules
|
||||
__debug_bin.exe
|
||||
|
||||
1
go.mod
1
go.mod
@@ -6,6 +6,7 @@ require (
|
||||
github.com/bwmarrin/discordgo v0.26.1 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/joho/godotenv v1.4.0 // indirect
|
||||
github.com/sashabaranov/go-openai v1.5.7 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirect
|
||||
golang.org/x/sys v0.1.0 // indirect
|
||||
|
||||
2
go.sum
2
go.sum
@@ -4,6 +4,8 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/sashabaranov/go-openai v1.5.7 h1:8DGgRG+P7yWixte5j720y6yiXgY3Hlgcd0gcpHdltfo=
|
||||
github.com/sashabaranov/go-openai v1.5.7/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/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE=
|
||||
|
||||
51
main.go
51
main.go
@@ -1,14 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/joho/godotenv"
|
||||
openai "github.com/sashabaranov/go-openai"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -16,6 +19,7 @@ var (
|
||||
BotPrefix string
|
||||
ChannelID string
|
||||
BotId string
|
||||
apiKey string
|
||||
goBot *discordgo.Session
|
||||
|
||||
config *configStruct
|
||||
@@ -54,6 +58,7 @@ func ReadConfig() error {
|
||||
Token = os.Getenv("TOKEN")
|
||||
BotPrefix = os.Getenv("BOTPREFIX")
|
||||
ChannelID = os.Getenv("ChannelID")
|
||||
apiKey = os.Getenv("OPENAI_API_KEY")
|
||||
|
||||
return nil
|
||||
|
||||
@@ -79,6 +84,7 @@ func Start() {
|
||||
goBot.AddHandler(messageHandler)
|
||||
goBot.AddHandler(handleGuildMemberAdd)
|
||||
goBot.AddHandler(handleGuildMemberRemove)
|
||||
goBot.AddHandler(handleTag)
|
||||
|
||||
err = goBot.Open()
|
||||
|
||||
@@ -239,3 +245,48 @@ func handleGuildMemberRemove(s *discordgo.Session, m *discordgo.GuildMemberRemov
|
||||
log.Printf("Error sending goodbye message: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func handleTag(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
// Ignore messages sent by the bot itself
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
|
||||
client := openai.NewClient(apiKey)
|
||||
|
||||
// Check if bot is mentioned in message
|
||||
for _, mention := range m.Mentions {
|
||||
if mention.ID == s.State.User.ID {
|
||||
// Remove mention from message content
|
||||
content := strings.Replace(m.ContentWithMentionsReplaced(), "<@"+s.State.User.ID+">", "", -1)
|
||||
content = strings.Replace(content, "<@!"+s.State.User.ID+">", "", -1)
|
||||
content = strings.TrimSpace(content)
|
||||
|
||||
resp, err := client.CreateChatCompletion(
|
||||
context.Background(),
|
||||
openai.ChatCompletionRequest{
|
||||
Model: openai.GPT3Dot5Turbo,
|
||||
Messages: []openai.ChatCompletionMessage{
|
||||
{
|
||||
Role: openai.ChatMessageRoleUser,
|
||||
Content: content,
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("ChatCompletion error: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(resp.Choices[0].Message.Content)
|
||||
|
||||
// Send generated response back to Discord
|
||||
_, err = s.ChannelMessageSend(m.ChannelID, resp.Choices[0].Message.Content)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user