-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
396 lines (338 loc) · 10.4 KB
/
main.go
File metadata and controls
396 lines (338 loc) · 10.4 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package main
import (
"context"
e "embed"
"log/slog"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/TheTipo01/YADMB/api"
"github.com/TheTipo01/YADMB/constants"
"github.com/TheTipo01/YADMB/database/mysql"
"github.com/TheTipo01/YADMB/database/sqlite"
"github.com/TheTipo01/YADMB/embed"
"github.com/TheTipo01/YADMB/manager"
"github.com/TheTipo01/YADMB/spotify"
"github.com/TheTipo01/YADMB/youtube"
"github.com/bwmarrin/lit"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/disgo/voice"
"github.com/disgoorg/godave/golibdave"
"github.com/disgoorg/snowflake/v2"
"github.com/gin-gonic/gin"
"github.com/kkyr/fig"
)
var (
// Holds all the info about a server
server = make(map[snowflake.ID]*manager.Server)
// String for storing the owners of the bot
owners map[snowflake.ID]struct{}
// Discord bot token
token string
// Cache for the user blacklist
blacklist *sync.Map
// Clients
clients manager.Clients
// Web API
webApi *api.Api
// Array of long lived tokens
longLivedTokens []apiToken
//go:embed all:web/build/*
buildFS e.FS
// Origin for CORS and link generation
origin string
// Server mutex
serverMutex sync.RWMutex
// If set to true, the bot will only respond to commands coming from guilds in the guild list
whitelist bool
// List of guilds the bot will respond to
guildList *sync.Map
// Channel used to notify the presence updater that the guild count has changed
guildCountChan = make(chan struct{})
)
func init() {
lit.LogLevel = lit.LogError
gin.SetMode(gin.ReleaseMode)
var cfg Config
err := fig.Load(&cfg, fig.File("config.yml"), fig.Dirs(".", "./data"))
if err != nil {
lit.Error(err.Error())
return
}
// Config file found
token = cfg.Token
owners = make(map[snowflake.ID]struct{}, len(cfg.Owner))
for _, o := range cfg.Owner {
owners[o] = struct{}{}
}
longLivedTokens = cfg.ApiTokens
origin = cfg.Origin
// Set lit.LogLevel to the given value
switch strings.ToLower(cfg.LogLevel) {
case "logwarning", "warning":
lit.LogLevel = lit.LogWarning
case "loginformational", "informational":
lit.LogLevel = lit.LogInformational
case "logdebug", "debug":
lit.LogLevel = lit.LogDebug
}
if cfg.ClientID != "" && cfg.ClientSecret != "" {
clients.Spotify, err = spotify.NewSpotify(cfg.ClientID, cfg.ClientSecret)
if err != nil {
lit.Error("spotify: couldn't get token: %s", err)
}
}
// Start the API, if enabled
if cfg.Address != "" {
webApi = api.NewApi(server, cfg.Address, owners, &clients, &buildFS, origin)
}
// Initialize the database
switch cfg.Driver {
case "sqlite", "sqlite3":
clients.Database = sqlite.NewDatabase(cfg.DSN)
case "mysql":
clients.Database = mysql.NewDatabase(cfg.DSN)
}
// And load custom commands from the db
commands, _ := clients.Database.GetCustomCommands()
for k := range commands {
if server[k] == nil {
initializeServer(k)
}
server[k].Custom = commands[k]
}
// Load the blacklist
blacklist, err = clients.Database.GetBlacklist()
if err != nil {
lit.Error("Error loading blacklist: %s", err)
}
// Load the DJ settings
dj, err := clients.Database.GetDJ()
if err != nil {
lit.Error("Error loading DJ settings: %s", err)
}
for k := range dj {
if server[k] == nil {
initializeServer(k)
}
server[k].DjMode = dj[k].Enabled
server[k].DjRole = dj[k].Role
}
// Load the whitelist
whitelist = cfg.WhiteList
guildList = &sync.Map{}
for _, g := range cfg.GuildList {
guildList.Store(g, struct{}{})
}
// Create folders used by the bot
if _, err = os.Stat(constants.CachePath); err != nil {
if err = os.Mkdir(constants.CachePath, 0755); err != nil {
lit.Error("Cannot create %s, %s", constants.CachePath, err)
}
}
// If yt-dlp is not terminated gracefully when downloading, it will leave a file called --Frag1
_ = os.Remove("--Frag1")
// Checks useful for knowing if every dependency exists
if manager.IsCommandNotAvailable("dca") {
lit.Error("Error: can't find dca!")
}
if manager.IsCommandNotAvailable("ffmpeg") {
lit.Error("Error: can't find ffmpeg!")
}
if manager.IsCommandNotAvailable("yt-dlp") {
lit.Error("Error: can't find yt-dlp!")
}
if cfg.YouTubeAPI != "" {
clients.Youtube, err = youtube.NewYoutube(cfg.YouTubeAPI)
if err != nil {
lit.Error("youtube: couldn't get client: %s", err)
}
}
go presenceUpdater()
}
func main() {
if token == "" {
lit.Error("No token provided. Please modify config.yml")
return
}
logger := slog.Default()
if lit.LogLevel == lit.LogDebug {
logger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
}
client, _ := disgo.New(token,
bot.WithGatewayConfigOpts(
gateway.WithIntents(
gateway.IntentGuildVoiceStates,
gateway.IntentGuilds,
),
),
bot.WithCacheConfigOpts(
cache.WithCaches(
cache.FlagVoiceStates,
),
),
bot.WithEventListenerFunc(ready),
bot.WithEventListenerFunc(guildCreate),
bot.WithEventListenerFunc(guildDelete),
//bot.WithEventListenerFunc(voiceStateUpdate),
bot.WithEventListenerFunc(guildMemberUpdate),
bot.WithEventListenerFunc(interactionCreate),
bot.WithVoiceManagerConfigOpts(voice.WithDaveSessionCreateFunc(golibdave.NewSession)),
bot.WithLogger(logger),
)
defer client.Close(context.TODO())
if err := client.OpenGateway(context.TODO()); err != nil {
lit.Error("errors while connecting to gateway %s", err)
return
}
// Register commands
_, err := client.Rest.SetGlobalCommands(client.ApplicationID, commands)
if err != nil {
lit.Error("Error registering commands: %s", err)
return
}
// Start the web API, if enabled
if webApi != nil {
go webApi.HandleNotifications()
if len(longLivedTokens) > 0 {
lit.Info("Loading long lived tokens")
for _, t := range longLivedTokens {
userInfo := api.UserInfo{
LongLivedToken: t.Token,
Guild: t.Guild,
TextChannel: t.TextChannel,
}
user, err := client.Rest.GetMember(t.Guild, t.UserID)
if err != nil {
lit.Error("Error loading long lived token for user %s in guild %s: %s", t.UserID, t.Guild, err)
} else {
webApi.AddLongLivedToken(user, userInfo)
}
}
}
}
// Print guilds the bot is connected to
if lit.LogLevel == lit.LogDebug {
lit.Debug("Bot is connected to %d guilds.", len(server))
for id := range server {
lit.Debug("Guild ID: %s", id.String())
}
}
// Save the session
clients.Discord = client
// Wait here until CTRL-C or another term signal is received.
lit.Info("YADMB is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// And the DB connection
clients.Database.Close()
}
// presenceUpdater updates the bot presence every time the guild count changes, with a debounce of 500ms to avoid making too many requests
func presenceUpdater() {
debounceTimer := time.NewTimer(0)
debounceTimer.Stop()
for {
select {
case <-guildCountChan:
debounceTimer.Reset(500 * time.Millisecond)
case <-debounceTimer.C:
if clients.Discord != nil {
_ = clients.Discord.SetPresence(context.TODO(), gateway.WithCustomActivity("Serving "+strconv.Itoa(len(server))+" guilds!"))
}
}
}
}
func notifyGuildCountChange() {
select {
case guildCountChan <- struct{}{}:
default:
}
}
func ready(e *events.Ready) {
notifyGuildCountChange()
manager.BotName = e.User.Username
}
// Initialize Server structure
func guildCreate(e *events.GuildReady) {
initializeServer(e.GuildID)
notifyGuildCountChange()
}
func guildDelete(e *events.GuildLeave) {
if server[e.GuildID].IsPlaying() {
ClearAndExit(server[e.GuildID])
}
// Update the status
notifyGuildCountChange()
}
// Update the voice channel when the bot is moved
func voiceStateUpdate(v *events.GuildVoiceStateUpdate) {
// If the bot is alone in the voice channel, stop the music
if server[v.VoiceState.GuildID].VC.IsConnected() {
channel := server[v.VoiceState.GuildID].VC.GetChannelID()
if (v.VoiceState.ChannelID == channel || (v.OldVoiceState.ChannelID != nil && v.OldVoiceState.ChannelID == channel)) && countVoiceStates(v.Client(), v.VoiceState.GuildID, *channel) == 0 {
go QuitIfEmptyVoiceChannel(server[v.VoiceState.GuildID])
}
}
}
func guildMemberUpdate(m *events.GuildMemberUpdate) {
// If we've been timed out, stop the music
if m.Member.User.ID == m.Client().ApplicationID && m.Member.CommunicationDisabledUntil != nil &&
m.Member.CommunicationDisabledUntil.After(time.Now()) && server[m.GuildID].IsPlaying() {
ClearAndExit(server[m.GuildID])
}
}
func interactionCreate(e *events.ApplicationCommandInteractionCreate) {
data := e.SlashCommandInteractionData()
// Ignores commands from DM
if e.Context() == discord.InteractionContextTypeGuild {
if _, ok := blacklist.Load(e.User().ID.String()); ok {
embed.SendAndDeleteEmbedInteraction(discord.NewEmbed().WithTitle(manager.BotName).AddField(constants.ErrorTitle,
constants.UserInBlacklist, false).
WithColor(0x7289DA), e, time.Second*3, nil)
} else {
if whitelist {
// Whitelist mode: check if the guild is in the list
if _, ok = guildList.Load(e.GuildID().String()); ok {
if h, ok := commandHandlers[data.CommandName()]; ok {
go h(e)
}
} else {
embed.SendAndDeleteEmbedInteraction(discord.NewEmbed().WithTitle(manager.BotName).AddField(constants.ErrorTitle,
constants.ServerNotInWhitelist, false).
WithColor(0x7289DA), e, time.Second*3, nil)
}
} else {
// Blacklist mode: check if the guild is not in the list
if _, ok = guildList.Load(e.GuildID().String()); !ok {
if h, ok := commandHandlers[data.CommandName()]; ok {
go h(e)
}
} else {
embed.SendAndDeleteEmbedInteraction(discord.NewEmbed().WithTitle(manager.BotName).AddField(constants.ErrorTitle,
constants.ServerInBlacklist, false).
WithColor(0x7289DA), e, time.Second*3, nil)
}
}
}
} else {
if _, ok := blacklist.Load(e.User().ID.String()); ok {
embed.SendAndDeleteEmbedInteraction(discord.NewEmbed().WithTitle(manager.BotName).AddField(constants.ErrorTitle,
constants.UserInBlacklist, false).
WithColor(0x7289DA), e, time.Second*3, nil)
} else {
embed.SendAndDeleteEmbedInteraction(discord.NewEmbed().WithTitle(manager.BotName).AddField(constants.ErrorTitle,
constants.ErrorDM, false).
WithColor(0x7289DA), e, time.Second*15, nil)
}
}
}