/
main.go
321 lines (283 loc) · 9.8 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/go-co-op/gocron"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/stablecog/sc-go/database"
"github.com/stablecog/sc-go/database/qdrant"
"github.com/stablecog/sc-go/database/repository"
"github.com/stablecog/sc-go/discobot/interactions"
dresponses "github.com/stablecog/sc-go/discobot/responses"
"github.com/stablecog/sc-go/log"
"github.com/stablecog/sc-go/server/analytics"
"github.com/stablecog/sc-go/server/requests"
"github.com/stablecog/sc-go/server/responses"
"github.com/stablecog/sc-go/shared"
"github.com/stablecog/sc-go/shared/queue"
"github.com/stablecog/sc-go/utils"
"golang.org/x/exp/slices"
)
var Version = "dev"
var (
integerOptionMinValue = 1.0
dmPermission = false
defaultMemberPermissions int64 = discordgo.PermissionManageServer
)
func main() {
log.Infof("Starting SC Discobot v%v", Version)
// Load .env
err := godotenv.Load("../.env")
if err != nil {
log.Warn("Error loading .env file (this is fine)", "err", err)
}
s, err := discordgo.New("Bot " + utils.GetEnv().DiscordBotToken)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
ctx := context.Background()
// Setup redis
redis, err := database.NewRedis(ctx)
if err != nil {
log.Fatal("Error connecting to redis", "err", err)
os.Exit(1)
}
// Setup sql
log.Info("🏡 Connecting to database...")
dbconn, err := database.GetSqlDbConn(false)
if err != nil {
log.Fatal("Failed to connect to database", "err", err)
os.Exit(1)
}
entClient, err := database.NewEntClient(dbconn)
if err != nil {
log.Fatal("Failed to create ent client", "err", err)
os.Exit(1)
}
defer entClient.Close()
// Run migrations
// We can't run on supabase, :(
if utils.GetEnv().RunMigrations {
log.Info("🦋 Running migrations...")
if err := entClient.Schema.Create(ctx); err != nil {
log.Fatal("Failed to run migrations", "err", err)
os.Exit(1)
}
}
// Setup qdrant
qdrantClient, err := qdrant.NewQdrantClient(ctx)
if err != nil {
log.Fatal("Error connecting to qdrant", "err", err)
os.Exit(1)
}
// Create repository (database access)
repo := &repository.Repository{
DB: entClient,
ConnInfo: dbconn,
Qdrant: qdrantClient,
Redis: redis,
Ctx: ctx,
}
// Sync map for tracking requests
sMap := shared.NewSyncMap[chan requests.CogWebhookMessage]()
// Make a sync map for tracking login requests
loginInteractionMap := shared.NewSyncMap[*interactions.LoginInteraction]()
// Q Throttler
qThrottler := shared.NewQueueThrottler(ctx, redis.Client, shared.REQUEST_COG_TIMEOUT)
// Get models, schedulers and put in cache
log.Info("📦 Populating cache...")
err = repo.UpdateCache()
if err != nil {
// ! Not getting these is fatal and will result in crash
panic(err)
}
// Update periodically
cronSscheduler := gocron.NewScheduler(time.UTC)
cronSscheduler.Every(5).Minutes().StartAt(time.Now().Add(5 * time.Minute)).Do(func() {
log.Info("📦 Updating cache...")
err = repo.UpdateCache()
if err != nil {
log.Error("Error updating cache", "err", err)
}
})
// Also delete records older than 10 minutes from loginInteractionMap
cronSscheduler.Every(10).Minutes().Do(func() {
items := loginInteractionMap.GetAll()
for k, v := range items {
if time.Since(v.InsertedAt) > 10*time.Minute {
loginInteractionMap.Delete(k)
}
}
})
// Safety checker
safetyChecker := utils.NewTranslatorSafetyChecker(ctx, utils.GetEnv().OpenAIApiKey, false)
// Create analytics service
analyticsService := analytics.NewAnalyticsService()
defer analyticsService.Close()
// Setup rabbitmq client
rabbitmqClient, err := queue.NewRabbitMQClient(ctx, utils.GetEnv().RabbitMQAMQPUrl)
if err != nil {
log.Fatalf("Error connecting to rabbitmq: %v", err)
}
defer rabbitmqClient.Close()
// Setup interactions
cmdWrapper := interactions.NewDiscordInteractionWrapper(repo, redis, database.NewSupabaseAuth(), sMap, qThrottler, safetyChecker, analyticsService, loginInteractionMap, rabbitmqClient)
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Infof("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
// Register messageCreate as a callback for the messageCreate events.
s.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
// check if the message is "!tip"
if strings.HasPrefix(m.Content, "!tip") {
cmdWrapper.HandleTip(s, m)
}
})
// Intents
s.Identify.Intents = discordgo.IntentsGuilds | discordgo.IntentsGuildMessages | discordgo.IntentsMessageContent
err = s.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
// Remove stale commands
existingCommands, err := s.ApplicationCommands(s.State.User.ID, "")
if err != nil {
log.Fatalf("Cannot get existing commands: %v", err)
}
wantedCommandNames := make([]string, len(cmdWrapper.Commands))
for i, v := range cmdWrapper.Commands {
wantedCommandNames[i] = v.ApplicationCommand.Name
}
for _, v := range existingCommands {
if slices.Contains(wantedCommandNames, v.Name) {
continue
}
err := s.ApplicationCommandDelete(s.State.User.ID, "", v.ID)
if err != nil {
log.Fatalf("Cannot delete '%v' command: %v", v.Name, err)
}
}
log.Info("Adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(cmdWrapper.Commands))
for i, v := range cmdWrapper.Commands {
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, "", v.ApplicationCommand)
if err != nil {
log.Fatalf("Cannot create '%v' command: %v", v.ApplicationCommand.Name, err)
}
registeredCommands[i] = cmd
}
// Register handlers
// Components are part of interactions, so we register InteractionCreate handler
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
handler := cmdWrapper.GetHandlerForCommand(i.ApplicationCommandData().Name)
if handler != nil {
handler(s, i)
}
case discordgo.InteractionMessageComponent:
if strings.HasPrefix(i.MessageComponentData().CustomID, "upscale:") {
// String should look like this: upscale:%s:number:%d
splitStr := strings.Split(i.MessageComponentData().CustomID, ":")
if len(splitStr) != 4 {
log.Error("Invalid upscale custom id, parsing length", "custom_id", i.MessageComponentData().CustomID)
return
}
// Parse uuid
outputId, err := uuid.Parse(splitStr[1])
if err != nil {
log.Error("Invalid upscale custom id, parsing uuid", "custom_id", i.MessageComponentData().CustomID)
return
}
// Get number
number, err := strconv.Atoi(splitStr[3])
if err != nil {
log.Error("Invalid upscale custom id, parsing number", "custom_id", i.MessageComponentData().CustomID)
return
}
cmdWrapper.HandleUpscaleGeneration(s, i, outputId, number)
} else {
handler := cmdWrapper.GetHandlerForComponent(i.MessageComponentData().CustomID)
if handler != nil {
handler(s, i)
}
}
}
})
defer s.Close()
// This redis subscription has the following purpose:
// For API token requests, they are synchronous with API requests
// so we need to send the response back to the appropriate channel
apiTokenChannel := redis.Client.Subscribe(ctx, shared.REDIS_APITOKEN_COG_CHANNEL)
defer apiTokenChannel.Close()
// Start SSE redis subscription
go func() {
log.Info("Listening for api messages", "channel", shared.REDIS_APITOKEN_COG_CHANNEL)
for msg := range apiTokenChannel.Channel() {
var cogMessage requests.CogWebhookMessage
err := json.Unmarshal([]byte(msg.Payload), &cogMessage)
if err != nil {
log.Error("Error unmarshalling cog webhook message", "err", err)
continue
}
if chl := sMap.Get(cogMessage.Input.ID.String()); chl != nil {
chl <- cogMessage
}
}
}()
// This channel tells the bot when a user has authenticated with Stablecog
// so we can send them a message
stableCogAuthChannel := redis.Client.Subscribe(ctx, shared.REDIS_DISCORD_COG_CHANNEL)
defer stableCogAuthChannel.Close()
// Start SSE redis subscription
go func() {
log.Info("Listening for stablecog auth messages", "channel", shared.REDIS_DISCORD_COG_CHANNEL)
for msg := range stableCogAuthChannel.Channel() {
var authMsg responses.DiscordRedisStreamMessage
err := json.Unmarshal([]byte(msg.Payload), &authMsg)
if err != nil {
log.Error("Error unmarshalling sc auth message", "err", err)
continue
}
dmUser, err := s.User(authMsg.DiscordId)
if err != nil {
log.Error("Error getting user information", "err", err)
continue
}
dmChannel, err := s.UserChannelCreate(authMsg.DiscordId)
if err != nil {
log.Error("Error creating dm channel", "err", err)
continue
}
s.ChannelMessageSendEmbed(dmChannel.ID, dresponses.NewEmbed(fmt.Sprintf("👋 Hi, @%s!", dmUser.Username), "I'm Stuart, the Stablecog bot. I'm here to provide you a suite of AI tools.\n\nTry one of the following commands:\n\n/imagine\n/speak", ""))
// See if interaction exists in sync map too
i := loginInteractionMap.Get(authMsg.DiscordId)
if i != nil {
// Update interaction
dresponses.InteractionEdit(i.Session, i.Interaction, &dresponses.InteractionResponseOptions{
EmbedTitle: "✅ Authenticated!",
EmbedContent: "You have successfully authenticated with Stablecog! You can now use various commands.\n\nTry one of the following commands:\n\n/imagine\n/speak",
})
// Remove from sync map
loginInteractionMap.Delete(authMsg.DiscordId)
}
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM, syscall.SIGKILL)
log.Info("Press Ctrl+C to exit")
<-stop
log.Info("Gracefully shutting down.")
}