-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
204 lines (176 loc) · 6.33 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
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"strings"
"syscall"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/cache"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/disgo/handler"
"github.com/disgoorg/disgo/handler/middleware"
"github.com/disgoorg/snowflake/v2"
"github.com/spf13/viper"
"github.com/myrkvi/heimdallr/commands"
"github.com/myrkvi/heimdallr/components"
_ "github.com/myrkvi/heimdallr/config"
"github.com/myrkvi/heimdallr/listeners"
"github.com/myrkvi/heimdallr/model"
"github.com/myrkvi/heimdallr/scheduled_tasks"
)
var rmGlobalCommands = flag.Bool("rm-global-commands", false, "Remove global commands")
var rmGuildCommands = flag.Uint64("rm-guild-commands", 0, "Remove guild commands for guild specified by ID")
var intents = gateway.IntentGuilds |
gateway.IntentGuildMembers |
gateway.IntentGuildModeration |
gateway.IntentGuildExpressions |
gateway.IntentGuildIntegrations |
gateway.IntentGuildWebhooks |
gateway.IntentGuildInvites |
gateway.IntentGuildVoiceStates |
gateway.IntentGuildMessages |
gateway.IntentGuildMessageReactions |
gateway.IntentDirectMessages |
gateway.IntentDirectMessageReactions |
gateway.IntentMessageContent |
gateway.IntentGuildScheduledEvents |
gateway.IntentAutoModerationConfiguration |
gateway.IntentAutoModerationExecution |
gateway.IntentGuildMessagePolls |
gateway.IntentDirectMessagePolls
func main() {
flag.Parse()
token := viper.GetString("bot.token")
if token == "" {
panic("No bot token found in config file. Please set 'bot.token'.")
}
if *rmGlobalCommands || *rmGuildCommands != 0 {
slog.Info("Removing commands.")
rmCommands(token, *rmGlobalCommands, *rmGuildCommands)
return
}
_, err := model.InitDB(viper.GetString("bot.db"))
if err != nil {
panic(fmt.Errorf("failed to initialize database: %w", err))
}
r := handler.New()
r.Use(middleware.Go)
r.Command("/ping", commands.PingHandler)
r.Command("/quote", commands.QuoteHandler)
r.Command("/warn", commands.WarnHandler)
r.Command("/warnings", commands.UserInfractionsHandler)
r.Route("/infractions", func(r handler.Router) {
r.Command("/list", commands.InfractionsListHandler)
r.Command("/remove", commands.InfractionsRemoveHandler)
})
r.Component("/infractions-user/{offset}", commands.UserInfractionButtonHandler)
r.Component("/infractions-mod/{userID}/{offset}", commands.InfractionsListComponentHandler)
r.Route("/admin", func(r handler.Router) {
r.Component("/show-all-button", commands.AdminShowAllButtonHandler)
r.Command("/info", commands.AdminInfoHandler)
r.Command("/mod-channel", commands.AdminModChannelHandler)
r.Command("/infractions", commands.AdminInfractionsHandler)
r.Command("/gatekeep", commands.AdminGatekeepHandler)
r.Command("/gatekeep-message", commands.AdminGatekeepMessageHandler)
r.Component("/gatekeep-message/button", commands.AdminGatekeepMessageButtonHandler)
r.Modal("/gatekeep-message/modal", commands.AdminGatekeepMessageModalHandler)
r.Command("/join-leave", commands.AdminJoinLeaveHandler)
r.Command("/join-message", commands.AdminJoinMessageHandler)
r.Component("/join-message/button", commands.AdminJoinMessageButtonHandler)
r.Modal("/join-message/modal", commands.AdminJoinMessageModalHandler)
r.Command("/leave-message", commands.AdminLeaveMessageHandler)
r.Component("/leave-message/button", commands.AdminLeaveMessageButtonHandler)
r.Modal("/leave-message/modal", commands.AdminLeaveMessageModalHandler)
})
r.Command("/Approve", commands.ApproveUserCommandHandler)
r.Command("/approve", commands.ApproveSlashCommandHandler)
r.Command("/kick/with-message", commands.KickWithMessageHandler)
r.Command("/ban/with-message", commands.BanWithMessageHandler)
r.Command("/ban/until", commands.BanUntilHandler)
r.Command("/create-role-button", commands.CreateRoleButtonHandler)
r.Component("/role/assign/{roleID}", components.RoleAssignButtonHandler)
r.Command("/prune-pending-members", commands.PruneHandler)
r.Command("/prune-pending-members-dry-run", commands.PruneDryRunHandler)
commandCreates := []discord.ApplicationCommandCreate{
commands.PingCommand,
commands.QuoteCommand,
commands.WarnCommand,
commands.UserInfractionsCommand,
commands.InfractionsCommand,
commands.AdminCommand,
commands.KickCommand,
commands.BanCommand,
commands.ApproveSlashCommand,
commands.ApproveUserCommand,
commands.CreateRoleButtonCommand,
commands.PruneCommand,
commands.PruneDryRunCommand,
}
client, err := disgo.New(token,
bot.WithLogger(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: getLogLevel(viper.GetString("loglevel")),
}))),
bot.WithDefaultGateway(),
bot.WithEventListeners(r),
bot.WithEventListenerFunc(func(e *events.Ready) {
fmt.Println("Bot is ready!")
}),
bot.WithEventListenerFunc(listeners.OnWarnedUserJoin),
bot.WithEventListenerFunc(listeners.OnGatekeepUserJoin),
bot.WithEventListenerFunc(listeners.OnUserJoin),
bot.WithEventListenerFunc(listeners.OnUserLeave),
bot.WithEventListenerFunc(listeners.OnMemberBan),
bot.WithEventListenerFunc(listeners.OnAuditLog),
bot.WithGatewayConfigOpts(gateway.WithIntents(intents)),
bot.WithCacheConfigOpts(
cache.WithCaches(cache.FlagsAll),
),
)
if err != nil {
panic(fmt.Errorf("failed to create disgo client: %w", err))
}
defer client.Close(context.Background())
var devGuilds []snowflake.ID
if viper.GetBool("dev_mode.enabled") {
slog.SetLogLoggerLevel(slog.LevelDebug)
devGuilds = append(devGuilds, snowflake.ID(viper.GetUint64("dev_mode.guild_id")))
}
err = handler.SyncCommands(
client,
commandCreates,
devGuilds,
)
if err != nil {
panic(fmt.Errorf("failed to sync commands: %w", err))
}
err = client.OpenGateway(context.Background())
if err != nil {
panic(fmt.Errorf("failed to open gateway: %w", err))
}
removeTempBansTask := scheduled_tasks.RemoveTempBansScheduledTask(client)
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-s
removeTempBansTask.Stop()
}
func getLogLevel(level string) slog.Level {
switch strings.ToLower(level) {
case "debug":
return slog.LevelDebug
case "info":
return slog.LevelInfo
case "warn":
return slog.LevelWarn
case "error":
return slog.LevelError
default:
return slog.LevelInfo
}
}