-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
236 lines (202 loc) · 5.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
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
package main
import (
"errors"
"github.com/automuteus/automuteus/discord/command"
"github.com/automuteus/utils/pkg/locale"
storage2 "github.com/automuteus/utils/pkg/storage"
"github.com/bwmarrin/discordgo"
"io"
"log"
"math/rand"
"os"
"os/signal"
"path"
"strconv"
"strings"
"syscall"
"time"
"github.com/automuteus/automuteus/storage"
"github.com/automuteus/automuteus/discord"
)
var (
version = "7.0.0"
commit = "none"
date = "unknown"
)
const DefaultURL = "http://localhost:8123"
type registeredCommand struct {
GuildID string
ApplicationCommand *discordgo.ApplicationCommand
}
func main() {
// seed the rand generator (used for making connection codes)
rand.Seed(time.Now().Unix())
err := discordMainWrapper()
if err != nil {
log.Println("Program exited with the following error:")
log.Println(err)
return
}
}
func discordMainWrapper() error {
var isOfficial = os.Getenv("AUTOMUTEUS_OFFICIAL") != ""
discordToken := os.Getenv("DISCORD_BOT_TOKEN")
if discordToken == "" {
return errors.New("no DISCORD_BOT_TOKEN provided")
}
logPath := os.Getenv("LOG_PATH")
if logPath == "" {
logPath = "./"
}
logEntry := os.Getenv("DISABLE_LOG_FILE")
if logEntry == "" {
file, err := os.Create(path.Join(logPath, "logs.txt"))
if err != nil {
return err
}
mw := io.MultiWriter(os.Stdout, file)
log.SetOutput(mw)
}
emojiGuildID := os.Getenv("EMOJI_GUILD_ID")
log.Println(version + "-" + commit)
if os.Getenv("WORKER_BOT_TOKENS") != "" {
log.Println("WORKER_BOT_TOKENS is now a variable used by Galactus, not AutoMuteUs!")
log.Fatal("Move WORKER_BOT_TOKENS to Galactus' config, then try again")
}
numShardsStr := os.Getenv("NUM_SHARDS")
numShards, err := strconv.Atoi(numShardsStr)
if err != nil {
log.Println("No NUM_SHARDS specified; defaulting to 1")
numShards = 1
}
shardIDStr := os.Getenv("SHARD_ID")
shardID, err := strconv.Atoi(shardIDStr)
if shardID >= numShards {
return errors.New("you specified a shardID higher than or equal to the total number of shards")
}
if err != nil {
log.Println("No SHARD_ID specified; defaulting to 0")
shardID = 0
}
url := os.Getenv("HOST")
if url == "" {
log.Printf("[Info] No valid HOST provided. Defaulting to %s\n", DefaultURL)
url = DefaultURL
}
var redisClient discord.RedisInterface
var storageInterface storage.StorageInterface
redisAddr := os.Getenv("REDIS_ADDR")
redisPassword := os.Getenv("REDIS_PASS")
if redisAddr != "" {
err := redisClient.Init(storage.RedisParameters{
Addr: redisAddr,
Username: "",
Password: redisPassword,
})
if err != nil {
log.Println(err)
}
err = storageInterface.Init(storage.RedisParameters{
Addr: redisAddr,
Username: "",
Password: redisPassword,
})
if err != nil {
log.Println(err)
}
} else {
return errors.New("no REDIS_ADDR specified; exiting")
}
galactusAddr := os.Getenv("GALACTUS_ADDR")
if galactusAddr == "" {
return errors.New("no GALACTUS_ADDR specified; exiting")
}
galactusClient, err := discord.NewGalactusClient(galactusAddr)
if err != nil {
log.Println("Error connecting to Galactus!")
return err
}
locale.InitLang(os.Getenv("LOCALE_PATH"), os.Getenv("BOT_LANG"))
psql := storage2.PsqlInterface{}
pAddr := os.Getenv("POSTGRES_ADDR")
if pAddr == "" {
return errors.New("no POSTGRES_ADDR specified; exiting")
}
pUser := os.Getenv("POSTGRES_USER")
if pUser == "" {
return errors.New("no POSTGRES_USER specified; exiting")
}
pPass := os.Getenv("POSTGRES_PASS")
if pPass == "" {
return errors.New("no POSTGRES_PASS specified; exiting")
}
err = psql.Init(storage2.ConstructPsqlConnectURL(pAddr, pUser, pPass))
if err != nil {
return err
}
if !isOfficial {
go func() {
err := psql.LoadAndExecFromFile("./storage/postgres.sql")
if err != nil {
log.Println("Exiting with fatal error when attempting to execute postgres.sql:")
log.Fatal(err)
}
}()
}
log.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
topGGToken := os.Getenv("TOP_GG_TOKEN")
bot := discord.MakeAndStartBot(version, commit, discordToken, topGGToken, url, emojiGuildID, numShards, shardID, &redisClient, &storageInterface, &psql, galactusClient, logPath)
if bot == nil {
log.Fatal("bot failed to initialize; did you provide a valid Discord Bot Token?")
}
// empty string entry = global
slashCommandGuildIds := []string{""}
slashCommandGuildIdStr := strings.ReplaceAll(os.Getenv("SLASH_COMMAND_GUILD_IDS"), " ", "")
if slashCommandGuildIdStr != "" {
slashCommandGuildIds = strings.Split(slashCommandGuildIdStr, ",")
}
var registeredCommands []registeredCommand
if !isOfficial || shardID == 0 {
for _, guild := range slashCommandGuildIds {
for _, v := range command.All {
if guild == "" {
log.Printf("Registering command %s GLOBALLY\n", v.Name)
} else {
log.Printf("Registering command %s in guild %s\n", v.Name, guild)
}
id, err := bot.PrimarySession.ApplicationCommandCreate(bot.PrimarySession.State.User.ID, guild, v)
if err != nil {
log.Panicf("Cannot create command: %v", err)
} else {
registeredCommands = append(registeredCommands, registeredCommand{
GuildID: guild,
ApplicationCommand: id,
})
}
}
}
log.Println("Finishing registering all commands!")
}
<-sc
log.Printf("Received Sigterm or Kill signal. Bot will terminate in 1 second")
time.Sleep(time.Second)
if !isOfficial {
log.Println("Deleting slash commands")
for _, v := range registeredCommands {
if v.GuildID == "" {
log.Printf("Deleting command %s GLOBALLY\n", v.ApplicationCommand.Name)
} else {
log.Printf("Deleting command %s on guild %s\n", v.ApplicationCommand.Name, v.GuildID)
}
err = bot.PrimarySession.ApplicationCommandDelete(v.ApplicationCommand.ApplicationID, v.GuildID, v.ApplicationCommand.ID)
if err != nil {
log.Println(err)
}
}
log.Println("Finished deleting all commands")
}
bot.Close()
return nil
}