Skip to content

Commit

Permalink
Merge pull request #1067 from LEDBrain/dev
Browse files Browse the repository at this point in the history
feat: Add settings command for command enabling/disabling
  • Loading branch information
kdev authored May 3, 2024
2 parents efdfe17 + 5f4e857 commit 497afee
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 29 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"joi": "^17.13.1",
"jsdom": "^24.0.0",
"node-libcurl": "^4.0.0",
"zod": "^3.23.5"
"zod": "^3.23.6"
},
"devDependencies": {
"@hapi/code": "^9.0.3",
Expand All @@ -61,7 +61,7 @@
"prettier": "^3.2.5",
"prisma": "^5.13.0",
"rimraf": "^5.0.5",
"tsx": "^4.8.2",
"tsx": "^4.9.0",
"typescript": "^5.4.5"
},
"config": {
Expand Down
3 changes: 1 addition & 2 deletions src/base/Base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { GuildSettings } from '@prisma/client';
import type { Guild, TextChannel } from 'discord.js';
import { ChannelType } from 'discord.js';
import packageJson from '../../package.json' with { type: 'json' };
Expand All @@ -12,7 +11,7 @@ export default abstract class Base {
this.db = prisma;
this.Sanction = SanctionManager;
}
async getGuildSettings(guildId: string): Promise<GuildSettings | null> {
async getGuildSettings(guildId: string) {
return await this.db.guildSettings.findUnique({
where: {
id: guildId,
Expand Down
81 changes: 64 additions & 17 deletions src/commands/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
ChannelType,
PermissionFlagsBits,
SlashCommandBuilder,
inlineCode,
} from 'discord.js';
import { client } from 'index.js';
import type { Config } from '../base/Command.js';
import Command from '../base/Command.js';

Expand Down Expand Up @@ -68,7 +70,9 @@ export default class Ping extends Command {
.addSubcommand(subcommand =>
subcommand
.setName('ban-approvals')
.setDescription('Set ban approvals')
.setDescription(
'Set the numbers of ban approvals needed to ban the user.'
)
.addNumberOption(numberOption =>
numberOption
.setName('ban-approvals-setting')
Expand All @@ -94,6 +98,31 @@ export default class Ping extends Command {
)
.setRequired(true)
)
)
.addSubcommand(subcommand =>
subcommand
.setName('enabled-commands')
.setDescription('Toggle active state of commands')
.addStringOption(stringOption =>
stringOption
.setName('command-name')
.setDescription(
'The command name to toggle on/off.'
)
.setRequired(true)
.setChoices(
...client.commands.map(command => ({
name: command.name,
value: command.name,
}))
)
)
.addBooleanOption(booleanOption =>
booleanOption
.setName('enabled')
.setDescription('Set the status')
.setRequired(true)
)
);

super(cmd as unknown as Config);
Expand All @@ -112,6 +141,9 @@ export default class Ping extends Command {
case 'welcome-message':
this.setWelcomeMessage(interaction);
break;
case 'enabled-commands':
this.setEnabledCommand(interaction);
break;
default:
break;
}
Expand All @@ -120,7 +152,7 @@ export default class Ping extends Command {
private async setSetting(
guildId: string,
setting: string,
value: string | number
value: string | string[] | number
) {
await this.db.guildSettings.upsert({
where: {
Expand Down Expand Up @@ -149,40 +181,55 @@ export default class Ping extends Command {
}

private async setChannel(interaction: ChatInputCommandInteraction) {
const setting = interaction.options.getString('channel-setting');
if (!interaction.guildId) return;
const setting = interaction.options.getString('channel-setting', true);
const channel = interaction.options.getChannel('channel', true);

await this.setSetting(
interaction.guildId as string,
setting as string,
channel.id
);
await this.setSetting(interaction.guildId, setting, channel.id);

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

private async setRole(interaction: ChatInputCommandInteraction) {
const setting = interaction.options.getString('role-setting');
if (!interaction.guildId) return;
const setting = interaction.options.getString('role-setting', true);
const role = interaction.options.getRole('role', true);

await this.setSetting(
interaction.guildId as string,
setting as string,
role.id
);
await this.setSetting(interaction.guildId, setting, role.id);

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

private async setWelcomeMessage(interaction: ChatInputCommandInteraction) {
if (!interaction.guildId) return;
const welcomeMessage = interaction.options.getString(
'welcome-message-setting'
'welcome-message-setting',
true
);
await this.setSetting(
interaction.guildId as string,
interaction.guildId,
'welcomeMessage',
welcomeMessage as string
welcomeMessage
);
interaction.reply(`welcomeMessage set to ${welcomeMessage}`);
}

private async setEnabledCommand(interaction: ChatInputCommandInteraction) {
if (!interaction.guildId) return;
const commandName = interaction.options.getString('command-name', true);
const isEnabled = interaction.options.getBoolean('enabled', true);
const guildSettings = await this.getGuildSettings(interaction.guildId);
if (!guildSettings) return;
const disabledCommands = new Set(guildSettings.disabledCommands);

if (isEnabled) disabledCommands.delete(commandName);
else disabledCommands.add(commandName);
await this.setSetting(interaction.guildId, 'disabledCommands', [
...disabledCommands,
]);

interaction.reply(
`disabledCommands set to ${inlineCode([...disabledCommands].join(', '))}`
);
}
}

0 comments on commit 497afee

Please sign in to comment.