-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
132 lines (104 loc) · 2.98 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
package main
import (
"encoding/json"
"flag"
"os"
"os/signal"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"github.com/monzim/uiuBot/bot"
"github.com/monzim/uiuBot/commands"
db "github.com/monzim/uiuBot/database"
"github.com/monzim/uiuBot/models"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
var (
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
BotToken = flag.String("token", "", "Bot access token")
RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutting down or not")
)
func init() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
log.Info().Msg("Initializing the bot...")
err := godotenv.Load()
if err != nil {
log.Error().Err(err).Msg("Error loading .env file")
}
*BotToken = os.Getenv("DISCORD_TOKEN")
}
func main() {
defer handleUnexpectedPanics()
log.Info().Msg("Starting the bot...")
flag.Parse()
postgres, err := db.NewDatabaseConnection(os.Getenv("DATABASE_URI"))
if err != nil {
log.Error().Err(err).Msg("Error initializing the database connection")
}
logPg, err := db.NewDatabaseConnection(os.Getenv("LOG_DATABASE_URI"))
if err != nil {
log.Error().Err(err).Msg("Error initializing the database connection 2")
}
err = db.Migrate(postgres)
if err != nil {
log.Error().Err(err).Msg("Error migrating the database")
}
err = db.Migrate(logPg)
if err != nil {
log.Error().Err(err).Msg("Error migrating the database 2")
}
myBot, err := bot.NewBot(*BotToken, *GuildID, *RemoveCommands, postgres, logPg)
if err != nil {
log.Error().Err(err).Msg("Invalid bot parameters")
}
defer myBot.Close()
err = myBot.Open()
if err != nil {
log.Fatal().Err(err).Msg("Cannot open the session")
}
go myBot.LogServerStats()
go func() {
myBot.Session.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
jn, err := json.Marshal(m)
if err != nil {
log.Error().Err(err).Msg("Error marshalling the message")
return
}
logPg.Create(models.MessageLog{
ID: m.ID,
ServerID: m.GuildID,
UserID: m.Author.ID,
ChannelID: m.ChannelID,
Message: m.Content,
Data: jn,
})
})
}()
go myBot.PingServerStatus()
go myBot.SendNotices()
go myBot.CronSchedule()
// go myBot.SendServerStatsToChannel()
// list all commands
myBot.ListCommands(*GuildID)
myBot.AddCommandHandlers()
log.Info().Msg("Adding commands...")
registeredCommands := myBot.RegisterCommands(commands.GetCommands(myBot.DB), *GuildID)
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
log.Info().Msg("Press Ctrl+C to exit")
<-stop
if *RemoveCommands {
log.Warn().Msg("Removing commands...")
myBot.RemoveCommands(registeredCommands, *GuildID)
}
log.Info().Msg("Gracefully shutting down.")
}
func handleUnexpectedPanics() {
if r := recover(); r != nil {
log.Error().Interface("panic", r).Msg("Unexpected panic")
os.Exit(1)
}
}