-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
129 lines (114 loc) · 3.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"github.com/rayspock/go-chatgpt-discord/handler"
"github.com/rayspock/go-chatgpt-discord/provider"
"github.com/rayspock/go-chatgpt-discord/ref"
"github.com/rayspock/go-chatgpt-discord/setup"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"syscall"
)
type config struct {
openaiAPIKey string
openaiModel string
discordClientID string
botToken string
logConfig setup.LogConfig
}
func readConfig() config {
return config{
openaiModel: os.Getenv("OPENAI_MODEL"),
openaiAPIKey: os.Getenv("OPENAI_API_KEY"),
discordClientID: os.Getenv("DISCORD_CLIENT_ID"),
botToken: os.Getenv("DISCORD_BOT_TOKEN"),
logConfig: setup.LogConfig{
LogLevel: os.Getenv("LOG_LEVEL"),
},
}
}
func init() {
err := godotenv.Load()
if err != nil {
panic(errors.Wrapf(err, "couldn't load .env file"))
}
}
var (
commands = []*discordgo.ApplicationCommand{
{
Name: handler.ApplicationCommandChat,
Description: "Create a new thread for conversation.",
DMPermission: ref.Of(true),
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "messages",
Description: "Message to send",
Required: true,
},
},
},
}
)
func main() {
// load configuration
cfg := readConfig()
setup.ConfigureLogger(cfg.logConfig)
// create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + cfg.botToken)
if err != nil {
log.Fatalf("error creating discord session: %v", err)
return
}
// get app id
app, err := dg.Application("@me")
if err != nil {
log.Fatalf("couldn't get app id: %v", err)
}
log.Println("adding commands...")
for _, v := range commands {
_, err = dg.ApplicationCommandCreate(app.ID, "", v)
if err != nil {
log.Panicf("cannot create '%v' command: %v", v.Name, err)
}
}
// configure discord handler
chatGPTService := provider.NewChatGPTService(cfg.openaiAPIKey, cfg.openaiModel)
discordHandler := handler.NewDiscordHandler(chatGPTService)
dg.AddHandler(discordHandler.GetInteractionCreateHandler())
// add direct message handler
dg.AddHandler(discordHandler.GetMessageCreateHandler())
// open a websocket connection to discord and begin listening.
err = dg.Open()
if err != nil {
log.Fatalf("error opening connection: %v", err)
return
}
defer dg.Close()
// show bot invite url
botInviteURL := fmt.Sprintf("https://discord.com/api/oauth2/authorize?client_id=%s&permissions=%s&scope=%s",
cfg.discordClientID, "328565073920", "bot")
log.Infof("invite bot to your server: %s", botInviteURL)
// wait here until ctrl-c or other term signal is received.
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
log.Info("bot is now running. press ctrl-c to exit.")
<-stop
// remove commands
log.Println("removing commands...")
registeredCommands, err := dg.ApplicationCommands(app.ID, "")
if err != nil {
log.Panicf("cannot get registered commands: %v", err)
}
for _, v := range registeredCommands {
err = dg.ApplicationCommandDelete(app.ID, "", v.ID)
if err != nil {
log.Panicf("cannot delete '%v' command: %v", v.Name, err)
}
}
log.Println("bot is now exiting. bye!")
}