generated from lazybytez/general-template
/
jojo_command.go
188 lines (175 loc) · 6.45 KB
/
jojo_command.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
/*
* JOJO Discord Bot - An advanced multi-purpose discord bot
* Copyright (C) 2022 Lazy Bytez (Elias Knodel, Pascal Zarrad)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package bot_core
import (
"github.com/bwmarrin/discordgo"
"github.com/lazybytez/jojo-discord-bot/api"
"github.com/lazybytez/jojo-discord-bot/core_components/bot_core/command/auditlog"
"github.com/lazybytez/jojo-discord-bot/core_components/bot_core/command/module"
"github.com/lazybytez/jojo-discord-bot/core_components/bot_core/command/sync_commands"
)
// jojoCommand holds the command configuration for the jojo command.
var jojoCommand *api.Command
// adminMemberPermissions is the default permission used for the jojo command.
var adminMemberPermissions int64 = discordgo.PermissionAdministrator
// getModuleCommandChoices builds a slice containing all available modules
// as command option choices
func getModuleCommandChoices() []*discordgo.ApplicationCommandOptionChoice {
availableModuleChoices := make([]*discordgo.ApplicationCommandOptionChoice, 0)
for _, comp := range C.EntityManager().RegisteredComponent().GetAvailable() {
if comp.IsCoreComponent() {
continue
}
availableModuleChoices = append(availableModuleChoices, &discordgo.ApplicationCommandOptionChoice{
Name: comp.Name,
Value: string(comp.Code),
})
}
return availableModuleChoices
}
// initAndRegisterJojoCommand initializes the jojo command variable and registers the command
// in the command API
func initAndRegisterJojoCommand() {
// Ensure the module package knows about the component
module.C = &C
sync_commands.C = &C
auditlog.C = &C
jojoCommand = &api.Command{
Cmd: &discordgo.ApplicationCommand{
Name: "jojo",
Description: "Manage modules and core settings of the bot!",
DefaultMemberPermissions: &adminMemberPermissions,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "module",
Description: "Manage which modules should be enabled / disabled on your server!",
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "list",
Description: "List all modules and their status",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "show",
Description: "Show information about a specific module",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "module",
Description: "The name of the module to show information about",
Required: true,
Type: discordgo.ApplicationCommandOptionString,
Choices: getModuleCommandChoices(),
},
},
},
{
Name: "enable",
Description: "Enable a module for the guild",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "module",
Description: "The name of the module to enable",
Required: true,
Type: discordgo.ApplicationCommandOptionString,
Choices: getModuleCommandChoices(),
},
},
},
{
Name: "disable",
Description: "Disable a module for the guild",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "module",
Description: "The name of the module to disable",
Required: true,
Type: discordgo.ApplicationCommandOptionString,
Choices: getModuleCommandChoices(),
},
},
},
},
},
{
Name: "sync-commands",
Description: "Trigger a re-synchronisation of slash-commands for the " +
"guild to tackle inconsistencies",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "auditlog",
Description: "Manage settings of the bot audit log!",
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "status",
Description: "Show the status of the current bot audit log configuration",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "enable",
Description: "Enable printing the bot audit log to the configured channel",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "channel",
Description: "The channel where audit log messages should be send to",
Required: true,
Type: discordgo.ApplicationCommandOptionChannel,
ChannelTypes: []discordgo.ChannelType{
discordgo.ChannelTypeGuildText,
},
},
},
},
{
Name: "disable",
Description: "Disable printing the bot audit log to the configured channel",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
},
},
},
},
Category: api.CategoryAdministration,
Handler: handleJojoCommand,
}
_ = C.SlashCommandManager().Register(jojoCommand)
}
// handleJojoCommand handles the parent JOJO command and delegates sub-command
// handling to the appropriate handlers
func handleJojoCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
subCommands := map[string]func(
s *discordgo.Session,
i *discordgo.InteractionCreate,
option *discordgo.ApplicationCommandInteractionDataOption,
){
"module": module.HandleModuleSubCommand,
"sync-commands": sync_commands.HandleSyncCommandSubCommand,
"auditlog": auditlog.HandleAuditLogCommandSubCommand,
}
api.ProcessSubCommands(
s,
i,
nil,
subCommands)
}