-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
142 lines (115 loc) · 3.36 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
/*
This package contains the core code for the Digital Overdose Discord Server's Management Bot.
Current features include:
- Manual Purge
*/
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"time"
"atomicmaya.me/digital-overdose-bot/common"
cron "atomicmaya.me/digital-overdose-bot/cron"
database_utils "atomicmaya.me/digital-overdose-bot/db"
"atomicmaya.me/digital-overdose-bot/extensions"
"atomicmaya.me/digital-overdose-bot/handler"
"github.com/bwmarrin/discordgo"
"github.com/go-co-op/gocron"
)
// The Discord session, used for state management.
var s *discordgo.Session
// Parses the flags provided in `argv`, then loads any overwrites from the .env file.
func init() {
flag.Parse()
common.LoadEnvOrFlags()
}
// Starts up the bot.
func init() {
var err error
s, err = discordgo.New("Bot " + *common.BotToken)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
s.Identify.Intents = discordgo.IntentsAllWithoutPrivileged | discordgo.IntentsGuildMembers
}
func init() {
common.InitializeLogging()
}
func init() {
err := errors.New("")
database_utils.Database, err = database_utils.InitializeDatabase()
if err != nil {
log.Printf("DB INIT failed. ERR: %v", err)
os.Exit(125)
}
}
func init() {
s.AddHandler(handler.OnReady)
s.AddHandler(handler.OnInteractionCreate)
s.AddHandler(handler.OnMessage)
s.AddHandler(handler.OnJoin)
}
// Initializes cron engine, and subsequently registers scheduled functions
func init() {
schedulers := make([]*gocron.Scheduler, len(cron.CronJobs))
for i := 0; i < len(cron.CronJobs); i++ {
schedulers[i] = gocron.NewScheduler(time.UTC)
job := cron.CronJobs[i]
log.Printf("[--] Registered job '%v': '%v'", job.Name, job.CronString)
schedulers[i].Cron(job.CronString).Do(func() {
log.Printf("[+] Executing cron job '%v': '%v'", job.Name, job.CronString)
job.Job(s, nil)
})
schedulers[i].StartAsync()
}
log.Print("[✓] Registering Jobs")
// Start the cron scheduler in another thread.
log.Print("[✓] cron handler Started")
}
// Entry point, loads system and performs clean-up
func main() {
err := s.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
log.Println("Adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(extensions.Commands))
// Registers all of the commands in the designated server.
for i, v := range extensions.Commands {
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, *common.GuildID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
registeredCommands[i] = cmd
}
defer s.Close()
// CTRL+C Signal Handler
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
signal.Notify(stop, os.Kill)
log.Println("Press Ctrl+C to exit")
common.LogAndSend(fmt.Sprintf(":robot::rotating_light: is currently running on version `%v%v", common.VERSION, func() string {
if *common.CURRENTLY_DEV {
return " (DEV VERSION)`"
} else {
return "`"
}
}()), s)
<-stop
log.Println("")
// Unregisters the commands in the designated server.
if *common.RemoveCommands {
log.Println("Removing commands...")
for _, v := range registeredCommands {
s.ApplicationCommandDelete(s.State.User.ID, *common.GuildID, v.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
}
}
log.Printf("Gracefully shutting down.")
}