From aeb3c70f918ce51e79d35150208a1385881aa282 Mon Sep 17 00:00:00 2001 From: powermaker450 Date: Sun, 13 Oct 2024 13:22:49 -0400 Subject: [PATCH] Add attachments support --- src/commands/confess.ts | 51 ++++++++++++++++++++-------- src/commands/confessbanlist.ts | 14 +++++--- src/main.ts | 61 +++++++++++++++++++++++++--------- src/modals/submit.ts | 32 ++++++++++-------- src/storeman/client.ts | 22 +++++++++--- src/storeman/types.ts | 1 + 6 files changed, 130 insertions(+), 51 deletions(-) diff --git a/src/commands/confess.ts b/src/commands/confess.ts index 4a30d0b..fe8da88 100644 --- a/src/commands/confess.ts +++ b/src/commands/confess.ts @@ -17,8 +17,8 @@ */ import { - ActionRowBuilder, - ButtonBuilder, + ActionRowBuilder, + ButtonBuilder, ButtonStyle, CommandInteraction, ComponentType, @@ -43,6 +43,11 @@ export const data = new SlashCommandBuilder() .setName("message") .setRequired(true) .setDescription("What you want to confess") + ) + .addStringOption(option => + option + .setName("attachment") + .setDescription("The link to an image to attach (optional)") ); export async function execute(interaction: CommandInteraction) { @@ -69,7 +74,12 @@ export async function execute(interaction: CommandInteraction) { const adminChannel = dt.getGuildInfo(interaction.guild?.id!)?.settings .modChannel; // @ts-ignore - const messageContent = interaction.options.getString("message"); + const messageContent: string = interaction.options.getString("message"); + // @ts-ignore + const attachment: string = interaction.options.getString("attachment"); + + const isAttachment = (text: string) => + text && (text.startsWith("http://") || text.startsWith("https://")); const color = getRandomColor(); const messageId = StoreMan.genId(); @@ -79,6 +89,8 @@ export async function execute(interaction: CommandInteraction) { // @ts-ignore .setDescription(messageContent); + isAttachment(attachment) && userConfessionEmbed.setImage(attachment); + const adminConfessionEmbed = new EmbedBuilder() .setColor(color) .setTitle(`Anonymous Confession \`${messageId}\``) @@ -95,13 +107,16 @@ export async function execute(interaction: CommandInteraction) { } ); + isAttachment(attachment) && adminConfessionEmbed.setImage(attachment); + const submitConfessionButton = new ButtonBuilder() .setCustomId("submitConfession") .setLabel("Submit a Confession") .setStyle(ButtonStyle.Primary); - const actionRow = new ActionRowBuilder() - .setComponents(submitConfessionButton); + const actionRow = new ActionRowBuilder().setComponents( + submitConfessionButton + ); const message = await ( BotClient.channels.cache.get(confessChannel!) as TextChannel @@ -110,7 +125,9 @@ export async function execute(interaction: CommandInteraction) { components: [actionRow] }); - const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button }); + const collector = message.createMessageComponentCollector({ + componentType: ComponentType.Button + }); collector.on("collect", i => { if (i.customId === "submitConfession") { @@ -127,17 +144,25 @@ export async function execute(interaction: CommandInteraction) { messageId, interaction.user.displayName, interaction.user.id, - messageContent + messageContent, + attachment ); - const confessionsLength = dt.getGuildInfo(interaction.guild?.id!)?.confessions.length!; + const confessionsLength = dt.getGuildInfo(interaction.guild?.id!) + ?.confessions.length!; if (confessionsLength >= 2) { - await (BotClient.channels.cache.get(confessChannel!) as TextChannel).messages.fetch( - dt.getGuildInfo(interaction.guild?.id!)?.confessions[confessionsLength - 2].messageId! - ).then(message => { - message.edit({ components: [] }); - }); + await ( + BotClient.channels.cache.get(confessChannel!) as TextChannel + ).messages + .fetch( + dt.getGuildInfo(interaction.guild?.id!)?.confessions[ + confessionsLength - 2 + ].messageId! + ) + .then(message => { + message.edit({ components: [] }); + }); } return interaction.reply({ diff --git a/src/commands/confessbanlist.ts b/src/commands/confessbanlist.ts index 95fbf9d..1cbd487 100644 --- a/src/commands/confessbanlist.ts +++ b/src/commands/confessbanlist.ts @@ -16,19 +16,25 @@ * along with this program. If not, see . */ -import { CommandInteraction,PermissionFlagsBits,SlashCommandBuilder } from "discord.js"; +import { + CommandInteraction, + PermissionFlagsBits, + SlashCommandBuilder +} from "discord.js"; import { dt } from "../main"; import { BotClient } from "../bot"; export const data = new SlashCommandBuilder() .setName("confessbanlist") .setDescription("Get the current ban list") - .setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages) + .setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages); export async function execute(interaction: CommandInteraction) { const bannedMembers = dt.getBans(interaction.guild?.id!); - let content = bannedMembers.length ? "Banned Members:\n" : "There are no banned members."; + let content = bannedMembers.length + ? "Banned Members:\n" + : "There are no banned members."; for (const member of bannedMembers) { const identifiedMember = await BotClient.users.fetch(member.user); @@ -39,5 +45,5 @@ export async function execute(interaction: CommandInteraction) { return interaction.reply({ content: content, ephemeral: true - }) + }); } diff --git a/src/main.ts b/src/main.ts index a40bc57..14c5531 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,7 +16,17 @@ * along with this program. If not, see . */ -import { ActionRowBuilder, ButtonBuilder, ButtonStyle, CacheType, ComponentType, EmbedBuilder, Events, Interaction, ModalSubmitInteraction, TextChannel, } from "discord.js"; +import { + ActionRowBuilder, + ButtonBuilder, + ButtonStyle, + ComponentType, + EmbedBuilder, + Events, + Interaction, + ModalSubmitInteraction, + TextChannel +} from "discord.js"; import { BotClient, BOT_TOKEN, deployCommands } from "./bot"; import { commands } from "./commands"; import { StoreMan } from "./storeman"; @@ -60,9 +70,12 @@ BotClient.on(Events.InteractionCreate, async interaction => { } if (interaction.customId === "submitConfession") { - const messageContent: string = interaction.fields.getTextInputValue("confessionInput"); - // const attachment: string = interaction.getTextInputValue("confessionAttachment"); - + const messageContent: string = + interaction.fields.getTextInputValue("confessionInput"); + const attachment: string = interaction.fields.getTextInputValue( + "confessionAttachment" + ); + try { if (dt.isBanned(interaction.guild?.id!, interaction.user.id)) { return interaction.reply({ @@ -84,6 +97,9 @@ BotClient.on(Events.InteractionCreate, async interaction => { const adminChannel = dt.getGuildInfo(interaction.guild?.id!)?.settings .modChannel; + const isAttachment = (text: string) => + text && (text.startsWith("http://") || text.startsWith("https://")); + const color = getRandomColor(); const messageId = StoreMan.genId(); const userConfessionEmbed = new EmbedBuilder() @@ -92,6 +108,8 @@ BotClient.on(Events.InteractionCreate, async interaction => { // @ts-ignore .setDescription(messageContent); + isAttachment(attachment) && userConfessionEmbed.setImage(attachment); + const adminConfessionEmbed = new EmbedBuilder() .setColor(color) .setTitle(`Anonymous Confession \`${messageId}\``) @@ -108,13 +126,16 @@ BotClient.on(Events.InteractionCreate, async interaction => { } ); + isAttachment(attachment) && adminConfessionEmbed.setImage(attachment); + const submitConfessionButton = new ButtonBuilder() .setCustomId("submitConfession") .setLabel("Submit a Confession") .setStyle(ButtonStyle.Primary); - const actionRow = new ActionRowBuilder() - .setComponents(submitConfessionButton); + const actionRow = new ActionRowBuilder().setComponents( + submitConfessionButton + ); const message = await ( BotClient.channels.cache.get(confessChannel!) as TextChannel @@ -123,7 +144,9 @@ BotClient.on(Events.InteractionCreate, async interaction => { components: [actionRow] }); - const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button }); + const collector = message.createMessageComponentCollector({ + componentType: ComponentType.Button + }); collector.on("collect", i => { if (i.customId === "submitConfession") { @@ -140,17 +163,25 @@ BotClient.on(Events.InteractionCreate, async interaction => { messageId, interaction.user.displayName, interaction.user.id, - messageContent + messageContent, + attachment ); - const confessionsLength = dt.getGuildInfo(interaction.guild?.id!)?.confessions.length!; + const confessionsLength = dt.getGuildInfo(interaction.guild?.id!) + ?.confessions.length!; if (confessionsLength >= 2) { - await (BotClient.channels.cache.get(confessChannel!) as TextChannel).messages.fetch( - dt.getGuildInfo(interaction.guild?.id!)?.confessions[confessionsLength - 2].messageId! - ).then(message => { - message.edit({ components: [] }); - }); + await ( + BotClient.channels.cache.get(confessChannel!) as TextChannel + ).messages + .fetch( + dt.getGuildInfo(interaction.guild?.id!)?.confessions[ + confessionsLength - 2 + ].messageId! + ) + .then(message => { + message.edit({ components: [] }); + }); } return interaction.reply({ @@ -161,6 +192,6 @@ BotClient.on(Events.InteractionCreate, async interaction => { logger.error("An error occured:", err); } } -}) +}); BotClient.login(BOT_TOKEN); diff --git a/src/modals/submit.ts b/src/modals/submit.ts index 4bb36cf..308a9df 100644 --- a/src/modals/submit.ts +++ b/src/modals/submit.ts @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import { +import { ActionRowBuilder, ModalActionRowComponentBuilder, ModalBuilder, @@ -26,27 +26,31 @@ import { const submit = new ModalBuilder() .setCustomId("submitConfession") - .setTitle("Submit Confession") + .setTitle("Submit Confession"); const confessionInput = new TextInputBuilder() .setCustomId("confessionInput") .setLabel("Confession") .setRequired(true) .setMaxLength(2000) - .setStyle(TextInputStyle.Paragraph) + .setStyle(TextInputStyle.Paragraph); -// TODO: Add support for attachments -// -// const attachmentInput = new TextInputBuilder() -// .setCustomId("confessionAttachment") -// .setLabel("Attachment (optional)") -// .setRequired(false) -// .setStyle(TextInputStyle.Short) +const attachmentInput = new TextInputBuilder() + .setCustomId("confessionAttachment") + .setLabel("Attachment (optional)") + .setRequired(false) + .setStyle(TextInputStyle.Short); -const actionRow = new ActionRowBuilder() - .addComponents(confessionInput); - // .addComponents(confessionInput, attachmentInput); +const confessionRow = + new ActionRowBuilder().addComponents( + confessionInput + ); -submit.addComponents(actionRow); +const attachmentRow = + new ActionRowBuilder().addComponents( + attachmentInput + ); + +submit.addComponents(confessionRow, attachmentRow); export { submit }; diff --git a/src/storeman/client.ts b/src/storeman/client.ts index 0df819f..75345d6 100644 --- a/src/storeman/client.ts +++ b/src/storeman/client.ts @@ -38,14 +38,16 @@ export class StoreMan { id: string, author: string, authorId: string, - content: string + content: string, + attachment?: string ): Confession { return { id: id, messageId: message.id, author: author, authorId: authorId, - content: content + content: content, + attachment: attachment }; } @@ -122,7 +124,8 @@ export class StoreMan { id: string, author: string, authorId: string, - content: string + content: string, + attachment?: string ): boolean { const guildId = message.guild?.id; @@ -134,7 +137,14 @@ export class StoreMan { } guild.confessions.push( - StoreMan.toConfession(message, id, author, authorId, content) + StoreMan.toConfession( + message, + id, + author, + authorId, + content, + attachment + ) ); this.saveFile(); return true; @@ -243,7 +253,9 @@ export class StoreMan { if (guild.id === guildId) { if (this.getConfession(guildId, confessionId)) { guild.settings.bans = guild.settings.bans.filter(ban => { - return ban.user !== this.getConfession(guildId, confessionId)?.authorId!; + return ( + ban.user !== this.getConfession(guildId, confessionId)?.authorId! + ); }); this.saveFile(); diff --git a/src/storeman/types.ts b/src/storeman/types.ts index 4a76c2b..b1e4845 100644 --- a/src/storeman/types.ts +++ b/src/storeman/types.ts @@ -22,6 +22,7 @@ export interface Confession { author: string; authorId: string; content: string; + attachment?: string; } export interface ConfessionBan {