🎨 Shop Command is now using guard classes

This commit is contained in:
Axel Olausson Holtenäs 2022-10-21 16:42:15 +02:00
parent 6589ac3ba3
commit 0816d0abe2
7 changed files with 52 additions and 57 deletions

View file

@ -3,18 +3,18 @@ import { SlashCommandBuilder } from "@discordjs/builders";
import { ChatInputCommandInteraction } from "discord.js"; import { ChatInputCommandInteraction } from "discord.js";
// Modules // Modules
import modules from "./modules"; import moduleCpgg from "./modules/cpgg";
import moduleRoles from "./modules/roles";
// Handlers
export const moduleData = modules;
// Function // Function
export const builder = new SlashCommandBuilder() export const builder = new SlashCommandBuilder()
.setName("shop") .setName("shop")
.setDescription("Shop for credits and custom roles.") .setDescription("Shop for credits and custom roles.")
.addSubcommand(modules.cpgg.builder) .setDMPermission(false)
.addSubcommandGroup(modules.roles.builder);
// Modules
.addSubcommand(moduleCpgg.builder)
.addSubcommandGroup(moduleRoles.builder);
// Execute the command // Execute the command
export const execute = async (interaction: ChatInputCommandInteraction) => { export const execute = async (interaction: ChatInputCommandInteraction) => {
@ -22,7 +22,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
switch (options.getSubcommand()) { switch (options.getSubcommand()) {
case "cpgg": { case "cpgg": {
await modules.cpgg.execute(interaction); await moduleCpgg.execute(interaction);
break; break;
} }
default: { default: {
@ -32,7 +32,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
switch (options.getSubcommandGroup()) { switch (options.getSubcommandGroup()) {
case "roles": { case "roles": {
await modules.roles.execute(interaction); await moduleRoles.execute(interaction);
break; break;
} }
default: { default: {

View file

@ -10,13 +10,12 @@ import {
} from "discord.js"; } from "discord.js";
import { v4 as uuidv4 } from "uuid"; import { v4 as uuidv4 } from "uuid";
import prisma from "../../../../handlers/database"; import prisma from "../../../../handlers/database";
import deferReply from "../../../../handlers/deferReply";
import encryption from "../../../../helpers/encryption"; import encryption from "../../../../helpers/encryption";
import getEmbedData from "../../../../helpers/getEmbedData"; import getEmbedData from "../../../../helpers/getEmbedData";
import logger from "../../../../middlewares/logger"; import logger from "../../../../middlewares/logger";
export default { export default {
metadata: { guildOnly: true, ephemeral: true },
builder: (command: SlashCommandSubcommandBuilder) => { builder: (command: SlashCommandSubcommandBuilder) => {
return command return command
.setName("cpgg") .setName("cpgg")
@ -29,6 +28,8 @@ export default {
); );
}, },
execute: async (interaction: ChatInputCommandInteraction) => { execute: async (interaction: ChatInputCommandInteraction) => {
await deferReply(interaction, true);
const { errorColor, successColor, footerText, footerIcon } = const { errorColor, successColor, footerText, footerIcon } =
await getEmbedData(interaction.guild); await getEmbedData(interaction.guild);
const { options, guild, user, client } = interaction; const { options, guild, user, client } = interaction;

View file

@ -1,4 +0,0 @@
import cpgg from "./cpgg";
import * as roles from "./roles";
export default { cpgg, roles };

View file

@ -5,39 +5,42 @@ import { ChatInputCommandInteraction } from "discord.js";
// Handlers // Handlers
// Modules // Modules
import modules from "./modules"; import moduleBuy from "./modules/buy";
import moduleCancel from "./modules/cancel";
import guildSchema from "../../../../models/guild"; import guildSchema from "../../../../models/guild";
export const moduleData = modules; export default {
builder: (group: SlashCommandSubcommandGroupBuilder) => {
return (
group
.setName("roles")
.setDescription("Shop for custom roles.")
// Function // Modules
export const builder = (group: SlashCommandSubcommandGroupBuilder) => { .addSubcommand(moduleBuy.builder)
return group .addSubcommand(moduleCancel.builder)
.setName("roles") );
.setDescription("Shop for custom roles.") },
.addSubcommand(modules.buy.builder) execute: async (interaction: ChatInputCommandInteraction) => {
.addSubcommand(modules.cancel.builder); if (!interaction.guild) return;
}; const { options, guild } = interaction;
export const execute = async (interaction: ChatInputCommandInteraction) => { const guildDB = await guildSchema?.findOne({
if (!interaction.guild) return; guildId: guild?.id,
const { options, guild } = interaction; });
const guildDB = await guildSchema?.findOne({ if (guildDB === null) return;
guildId: guild?.id,
}); if (!guildDB.shop.roles.status)
throw new Error("This server has disabled shop roles.");
if (guildDB === null) return;
if (options?.getSubcommand() === "buy") {
if (!guildDB.shop.roles.status) await moduleBuy.execute(interaction);
throw new Error("This server has disabled shop roles."); }
if (options?.getSubcommand() === "buy") { if (options?.getSubcommand() === "cancel") {
await modules.buy.execute(interaction); await moduleCancel.execute(interaction);
} }
},
if (options?.getSubcommand() === "cancel") {
await modules.cancel.execute(interaction);
}
}; };

View file

@ -2,14 +2,13 @@
// Helpers // Helpers
import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
import { ChatInputCommandInteraction } from "discord.js"; import { ChatInputCommandInteraction } from "discord.js";
import deferReply from "../../../../../../handlers/deferReply";
// Configurations // Configurations
// import fetchUser from "../../../../../../helpers/userData"; // import fetchUser from "../../../../../../helpers/userData";
// Models // Models
// Function // Function
export default { export default {
metadata: { guildOnly: true, ephemeral: true },
builder: (command: SlashCommandSubcommandBuilder) => { builder: (command: SlashCommandSubcommandBuilder) => {
return command return command
.setName("buy") .setName("buy")
@ -28,6 +27,8 @@ export default {
); );
}, },
execute: async (interaction: ChatInputCommandInteraction) => { execute: async (interaction: ChatInputCommandInteraction) => {
await deferReply(interaction, true);
// const { successColor, footerText, footerIcon } = await getEmbedConfig( // const { successColor, footerText, footerIcon } = await getEmbedConfig(
// interaction.guild // interaction.guild
// ); // );

View file

@ -5,11 +5,10 @@ import { ChatInputCommandInteraction } from "discord.js";
// Configurations // Configurations
// import fetchUser from "../../../../../../helpers/userData"; // import fetchUser from "../../../../../../helpers/userData";
// Models // Models
import deferReply from "../../../../../../handlers/deferReply";
// Function // Function
export default { export default {
metadata: { guildOnly: true, ephemeral: true },
builder: (command: SlashCommandSubcommandBuilder) => { builder: (command: SlashCommandSubcommandBuilder) => {
return command return command
.setName("cancel") .setName("cancel")
@ -22,6 +21,8 @@ export default {
); );
}, },
execute: async (interaction: ChatInputCommandInteraction) => { execute: async (interaction: ChatInputCommandInteraction) => {
await deferReply(interaction, true);
// const { successColor, footerText, footerIcon } = await getEmbedConfig( // const { successColor, footerText, footerIcon } = await getEmbedConfig(
// interaction.guild // interaction.guild
// ); // );

View file

@ -1,7 +0,0 @@
import buy from "./buy";
import cancel from "./cancel";
export default {
buy,
cancel,
};