Skip to content

Commit 497afee

Browse files
authored
Merge pull request #1067 from LEDBrain/dev
feat: Add settings command for command enabling/disabling
2 parents efdfe17 + 5f4e857 commit 497afee

File tree

4 files changed

+75
-29
lines changed

4 files changed

+75
-29
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"joi": "^17.13.1",
4040
"jsdom": "^24.0.0",
4141
"node-libcurl": "^4.0.0",
42-
"zod": "^3.23.5"
42+
"zod": "^3.23.6"
4343
},
4444
"devDependencies": {
4545
"@hapi/code": "^9.0.3",
@@ -61,7 +61,7 @@
6161
"prettier": "^3.2.5",
6262
"prisma": "^5.13.0",
6363
"rimraf": "^5.0.5",
64-
"tsx": "^4.8.2",
64+
"tsx": "^4.9.0",
6565
"typescript": "^5.4.5"
6666
},
6767
"config": {

src/base/Base.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { GuildSettings } from '@prisma/client';
21
import type { Guild, TextChannel } from 'discord.js';
32
import { ChannelType } from 'discord.js';
43
import packageJson from '../../package.json' with { type: 'json' };
@@ -12,7 +11,7 @@ export default abstract class Base {
1211
this.db = prisma;
1312
this.Sanction = SanctionManager;
1413
}
15-
async getGuildSettings(guildId: string): Promise<GuildSettings | null> {
14+
async getGuildSettings(guildId: string) {
1615
return await this.db.guildSettings.findUnique({
1716
where: {
1817
id: guildId,

src/commands/Settings.ts

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import {
33
ChannelType,
44
PermissionFlagsBits,
55
SlashCommandBuilder,
6+
inlineCode,
67
} from 'discord.js';
8+
import { client } from 'index.js';
79
import type { Config } from '../base/Command.js';
810
import Command from '../base/Command.js';
911

@@ -68,7 +70,9 @@ export default class Ping extends Command {
6870
.addSubcommand(subcommand =>
6971
subcommand
7072
.setName('ban-approvals')
71-
.setDescription('Set ban approvals')
73+
.setDescription(
74+
'Set the numbers of ban approvals needed to ban the user.'
75+
)
7276
.addNumberOption(numberOption =>
7377
numberOption
7478
.setName('ban-approvals-setting')
@@ -94,6 +98,31 @@ export default class Ping extends Command {
9498
)
9599
.setRequired(true)
96100
)
101+
)
102+
.addSubcommand(subcommand =>
103+
subcommand
104+
.setName('enabled-commands')
105+
.setDescription('Toggle active state of commands')
106+
.addStringOption(stringOption =>
107+
stringOption
108+
.setName('command-name')
109+
.setDescription(
110+
'The command name to toggle on/off.'
111+
)
112+
.setRequired(true)
113+
.setChoices(
114+
...client.commands.map(command => ({
115+
name: command.name,
116+
value: command.name,
117+
}))
118+
)
119+
)
120+
.addBooleanOption(booleanOption =>
121+
booleanOption
122+
.setName('enabled')
123+
.setDescription('Set the status')
124+
.setRequired(true)
125+
)
97126
);
98127

99128
super(cmd as unknown as Config);
@@ -112,6 +141,9 @@ export default class Ping extends Command {
112141
case 'welcome-message':
113142
this.setWelcomeMessage(interaction);
114143
break;
144+
case 'enabled-commands':
145+
this.setEnabledCommand(interaction);
146+
break;
115147
default:
116148
break;
117149
}
@@ -120,7 +152,7 @@ export default class Ping extends Command {
120152
private async setSetting(
121153
guildId: string,
122154
setting: string,
123-
value: string | number
155+
value: string | string[] | number
124156
) {
125157
await this.db.guildSettings.upsert({
126158
where: {
@@ -149,40 +181,55 @@ export default class Ping extends Command {
149181
}
150182

151183
private async setChannel(interaction: ChatInputCommandInteraction) {
152-
const setting = interaction.options.getString('channel-setting');
184+
if (!interaction.guildId) return;
185+
const setting = interaction.options.getString('channel-setting', true);
153186
const channel = interaction.options.getChannel('channel', true);
154187

155-
await this.setSetting(
156-
interaction.guildId as string,
157-
setting as string,
158-
channel.id
159-
);
188+
await this.setSetting(interaction.guildId, setting, channel.id);
160189

161190
interaction.reply(`${setting} set to ${channel}`);
162191
}
163192

164193
private async setRole(interaction: ChatInputCommandInteraction) {
165-
const setting = interaction.options.getString('role-setting');
194+
if (!interaction.guildId) return;
195+
const setting = interaction.options.getString('role-setting', true);
166196
const role = interaction.options.getRole('role', true);
167197

168-
await this.setSetting(
169-
interaction.guildId as string,
170-
setting as string,
171-
role.id
172-
);
198+
await this.setSetting(interaction.guildId, setting, role.id);
173199

174200
interaction.reply(`${setting} set to ${role}`);
175201
}
176202

177203
private async setWelcomeMessage(interaction: ChatInputCommandInteraction) {
204+
if (!interaction.guildId) return;
178205
const welcomeMessage = interaction.options.getString(
179-
'welcome-message-setting'
206+
'welcome-message-setting',
207+
true
180208
);
181209
await this.setSetting(
182-
interaction.guildId as string,
210+
interaction.guildId,
183211
'welcomeMessage',
184-
welcomeMessage as string
212+
welcomeMessage
185213
);
186214
interaction.reply(`welcomeMessage set to ${welcomeMessage}`);
187215
}
216+
217+
private async setEnabledCommand(interaction: ChatInputCommandInteraction) {
218+
if (!interaction.guildId) return;
219+
const commandName = interaction.options.getString('command-name', true);
220+
const isEnabled = interaction.options.getBoolean('enabled', true);
221+
const guildSettings = await this.getGuildSettings(interaction.guildId);
222+
if (!guildSettings) return;
223+
const disabledCommands = new Set(guildSettings.disabledCommands);
224+
225+
if (isEnabled) disabledCommands.delete(commandName);
226+
else disabledCommands.add(commandName);
227+
await this.setSetting(interaction.guildId, 'disabledCommands', [
228+
...disabledCommands,
229+
]);
230+
231+
interaction.reply(
232+
`disabledCommands set to ${inlineCode([...disabledCommands].join(', '))}`
233+
);
234+
}
188235
}

0 commit comments

Comments
 (0)