/
main.go
155 lines (134 loc) · 3.66 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/jaspersurmont/rs-drop-simulator/logger"
"github.com/jaspersurmont/rs-drop-simulator/simulations"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
var (
log logger.LoggerWrapper
discord *discordgo.Session
amountGuilds = 0
fullyStarted bool
commands = []*discordgo.ApplicationCommand{
simulations.GiantMoleCommand,
simulations.ZilyanaCommand,
simulations.GraardorCommand,
simulations.KreearraCommand,
simulations.KrilCommand,
simulations.NexCommand,
simulations.VindictaCommand,
simulations.HelwyrCommand,
simulations.TwinfuriesCommand,
simulations.GregorovicCommand,
simulations.VoragoCommand,
simulations.RakshaCommand,
HelpCommand,
ContributeCommand,
InviteCommand,
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"giantmole": simulations.GiantMole,
"graardor": simulations.Graardor,
"zilyana": simulations.Zilyana,
"kreearra": simulations.Kreearra,
"kril": simulations.Kril,
"nex": simulations.Nex,
"vindicta": simulations.Vindicta,
"helwyr": simulations.Helwyr,
"twinfuries": simulations.Twinfuries,
"gregorovic": simulations.Gregorovic,
"vorago": simulations.Vorago,
"raksha": simulations.Raksha,
"help": Help,
"contribute": Contribute,
"invite": Invite,
}
botToken string
)
func init() {
log = logger.CreateLogger("main")
godotenv.Load()
botToken = os.Getenv("DISCORD_BOT_TOKEN")
var err error
discord, err = discordgo.New(fmt.Sprintf("Bot %v", botToken))
if err != nil {
log.Fatal(fmt.Sprintf("couldn't set up bot; %v", err))
}
discord.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
discord.AddHandler(func(s *discordgo.Session, i *discordgo.GuildCreate) {
if !fullyStarted {
return
}
amountGuilds++
log.Info(fmt.Sprintf("bot joined guild; new amount: %v", amountGuilds),
"guildName", i.Guild.Name,
"amountGuilds", amountGuilds,
)
})
discord.AddHandler(func(s *discordgo.Session, i *discordgo.GuildDelete) {
if !fullyStarted {
return
}
amountGuilds--
log.Info(fmt.Sprintf("bot left guild; new amount: %v", amountGuilds),
"amountGuilds", amountGuilds,
)
})
}
func main() {
defer log.Sync()
defer discord.Close()
startBot()
}
// Starts the connection and awaits a termination signal
func startBot() {
err := discord.Open()
if err != nil {
log.Fatal(fmt.Sprintf("error opening connection %v", err))
}
// Use guild only commands when testing, to propagate changes faster
env := os.Getenv("RS_DROP_SIMULATOR_ENV")
guildId := "512644466281152526"
if env == "PROD" {
guildId = ""
}
ch := make(chan bool, 50)
for _, c := range commands {
go func(c *discordgo.ApplicationCommand, guildId string, ch chan<- bool) {
_, err := discord.ApplicationCommandCreate(discord.State.User.ID, guildId, c)
if err != nil {
log.Error(fmt.Sprintf("can't add application command %v", c.Name),
"err", err,
)
ch <- false
} else {
log.Debug("added application command", "name", c.Name)
ch <- true
}
}(c, guildId, ch)
}
// Wait for all the commands to be created (or failed)
for range commands {
<-ch
}
amountGuilds = len(discord.State.Guilds)
log.Info("Bot succesfully started up and listening",
"amountGuilds", amountGuilds,
)
fullyStarted = true
// Await termination
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, os.Interrupt, syscall.SIGTERM)
s := <-sc
log.Info(fmt.Sprintf("shutting down with signal %v", s))
discord.Close()
}