-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
89 lines (75 loc) · 2.19 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
package main
import (
"log"
"os"
"github.com/raikerian/go-remai-bot-discord/pkg/bot"
"github.com/raikerian/go-remai-bot-discord/pkg/commands"
"github.com/raikerian/go-remai-bot-discord/pkg/commands/gpt"
"github.com/raikerian/go-remai-bot-discord/pkg/constants"
"github.com/sashabaranov/go-openai"
"gopkg.in/yaml.v2"
)
type Config struct {
Discord struct {
Token string `yaml:"token"`
Guild string `yaml:"guild"`
RemoveCommands bool `yaml:"removeCommands"`
} `yaml:"discord"`
OpenAI struct {
APIKey string `yaml:"apiKey"`
CompletionModels []string `yaml:"completionModels"`
} `yaml:"openAI"`
}
func (c *Config) ReadFromFile(file string) error {
data, err := os.ReadFile(file)
if err != nil {
return err
}
err = yaml.Unmarshal(data, c)
if err != nil {
return err
}
return nil
}
func init() {
log.SetOutput(os.Stdout)
log.SetFlags(log.LstdFlags | log.Lshortfile)
}
var (
discordBot *bot.Bot
openaiClient *openai.Client
gptMessagesCache *gpt.MessagesCache
ignoredChannelsCache = make(gpt.IgnoredChannelsCache)
)
func main() {
// Read config from file
config := &Config{}
err := config.ReadFromFile("credentials.yaml")
if err != nil {
log.Fatalf("Error reading credentials.yaml: %v", err)
}
// Initialize cache
gptMessagesCache, err = gpt.NewMessagesCache(constants.DiscordThreadsCacheSize)
if err != nil {
log.Fatalf("Error initializing GPTMessagesCache: %v", err)
}
// Initialize discord bot
discordBot, err = bot.NewBot(config.Discord.Token)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
// Register commands
if config.OpenAI.APIKey != "" {
openaiClient = openai.NewClient(config.OpenAI.APIKey) // initialize OpenAI client first
discordBot.Router.Register(commands.ChatCommand(&commands.ChatCommandParams{
OpenAIClient: openaiClient,
OpenAICompletionModels: config.OpenAI.CompletionModels,
GPTMessagesCache: gptMessagesCache,
IgnoredChannelsCache: &ignoredChannelsCache,
}))
discordBot.Router.Register(commands.ImageCommand(openaiClient))
}
discordBot.Router.Register(commands.InfoCommand())
// Run the bot
discordBot.Run(config.Discord.Guild, config.Discord.RemoveCommands)
}