From 80f201a1adc7fcda405b311730a060f76ad44809 Mon Sep 17 00:00:00 2001 From: smyalygames Date: Thu, 9 Feb 2023 13:37:43 +0000 Subject: [PATCH] refactor(arabot): change subcommand logic to sapphire's plugin --- src/commands/coordinators/private.ts | 48 +++++++----------- src/commands/mod/diversity.ts | 63 +++++++++--------------- src/commands/mod/sus.ts | 73 ++++++++++++---------------- 3 files changed, 71 insertions(+), 113 deletions(-) diff --git a/src/commands/coordinators/private.ts b/src/commands/coordinators/private.ts index 3cbf6fd..da89491 100644 --- a/src/commands/coordinators/private.ts +++ b/src/commands/coordinators/private.ts @@ -17,7 +17,8 @@ along with this program. If not, see . */ -import { Command, RegisterBehavior } from '@sapphire/framework'; +import { RegisterBehavior } from '@sapphire/framework'; +import { Subcommand } from '@sapphire/plugin-subcommands'; import type { Guild, TextChannel } from 'discord.js'; import { CategoryChannel, @@ -29,18 +30,28 @@ import { } from 'discord.js'; import IDs from '#utils/ids'; -export class PrivateCommand extends Command { - public constructor(context: Command.Context, options: Command.Options) { +export class PrivateCommand extends Subcommand { + public constructor(context: Subcommand.Context, options: Subcommand.Options) { super(context, { ...options, name: 'private', + subcommands: [ + { + name: 'create', + chatInputRun: 'create', + }, + { + name: 'delete', + chatInputRun: 'delete', + }, + ], description: 'Creates/deletes private channels for a user', preconditions: ['CoordinatorOnly'], }); } // Registers that this is a slash command - public override registerApplicationCommands(registry: Command.Registry) { + public override registerApplicationCommands(registry: Subcommand.Registry) { registry.registerChatInputCommand( (builder) => builder .setName(this.name) @@ -60,32 +71,7 @@ export class PrivateCommand extends Command { ); } - // Command run - public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { - const subcommand = interaction.options.getSubcommand(true); - - // Checks what subcommand was run - switch (subcommand) { - case 'create': { - await this.create(interaction); - return; - } - case 'delete': { - await this.delete(interaction); - return; - } - default: { - // If subcommand is invalid - await interaction.reply({ - content: 'Invalid sub command!', - ephemeral: true, - fetchReply: true, - }); - } - } - } - - private async create(interaction: Command.ChatInputCommandInteraction) { + public async create(interaction: Subcommand.ChatInputCommandInteraction) { // Get the arguments const user = interaction.options.getUser('user'); const mod = interaction.member; @@ -227,7 +213,7 @@ export class PrivateCommand extends Command { }); } - private async delete(interaction: Command.ChatInputCommandInteraction) { + public async delete(interaction: Subcommand.ChatInputCommandInteraction) { // Get the arguments const user = interaction.options.getUser('user'); const mod = interaction.member; diff --git a/src/commands/mod/diversity.ts b/src/commands/mod/diversity.ts index 8c1ace6..e6d1126 100644 --- a/src/commands/mod/diversity.ts +++ b/src/commands/mod/diversity.ts @@ -17,12 +17,8 @@ along with this program. If not, see . */ -import { - Args, - Command, - container, - RegisterBehavior, -} from '@sapphire/framework'; +import { Args, container, RegisterBehavior } from '@sapphire/framework'; +import { Subcommand } from '@sapphire/plugin-subcommands'; import { ChannelType, GuildMember, @@ -33,30 +29,42 @@ import { import type { TextChannel } from 'discord.js'; import IDs from '#utils/ids'; -export class DiversityCommand extends Command { - public constructor(context: Command.Context, options: Command.Options) { +export class DiversityCommand extends Subcommand { + public constructor(context: Subcommand.Context, options: Subcommand.Options) { super(context, { ...options, name: 'diversity', aliases: ['di', 'div'], + subcommands: [ + { + name: 'role', + default: true, + chatInputRun: 'roleCommand', + messageRun: 'roleMessage', + }, + { + name: 'toggleopen', + chatInputRun: 'toggleOpen', + }, + ], description: 'Commands for the Diversity Coordinators', preconditions: ['DiversityCoordinatorOnly'], }); } // Registers that this is a slash command - public override registerApplicationCommands(registry: Command.Registry) { + public override registerApplicationCommands(registry: Subcommand.Registry) { registry.registerChatInputCommand( (builder) => builder .setName(this.name) .setDescription(this.description) - .addSubcommand((command) => command.setName('toggleopen') - .setDescription('Toggles read-only for vegans in diversity section')) .addSubcommand((command) => command.setName('role') .setDescription('Gives/removes the diversity role') .addUserOption((option) => option.setName('user') .setDescription('User to give/remove diversity to') - .setRequired(true))), + .setRequired(true))) + .addSubcommand((command) => command.setName('toggleopen') + .setDescription('Toggles read-only for vegans in diversity section')), { behaviorWhenNotIdentical: RegisterBehavior.Overwrite, }, @@ -64,32 +72,7 @@ export class DiversityCommand extends Command { } // Command run - public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { - const subcommand = interaction.options.getSubcommand(true); - - // Checks what subcommand was run - switch (subcommand) { - case 'toggleopen': { - await this.toggleOpen(interaction); - return; - } - case 'role': { - await this.roleCommand(interaction); - return; - } - default: { - // If subcommand is invalid - await interaction.reply({ - content: 'Invalid sub command!', - ephemeral: true, - fetchReply: true, - }); - } - } - } - - // Command run - public async toggleOpen(interaction: Command.ChatInputCommandInteraction) { + public async toggleOpen(interaction: Subcommand.ChatInputCommandInteraction) { // Check if guild is not null if (interaction.guild === null) { await interaction.reply({ @@ -148,7 +131,7 @@ export class DiversityCommand extends Command { }); } - public async roleCommand(interaction: Command.ChatInputCommandInteraction) { + public async roleCommand(interaction: Subcommand.ChatInputCommandInteraction) { // TODO add database updates // Get the arguments const user = interaction.options.getUser('user'); @@ -201,7 +184,7 @@ export class DiversityCommand extends Command { .catch(() => {}); } - public async messageRun(message: Message, args: Args) { + public async roleMessage(message: Message, args: Args) { // Get arguments let user: GuildMember; try { diff --git a/src/commands/mod/sus.ts b/src/commands/mod/sus.ts index f5e024e..4af3653 100644 --- a/src/commands/mod/sus.ts +++ b/src/commands/mod/sus.ts @@ -17,7 +17,8 @@ along with this program. If not, see . */ -import { Command, RegisterBehavior, Args } from '@sapphire/framework'; +import { RegisterBehavior, Args } from '@sapphire/framework'; +import { Subcommand } from '@sapphire/plugin-subcommands'; import { ChannelType, EmbedBuilder, @@ -41,17 +42,38 @@ import IDs from '#utils/ids'; // TODO add a check when they join the server to give the user the sus role again -export class SusCommand extends Command { - public constructor(context: Command.Context) { +export class SusCommand extends Subcommand { + public constructor(context: Subcommand.Context, options: Subcommand.Options) { super(context, { + ...options, name: 'sus', + subcommands: [ + { + name: 'add', + default: true, + chatInputRun: 'addNote', + messageRun: 'addMessage', + }, + { + name: 'view', + chatInputRun: 'listNote', + }, + { + name: 'remove', + chatInputRun: 'removeNote', + }, + { + name: 'purge', + chatInputRun: 'removeAllNotes', + }, + ], description: 'Notes about users that are sus', preconditions: [['VerifierOnly', 'ModOnly']], }); } // Registers that this is a slash command - public override registerApplicationCommands(registry: Command.Registry) { + public override registerApplicationCommands(registry: Subcommand.Registry) { registry.registerChatInputCommand( (builder) => builder .setName(this.name) @@ -89,41 +111,8 @@ export class SusCommand extends Command { ); } - // Command run - public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { - const subcommand = interaction.options.getSubcommand(true); - - // Checks what subcommand was run - switch (subcommand) { - case 'add': { - await this.addNote(interaction); - return; - } - case 'view': { - await this.listNote(interaction); - return; - } - case 'remove': { - await this.removeNote(interaction); - return; - } - case 'purge': { - await this.removeAllNotes(interaction); - return; - } - default: { - // If subcommand is invalid - await interaction.reply({ - content: 'Invalid sub command!', - ephemeral: true, - fetchReply: true, - }); - } - } - } - // Subcommand to add sus note - private async addNote(interaction: Command.ChatInputCommandInteraction) { + public async addNote(interaction: Subcommand.ChatInputCommandInteraction) { // Get the arguments const user = interaction.options.getUser('user'); const note = interaction.options.getString('note'); @@ -177,7 +166,7 @@ export class SusCommand extends Command { }); } - private async listNote(interaction: Command.ChatInputCommandInteraction) { + public async listNote(interaction: Subcommand.ChatInputCommandInteraction) { // Get the arguments const user = interaction.options.getUser('user'); const { guild } = interaction; @@ -243,7 +232,7 @@ export class SusCommand extends Command { }); } - private async removeNote(interaction: Command.ChatInputCommandInteraction) { + public async removeNote(interaction: Subcommand.ChatInputCommandInteraction) { // Get the arguments const noteId = interaction.options.getInteger('id'); const { guild, channel } = interaction; @@ -368,7 +357,7 @@ export class SusCommand extends Command { }); } - private async removeAllNotes(interaction: Command.ChatInputCommandInteraction) { + public async removeAllNotes(interaction: Subcommand.ChatInputCommandInteraction) { // Get the arguments const user = interaction.options.getUser('user'); const { guild, channel } = interaction; @@ -488,7 +477,7 @@ export class SusCommand extends Command { // Non Application Command method of adding a sus note // xlevra begged me to add this... so I guess here it is - public async messageRun(message: Message, args: Args) { + public async addMessage(message: Message, args: Args) { // Get arguments let user: GuildMember; try {