-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
commandhandler.go
148 lines (131 loc) · 4.22 KB
/
commandhandler.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
package inits
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/go-redis/redis/v8"
"github.com/sarulabs/di/v2"
"github.com/zekroTJA/shinpuru/internal/messagecommands"
"github.com/zekroTJA/shinpuru/internal/middleware"
"github.com/zekroTJA/shinpuru/internal/services/permissions"
"github.com/zekroTJA/shinpuru/internal/slashcommands"
"github.com/zekroTJA/shinpuru/internal/usercommands"
"github.com/zekroTJA/shinpuru/internal/util/embedded"
"github.com/zekroTJA/shinpuru/internal/util/static"
"github.com/zekroTJA/shinpuru/pkg/rediscmdstore"
"github.com/zekrotja/dgrs"
"github.com/zekrotja/ken"
"github.com/zekrotja/ken/middlewares/cmdhelp"
"github.com/zekrotja/ken/state"
"github.com/zekrotja/ken/store"
"github.com/zekrotja/rogu/log"
)
func InitCommandHandler(container di.Container) (k *ken.Ken, err error) {
session := container.Get(static.DiDiscordSession).(*discordgo.Session)
st := container.Get(static.DiState).(*dgrs.State)
perms := container.Get(static.DiPermissions).(*permissions.Permissions)
rd, _ := container.Get(static.DiRedis).(*redis.Client)
log := log.Tagged("CmdHandler")
log.Info().Msg("Initializing command handler ...")
var cmdStore store.CommandStore
if rd != nil {
cmdStore = rediscmdstore.New(rd, fmt.Sprintf("snp:cmdstore:%s", embedded.AppCommit))
}
k, err = ken.New(session, ken.Options{
State: state.NewDgrs(st),
CommandStore: cmdStore,
DependencyProvider: container,
OnSystemError: systemErrorHandler,
OnCommandError: commandErrorHandler,
OnEventError: eventErrorHandler,
EmbedColors: ken.EmbedColors{
Default: static.ColorEmbedDefault,
Error: static.ColorEmbedError,
},
})
if err != nil {
return
}
err = k.RegisterCommands(
new(usercommands.User),
new(messagecommands.Quote),
new(slashcommands.Autorole),
new(slashcommands.Autovc),
new(slashcommands.Backup),
new(slashcommands.Bug),
new(slashcommands.Clear),
new(slashcommands.Vote),
new(slashcommands.Report),
new(slashcommands.Mute),
new(slashcommands.Perms),
new(slashcommands.Chanstats),
new(slashcommands.Exec),
new(slashcommands.Say),
new(slashcommands.Notify),
new(slashcommands.Mvall),
new(slashcommands.Lock),
new(slashcommands.Inviteblock),
new(slashcommands.Ghostping),
new(slashcommands.Voicelog),
new(slashcommands.Modlog),
new(slashcommands.Announcements),
new(slashcommands.Starboard),
new(slashcommands.Colorreation),
new(slashcommands.User),
new(slashcommands.Twitchnotify),
new(slashcommands.Tag),
new(slashcommands.Presence),
new(slashcommands.Login),
new(slashcommands.Quote),
new(slashcommands.Stats),
new(slashcommands.Karma),
new(slashcommands.Guild),
new(slashcommands.Id),
new(slashcommands.Snowflake),
new(slashcommands.Maintenance),
new(slashcommands.Info),
new(slashcommands.Help),
new(slashcommands.Birthday),
new(slashcommands.Kick),
new(slashcommands.Ban),
new(slashcommands.Roleselect),
new(slashcommands.Modnot),
)
if err != nil {
return
}
if !embedded.IsRelease() {
err = k.RegisterCommands(new(slashcommands.Debug))
if err != nil {
return
}
}
err = k.RegisterMiddlewares(
middleware.NewDisableCommandsMiddleware(container),
perms,
cmdhelp.New("help"),
middleware.NewCommandStatsMiddleware(),
middleware.NewCommandLoggingMiddleware(container),
)
return
}
func systemErrorHandler(context string, err error, args ...interface{}) {
log.Error().Err(err).Field("ctx", context).Msg("Ken System Error")
}
func eventErrorHandler(context string, err error) {
log.Error().Err(err).Field("context", context).Msg("Ken Event Error")
}
func commandErrorHandler(err error, ctx *ken.Ctx) {
// Is ignored if interaction has already been responded
ctx.Defer()
if err == ken.ErrNotDMCapable {
ctx.FollowUpError("This command can not be used in DMs.", "").Send()
return
}
log.Tagged("Ken").Error().
Err(err).
Field("cmd", ctx.Command.Name()).
Msg("Unexpected command result")
ctx.FollowUpError(
fmt.Sprintf("The command execution failed unexpectedly:\n```\n%s\n```", err.Error()),
"Command execution failed").Send()
}