refactor(arabot): change subcommand logic to sapphire's plugin

This commit is contained in:
smyalygames 2023-02-09 13:37:43 +00:00
parent 8d83365c8a
commit 80f201a1ad
3 changed files with 71 additions and 113 deletions

View File

@ -17,7 +17,8 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
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 type { Guild, TextChannel } from 'discord.js';
import { import {
CategoryChannel, CategoryChannel,
@ -29,18 +30,28 @@ import {
} from 'discord.js'; } from 'discord.js';
import IDs from '#utils/ids'; import IDs from '#utils/ids';
export class PrivateCommand extends Command { export class PrivateCommand extends Subcommand {
public constructor(context: Command.Context, options: Command.Options) { public constructor(context: Subcommand.Context, options: Subcommand.Options) {
super(context, { super(context, {
...options, ...options,
name: 'private', name: 'private',
subcommands: [
{
name: 'create',
chatInputRun: 'create',
},
{
name: 'delete',
chatInputRun: 'delete',
},
],
description: 'Creates/deletes private channels for a user', description: 'Creates/deletes private channels for a user',
preconditions: ['CoordinatorOnly'], preconditions: ['CoordinatorOnly'],
}); });
} }
// Registers that this is a slash command // Registers that this is a slash command
public override registerApplicationCommands(registry: Command.Registry) { public override registerApplicationCommands(registry: Subcommand.Registry) {
registry.registerChatInputCommand( registry.registerChatInputCommand(
(builder) => builder (builder) => builder
.setName(this.name) .setName(this.name)
@ -60,32 +71,7 @@ export class PrivateCommand extends Command {
); );
} }
// Command run public async create(interaction: Subcommand.ChatInputCommandInteraction) {
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) {
// Get the arguments // Get the arguments
const user = interaction.options.getUser('user'); const user = interaction.options.getUser('user');
const mod = interaction.member; 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 // Get the arguments
const user = interaction.options.getUser('user'); const user = interaction.options.getUser('user');
const mod = interaction.member; const mod = interaction.member;

View File

@ -17,12 +17,8 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { import { Args, container, RegisterBehavior } from '@sapphire/framework';
Args, import { Subcommand } from '@sapphire/plugin-subcommands';
Command,
container,
RegisterBehavior,
} from '@sapphire/framework';
import { import {
ChannelType, ChannelType,
GuildMember, GuildMember,
@ -33,30 +29,42 @@ import {
import type { TextChannel } from 'discord.js'; import type { TextChannel } from 'discord.js';
import IDs from '#utils/ids'; import IDs from '#utils/ids';
export class DiversityCommand extends Command { export class DiversityCommand extends Subcommand {
public constructor(context: Command.Context, options: Command.Options) { public constructor(context: Subcommand.Context, options: Subcommand.Options) {
super(context, { super(context, {
...options, ...options,
name: 'diversity', name: 'diversity',
aliases: ['di', 'div'], aliases: ['di', 'div'],
subcommands: [
{
name: 'role',
default: true,
chatInputRun: 'roleCommand',
messageRun: 'roleMessage',
},
{
name: 'toggleopen',
chatInputRun: 'toggleOpen',
},
],
description: 'Commands for the Diversity Coordinators', description: 'Commands for the Diversity Coordinators',
preconditions: ['DiversityCoordinatorOnly'], preconditions: ['DiversityCoordinatorOnly'],
}); });
} }
// Registers that this is a slash command // Registers that this is a slash command
public override registerApplicationCommands(registry: Command.Registry) { public override registerApplicationCommands(registry: Subcommand.Registry) {
registry.registerChatInputCommand( registry.registerChatInputCommand(
(builder) => builder (builder) => builder
.setName(this.name) .setName(this.name)
.setDescription(this.description) .setDescription(this.description)
.addSubcommand((command) => command.setName('toggleopen')
.setDescription('Toggles read-only for vegans in diversity section'))
.addSubcommand((command) => command.setName('role') .addSubcommand((command) => command.setName('role')
.setDescription('Gives/removes the diversity role') .setDescription('Gives/removes the diversity role')
.addUserOption((option) => option.setName('user') .addUserOption((option) => option.setName('user')
.setDescription('User to give/remove diversity to') .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, behaviorWhenNotIdentical: RegisterBehavior.Overwrite,
}, },
@ -64,32 +72,7 @@ export class DiversityCommand extends Command {
} }
// Command run // Command run
public async chatInputRun(interaction: Command.ChatInputCommandInteraction) { public async toggleOpen(interaction: Subcommand.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) {
// Check if guild is not null // Check if guild is not null
if (interaction.guild === null) { if (interaction.guild === null) {
await interaction.reply({ 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 // TODO add database updates
// Get the arguments // Get the arguments
const user = interaction.options.getUser('user'); const user = interaction.options.getUser('user');
@ -201,7 +184,7 @@ export class DiversityCommand extends Command {
.catch(() => {}); .catch(() => {});
} }
public async messageRun(message: Message, args: Args) { public async roleMessage(message: Message, args: Args) {
// Get arguments // Get arguments
let user: GuildMember; let user: GuildMember;
try { try {

View File

@ -17,7 +17,8 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { Command, RegisterBehavior, Args } from '@sapphire/framework'; import { RegisterBehavior, Args } from '@sapphire/framework';
import { Subcommand } from '@sapphire/plugin-subcommands';
import { import {
ChannelType, ChannelType,
EmbedBuilder, 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 // TODO add a check when they join the server to give the user the sus role again
export class SusCommand extends Command { export class SusCommand extends Subcommand {
public constructor(context: Command.Context) { public constructor(context: Subcommand.Context, options: Subcommand.Options) {
super(context, { super(context, {
...options,
name: 'sus', 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', description: 'Notes about users that are sus',
preconditions: [['VerifierOnly', 'ModOnly']], preconditions: [['VerifierOnly', 'ModOnly']],
}); });
} }
// Registers that this is a slash command // Registers that this is a slash command
public override registerApplicationCommands(registry: Command.Registry) { public override registerApplicationCommands(registry: Subcommand.Registry) {
registry.registerChatInputCommand( registry.registerChatInputCommand(
(builder) => builder (builder) => builder
.setName(this.name) .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 // Subcommand to add sus note
private async addNote(interaction: Command.ChatInputCommandInteraction) { public async addNote(interaction: Subcommand.ChatInputCommandInteraction) {
// Get the arguments // Get the arguments
const user = interaction.options.getUser('user'); const user = interaction.options.getUser('user');
const note = interaction.options.getString('note'); 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 // Get the arguments
const user = interaction.options.getUser('user'); const user = interaction.options.getUser('user');
const { guild } = interaction; 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 // Get the arguments
const noteId = interaction.options.getInteger('id'); const noteId = interaction.options.getInteger('id');
const { guild, channel } = interaction; 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 // Get the arguments
const user = interaction.options.getUser('user'); const user = interaction.options.getUser('user');
const { guild, channel } = interaction; const { guild, channel } = interaction;
@ -488,7 +477,7 @@ export class SusCommand extends Command {
// Non Application Command method of adding a sus note // Non Application Command method of adding a sus note
// xlevra begged me to add this... so I guess here it is // 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 // Get arguments
let user: GuildMember; let user: GuildMember;
try { try {