-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
105 lines (84 loc) · 2.48 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
package main
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/dixtel/dicord-bot-kog/bot"
"github.com/dixtel/dicord-bot-kog/channel"
"github.com/dixtel/dicord-bot-kog/config"
"github.com/dixtel/dicord-bot-kog/models"
"github.com/dixtel/dicord-bot-kog/roles"
"github.com/dixtel/dicord-bot-kog/webserver"
"github.com/glebarez/sqlite"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
)
func main() {
gormDb, err := gorm.Open(sqlite.Open("sqlite.db"), &gorm.Config{})
if err != nil {
log.Error().Err(err).Msg("cannot connect to database")
return
}
err = gormDb.AutoMigrate(
&models.User{},
&models.Map{},
&models.TestingChannel{},
&models.BannedUserFromSubmission{},
)
if err != nil {
log.Error().Err(err).Msg("cannot migrate models")
return
}
db := models.NewDatabase(gormDb.Debug())
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + config.CONFIG.Token)
if err != nil {
log.Error().Err(err).Msg("error creating Discord session")
return
}
botRoles, err := roles.SetupRoles(dg)
if err != nil {
log.Error().Err(err).Msg("cannot setup roles")
return
}
channelManager, err := channel.NewChannelManager(dg, botRoles)
if err != nil {
log.Error().Err(err).Msg("cannot create channel manager")
return
}
// defer bot.SetupBot(dg, db, botRoles, channelManager)()
cleanup, err := bot.SetupBot(dg, db, botRoles, channelManager)
if err != nil {
log.Error().Err(err).Msg("cannot setup bot v2")
return
}
defer cleanup()
dg.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Info().Msg("Bot is up!")
})
// Just like the ping pong example, we only care about receiving message
// events in this example.
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentMessageContent
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
log.Error().Err(err).Msg("error opening connection")
return
}
srv := webserver.Run(dg)
// Wait here until CTRL-C or other term signal is received.
log.Info().Msg("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
// Cleanly close down the Discord session.
err = dg.Close()
if err != nil {
log.Err(err).Msg("cannot close discord bot")
}
if err := srv.Shutdown(context.TODO()); err != nil {
panic(err) // failure/timeout shutting down the server gracefully
}
}