From 4564cfb6a2636019e5a4dbecb56e0d786bbb26ee Mon Sep 17 00:00:00 2001 From: powermaker450 Date: Sun, 20 Oct 2024 15:16:49 -0400 Subject: [PATCH] Cleanup involving destructures and prettier --- src/commands/confess.ts | 31 +++++++++++++++---------------- src/commands/confessdel.ts | 19 +++++++++---------- src/commands/confessmod.ts | 9 +++------ src/commands/setup.ts | 30 +++++++++++++++++------------- src/commands/update.ts | 19 +++++++++++++------ src/main.ts | 9 ++++++--- src/storeman/client.ts | 18 +++++++++--------- 7 files changed, 72 insertions(+), 63 deletions(-) diff --git a/src/commands/confess.ts b/src/commands/confess.ts index 922b2c0..a3f10cd 100644 --- a/src/commands/confess.ts +++ b/src/commands/confess.ts @@ -52,9 +52,12 @@ export const data = new SlashCommandBuilder() export async function execute(interaction: ChatInputCommandInteraction) { // TODO: This all works as intended, but I'd like for it so be a reusable function // instead because all of this is used in src/main.ts + const { id: guildId } = interaction.guild!; + const { id: userId } = interaction.user; + try { // If the user is banned in this guild, don't let them post - if (dt.isBannedByUser(interaction.guild?.id!, interaction.user.id)) { + if (dt.isBannedByUser(guildId, userId)) { return interaction.reply({ content: "You are banned from confessions in this server!", ephemeral: true @@ -62,7 +65,7 @@ export async function execute(interaction: ChatInputCommandInteraction) { } // If no guild info is present for this guild, don't let the user post - if (!dt.getGuildInfo(interaction.guild?.id!)) { + if (!dt.getGuildInfo(guildId)) { return interaction.reply({ content: "The bot hasn't been set up yet! Ask the server admins to set it up.", @@ -70,10 +73,8 @@ export async function execute(interaction: ChatInputCommandInteraction) { }); } - const confessChannel = dt.getGuildInfo(interaction.guild?.id!)?.settings - .confessChannel; - const adminChannel = dt.getGuildInfo(interaction.guild?.id!)?.settings - .modChannel; + const confessChannel = dt.getGuildInfo(guildId)?.settings.confessChannel; + const adminChannel = dt.getGuildInfo(guildId)?.settings.modChannel; const messageContent = `"${interaction.options.getString("message")}"`; const attachment = interaction.options.getString("attachment")!; @@ -121,11 +122,11 @@ export async function execute(interaction: ChatInputCommandInteraction) { .addFields( { name: "Author", - value: `<@${interaction.user.id}>` + value: `<@${userId}>` }, { name: "Author ID", - value: interaction.user.id + value: userId } ); @@ -153,9 +154,10 @@ export async function execute(interaction: ChatInputCommandInteraction) { components: [actionRow] }); - adminChannel && await (BotClient.channels.cache.get(adminChannel!) as TextChannel).send({ - embeds: [adminConfessionEmbed] - }); + adminChannel && + (await (BotClient.channels.cache.get(adminChannel!) as TextChannel).send({ + embeds: [adminConfessionEmbed] + })); dt.addConfession( message, @@ -166,8 +168,7 @@ export async function execute(interaction: ChatInputCommandInteraction) { attachment ); - const confessionsLength = dt.getGuildInfo(interaction.guild?.id!) - ?.confessions.length!; + const confessionsLength = dt.getGuildInfo(guildId)!.confessions.length; // If there are 2 or more confessions, remove the previous confession's button components if (confessionsLength >= 2) { @@ -175,9 +176,7 @@ export async function execute(interaction: ChatInputCommandInteraction) { BotClient.channels.cache.get(confessChannel!) as TextChannel ).messages .fetch( - dt.getGuildInfo(interaction.guild?.id!)?.confessions[ - confessionsLength - 2 - ].messageId! + dt.getGuildInfo(guildId)!.confessions[confessionsLength - 2].messageId ) .then(message => { message.edit({ components: [] }); diff --git a/src/commands/confessdel.ts b/src/commands/confessdel.ts index aa4de1a..7b5f09e 100644 --- a/src/commands/confessdel.ts +++ b/src/commands/confessdel.ts @@ -38,8 +38,11 @@ export const data = new SlashCommandBuilder() ); export async function execute(interaction: ChatInputCommandInteraction) { + const { id: guildId } = interaction.guild!; + const { id: userId } = interaction.user; + // If there is no guild info, don't let the user delete anything - if (!dt.getGuildInfo(interaction.guild?.id!)) { + if (!dt.getGuildInfo(guildId)) { return interaction.reply({ content: "The bot hasn't been set up yet! Ask the server admins to set it up.", @@ -48,9 +51,9 @@ export async function execute(interaction: ChatInputCommandInteraction) { } const idVal = interaction.options.getString("id")!; - const result = dt.getConfession(interaction.guild?.id!, idVal); + const result = dt.getConfession(guildId, idVal); // If there is a result, and the user is either an author or has manage messages - const allowedByUser = result && result.authorId === interaction.user.id; + const allowedByUser = result && result.authorId === userId; const allowedByMod = result && interaction.memberPermissions?.has(PermissionFlagsBits.ManageMessages); @@ -58,12 +61,8 @@ export async function execute(interaction: ChatInputCommandInteraction) { // If a confession is found with the given ID, check if the user is the one that posted it, and delete it if they are. // Otherwise, don't let the user delete anything. if (allowedByUser || allowedByMod) { - const confession = dt.getConfession( - interaction.guild?.id!, - idVal - )?.messageId; - const channelId = dt.getGuildInfo(interaction.guild?.id!)?.settings - .confessChannel!; + const confession = dt.getConfession(guildId, idVal)!.messageId; + const channelId = dt.getGuildInfo(guildId)!.settings.confessChannel; const emptyEmbed = new EmbedBuilder() .setColor(getRandomColor()) .setTitle("Confession Deleted") @@ -76,7 +75,7 @@ export async function execute(interaction: ChatInputCommandInteraction) { try { // Replace the given confession with an empty embed await (BotClient.channels.cache.get(channelId) as TextChannel).messages - .fetch(confession!) + .fetch(confession) .then(e => { e.edit({ embeds: [emptyEmbed] diff --git a/src/commands/confessmod.ts b/src/commands/confessmod.ts index 80bdc67..6441b71 100644 --- a/src/commands/confessmod.ts +++ b/src/commands/confessmod.ts @@ -61,7 +61,7 @@ export const data = new SlashCommandBuilder() ); export async function execute(interaction: ChatInputCommandInteraction) { - const guildId = interaction.guild?.id!; + const { id: guildId } = interaction.guild!; // /confessmod ban if (interaction.options.getSubcommand() === "ban") { @@ -95,7 +95,7 @@ export async function execute(interaction: ChatInputCommandInteraction) { } // /confessmod list } else if (interaction.options.getSubcommand() === "list") { - const bannedMembers = dt.getBans(interaction.guild?.id!); + const bannedMembers = dt.getBans(guildId); let content = bannedMembers.length ? "Banned Members:\n" @@ -124,10 +124,7 @@ export async function execute(interaction: ChatInputCommandInteraction) { } // /confessmod pardon } else if (interaction.options.getSubcommand() === "pardon") { - const result = dt.removeBan( - interaction.guild?.id!, - interaction.options.getString("id")! - ); + const result = dt.removeBan(guildId, interaction.options.getString("id")!); try { return result diff --git a/src/commands/setup.ts b/src/commands/setup.ts index 4be52e9..27fd4eb 100644 --- a/src/commands/setup.ts +++ b/src/commands/setup.ts @@ -38,14 +38,16 @@ export const data = new SlashCommandBuilder() .setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild); export async function execute(interaction: CommandInteraction) { - if (dt.checkSetup(interaction.guild?.id!)) { + const { id: guildId } = interaction.guild!; + const { displayName: username } = interaction.user; + + if (dt.checkSetup(guildId)) { return interaction.reply({ content: "This guild has already been set up!", ephemeral: true }); } - const guildId = interaction.guild?.id; let confessChannel: string, logChannel: string; const channelList = new ChannelSelectMenuBuilder() @@ -56,17 +58,18 @@ export async function execute(interaction: CommandInteraction) { const skipButton = new ButtonBuilder() .setCustomId("skipModChannel") .setLabel("Skip") - .setStyle(ButtonStyle.Secondary) + .setStyle(ButtonStyle.Secondary); const channelRow = new ActionRowBuilder().addComponents(channelList); - const buttonRow = new ActionRowBuilder() - .addComponents(skipButton); + const buttonRow = new ActionRowBuilder().addComponents( + skipButton + ); try { const response = await interaction.reply({ - content: `# Let's get started, ${interaction.user.displayName}!\nFirst, let's choose a channel for your confessions.`, + content: `# Let's get started, ${username}!\nFirst, let's choose a channel for your confessions.`, ephemeral: true, components: [channelRow] }); @@ -77,7 +80,7 @@ export async function execute(interaction: CommandInteraction) { }); collector.on("collect", async i => { - [ confessChannel ] = i.values; + [confessChannel] = i.values; await i.update({ content: "Awesome!", @@ -111,18 +114,18 @@ export async function execute(interaction: CommandInteraction) { componentType: ComponentType.Button, time: 45_000 }); - + let skipped = false; logCollector.on("collect", async ij => { - [ logChannel ] = ij.values; + [logChannel] = ij.values; await ij.update({ content: "Setup Complete!", components: [] }); - dt.setup(guildId!, { + dt.setup(guildId, { confessChannel: confessChannel, modChannel: logChannel, bans: [] @@ -138,7 +141,7 @@ export async function execute(interaction: CommandInteraction) { content: "Setup complete!", components: [] }); - + dt.setup(guildId!, { confessChannel: confessChannel, bans: [] @@ -148,11 +151,12 @@ export async function execute(interaction: CommandInteraction) { logCollector.stop(); skipCollector.stop(); } - }) + }); logCollector.on("end", content => { // If there is no content and the channel hasn't been skipped, follow up with an error message. - (!content.size && !skipped) && + !content.size && + !skipped && interaction.followUp({ content: "No channel selected. Please try again.", ephemeral: true, diff --git a/src/commands/update.ts b/src/commands/update.ts index 640b86e..22953f7 100644 --- a/src/commands/update.ts +++ b/src/commands/update.ts @@ -16,7 +16,11 @@ * along with this program. If not, see . */ -import { ChatInputCommandInteraction, PermissionFlagsBits, SlashCommandBuilder } from "discord.js"; +import { + ChatInputCommandInteraction, + PermissionFlagsBits, + SlashCommandBuilder +} from "discord.js"; import { deployCommands } from "../bot"; import Logger from "../utils/Logger"; @@ -39,15 +43,18 @@ export async function execute(interaction: ChatInputCommandInteraction) { }); } - deployCommands({ guildId: guildId }); + deployCommands({ guildId: guildId }); cooldownList.add(guildId); logger.log(`Applied cooldown to "${guildName}"`); - setTimeout(() => { - cooldownList.delete(guildId); - logger.log(`Removed cooldown from "${guildName}"`); - }, minutes*60*1000); + setTimeout( + () => { + cooldownList.delete(guildId); + logger.log(`Removed cooldown from "${guildName}"`); + }, + minutes * 60 * 1000 + ); return interaction.reply({ content: "Commands refreshed.", diff --git a/src/main.ts b/src/main.ts index 746dabc..5979b79 100644 --- a/src/main.ts +++ b/src/main.ts @@ -183,9 +183,12 @@ BotClient.on(Events.InteractionCreate, async interaction => { components: [actionRow] }); - adminChannel && await (BotClient.channels.cache.get(adminChannel!) as TextChannel).send({ - embeds: [adminConfessionEmbed] - }); + adminChannel && + (await ( + BotClient.channels.cache.get(adminChannel!) as TextChannel + ).send({ + embeds: [adminConfessionEmbed] + })); dt.addConfession( message, diff --git a/src/storeman/client.ts b/src/storeman/client.ts index 26508d7..451b85b 100644 --- a/src/storeman/client.ts +++ b/src/storeman/client.ts @@ -82,9 +82,9 @@ export class StoreMan { } // Checks if a guild is not set up - public checkSetup(id: string): boolean { + public checkSetup(guildId: string): boolean { for (const guild of this.data) { - if (guild.id === id) { + if (guild.id === guildId) { return true; } } @@ -93,9 +93,9 @@ export class StoreMan { } // Sets up a guild and stores it in the persistent file - public setup(id: string, opts: GuildSettings): void { + public setup(guildId: string, opts: GuildSettings): void { this.data.push({ - id: id, + id: guildId, confessions: [], settings: opts }); @@ -104,16 +104,16 @@ export class StoreMan { } // Clear the settings for a given guild - public clearSettings(id: string): void { + public clearSettings(guildId: string): void { this.data = this.data.filter(guild => { - return guild.id !== id; + return guild.id !== guildId; }); this.saveFile(); } - public getGuildInfo(id: string): GuildData | null { + public getGuildInfo(guildId: string): GuildData | null { for (const guild of this.data) { - if (guild.id === id) { + if (guild.id === guildId) { return guild; } } @@ -130,7 +130,7 @@ export class StoreMan { content: string, attachment?: string ): boolean { - const guildId = message.guild?.id; + const { id: guildId } = message.guild!; for (const guild of this.data) { if (guild.id === guildId) {