♻️ Counters Module
This commit is contained in:
parent
57131677b5
commit
8804a83149
2 changed files with 63 additions and 67 deletions
|
@ -1,22 +1,22 @@
|
|||
import { SlashCommandBuilder } from "@discordjs/builders";
|
||||
import { ChatInputCommandInteraction } from "discord.js";
|
||||
import { ChatInputCommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||
|
||||
// Modules
|
||||
import moduleView from "./modules/view";
|
||||
import { builder as ViewBuilder, execute as ViewExecute } from "./modules/view";
|
||||
|
||||
//
|
||||
export const builder = new SlashCommandBuilder()
|
||||
.setName("counters")
|
||||
.setDescription("View guild counters")
|
||||
.setDMPermission(false)
|
||||
|
||||
// Modules
|
||||
.addSubcommand(moduleView.builder);
|
||||
.addSubcommand(ViewBuilder);
|
||||
|
||||
// Execute function
|
||||
export const execute = async (interaction: ChatInputCommandInteraction) => {
|
||||
switch (interaction.options.getSubcommand()) {
|
||||
case "view":
|
||||
await moduleView.execute(interaction);
|
||||
await ViewExecute(interaction);
|
||||
break;
|
||||
default:
|
||||
throw new Error("No module found for that command.");
|
||||
|
|
|
@ -1,67 +1,63 @@
|
|||
import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
|
||||
import { ChannelType } from "discord-api-types/v10";
|
||||
import { ChatInputCommandInteraction, EmbedBuilder } from "discord.js";
|
||||
import {
|
||||
ChannelType,
|
||||
ChatInputCommandInteraction,
|
||||
SlashCommandSubcommandBuilder,
|
||||
} from "discord.js";
|
||||
|
||||
import prisma from "../../../../handlers/database";
|
||||
import deferReply from "../../../../handlers/deferReply";
|
||||
import getEmbedConfig from "../../../../helpers/getEmbedData";
|
||||
import { success as BaseEmbedSuccess } from "../../../../helpers/baseEmbeds";
|
||||
|
||||
export default {
|
||||
builder: (command: SlashCommandSubcommandBuilder) => {
|
||||
return command
|
||||
.setName("view")
|
||||
.setDescription(`View a guild counter`)
|
||||
.addChannelOption((option) =>
|
||||
option
|
||||
.setName("channel")
|
||||
.setDescription(
|
||||
`The channel that contains the counter you want to view`
|
||||
)
|
||||
.setRequired(true)
|
||||
.addChannelTypes(ChannelType.GuildText)
|
||||
);
|
||||
},
|
||||
|
||||
execute: async (interaction: ChatInputCommandInteraction) => {
|
||||
await deferReply(interaction, false);
|
||||
|
||||
const { successColor, footerText, footerIcon } = await getEmbedConfig(
|
||||
interaction.guild
|
||||
// 1. Create builder function.
|
||||
export const builder = (command: SlashCommandSubcommandBuilder) => {
|
||||
return command
|
||||
.setName("view")
|
||||
.setDescription(`View a guild counter`)
|
||||
.addChannelOption((option) =>
|
||||
option
|
||||
.setName("channel")
|
||||
.setDescription(
|
||||
`The channel that contains the counter you want to view`
|
||||
)
|
||||
.setRequired(true)
|
||||
.addChannelTypes(ChannelType.GuildText)
|
||||
);
|
||||
const { options, guild } = interaction;
|
||||
|
||||
const discordChannel = options.getChannel("channel");
|
||||
|
||||
if (!guild) throw new Error(`Guild not found`);
|
||||
if (!discordChannel) throw new Error(`Channel not found`);
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle("[:1234:] Counters (View)")
|
||||
.setTimestamp(new Date())
|
||||
.setFooter({
|
||||
text: footerText,
|
||||
iconURL: footerIcon,
|
||||
});
|
||||
|
||||
const channelCounter = await prisma.guildCounter.findUnique({
|
||||
where: {
|
||||
guildId_channelId: {
|
||||
guildId: guild.id,
|
||||
channelId: discordChannel.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!channelCounter) throw new Error("No counter found for channel");
|
||||
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
embed
|
||||
.setDescription(
|
||||
`Viewing counter for channel ${discordChannel}: ${channelCounter.count}!`
|
||||
)
|
||||
.setColor(successColor),
|
||||
],
|
||||
});
|
||||
return;
|
||||
},
|
||||
};
|
||||
|
||||
// 2. Create execute function.
|
||||
export const execute = async (interaction: ChatInputCommandInteraction) => {
|
||||
// 1. Defer reply as permanent.
|
||||
await deferReply(interaction, false);
|
||||
|
||||
// 2. Destructure interaction object
|
||||
const { options, guild } = interaction;
|
||||
if (!guild) throw new Error(`Guild not found`);
|
||||
if (!options) throw new Error(`Options not found`);
|
||||
|
||||
// 3. Get options
|
||||
const discordChannel = options.getChannel("channel");
|
||||
if (!discordChannel) throw new Error(`Channel not found`);
|
||||
|
||||
// 4. Create base embeds.
|
||||
const EmbedSuccess = await BaseEmbedSuccess(guild, "[:1234:] View");
|
||||
|
||||
// 5. Get counter from database.
|
||||
const channelCounter = await prisma.guildCounter.findUnique({
|
||||
where: {
|
||||
guildId_channelId: {
|
||||
guildId: guild.id,
|
||||
channelId: discordChannel.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!channelCounter) throw new Error("No counter found for channel");
|
||||
|
||||
// 6. Send embed.
|
||||
await interaction.editReply({
|
||||
embeds: [
|
||||
EmbedSuccess.setDescription(
|
||||
`Viewing counter for channel ${discordChannel}: ${channelCounter.count}!`
|
||||
),
|
||||
],
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue