Add attachments support

This commit is contained in:
powermaker450 2024-10-13 13:22:49 -04:00
parent 11404358eb
commit aeb3c70f91
6 changed files with 130 additions and 51 deletions

View file

@ -17,8 +17,8 @@
*/ */
import { import {
ActionRowBuilder, ActionRowBuilder,
ButtonBuilder, ButtonBuilder,
ButtonStyle, ButtonStyle,
CommandInteraction, CommandInteraction,
ComponentType, ComponentType,
@ -43,6 +43,11 @@ export const data = new SlashCommandBuilder()
.setName("message") .setName("message")
.setRequired(true) .setRequired(true)
.setDescription("What you want to confess") .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) { export async function execute(interaction: CommandInteraction) {
@ -69,7 +74,12 @@ export async function execute(interaction: CommandInteraction) {
const adminChannel = dt.getGuildInfo(interaction.guild?.id!)?.settings const adminChannel = dt.getGuildInfo(interaction.guild?.id!)?.settings
.modChannel; .modChannel;
// @ts-ignore // @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 color = getRandomColor();
const messageId = StoreMan.genId(); const messageId = StoreMan.genId();
@ -79,6 +89,8 @@ export async function execute(interaction: CommandInteraction) {
// @ts-ignore // @ts-ignore
.setDescription(messageContent); .setDescription(messageContent);
isAttachment(attachment) && userConfessionEmbed.setImage(attachment);
const adminConfessionEmbed = new EmbedBuilder() const adminConfessionEmbed = new EmbedBuilder()
.setColor(color) .setColor(color)
.setTitle(`Anonymous Confession \`${messageId}\``) .setTitle(`Anonymous Confession \`${messageId}\``)
@ -95,13 +107,16 @@ export async function execute(interaction: CommandInteraction) {
} }
); );
isAttachment(attachment) && adminConfessionEmbed.setImage(attachment);
const submitConfessionButton = new ButtonBuilder() const submitConfessionButton = new ButtonBuilder()
.setCustomId("submitConfession") .setCustomId("submitConfession")
.setLabel("Submit a Confession") .setLabel("Submit a Confession")
.setStyle(ButtonStyle.Primary); .setStyle(ButtonStyle.Primary);
const actionRow = new ActionRowBuilder<ButtonBuilder>() const actionRow = new ActionRowBuilder<ButtonBuilder>().setComponents(
.setComponents(submitConfessionButton); submitConfessionButton
);
const message = await ( const message = await (
BotClient.channels.cache.get(confessChannel!) as TextChannel BotClient.channels.cache.get(confessChannel!) as TextChannel
@ -110,7 +125,9 @@ export async function execute(interaction: CommandInteraction) {
components: [actionRow] components: [actionRow]
}); });
const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button }); const collector = message.createMessageComponentCollector({
componentType: ComponentType.Button
});
collector.on("collect", i => { collector.on("collect", i => {
if (i.customId === "submitConfession") { if (i.customId === "submitConfession") {
@ -127,17 +144,25 @@ export async function execute(interaction: CommandInteraction) {
messageId, messageId,
interaction.user.displayName, interaction.user.displayName,
interaction.user.id, 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) { if (confessionsLength >= 2) {
await (BotClient.channels.cache.get(confessChannel!) as TextChannel).messages.fetch( await (
dt.getGuildInfo(interaction.guild?.id!)?.confessions[confessionsLength - 2].messageId! BotClient.channels.cache.get(confessChannel!) as TextChannel
).then(message => { ).messages
message.edit({ components: [] }); .fetch(
}); dt.getGuildInfo(interaction.guild?.id!)?.confessions[
confessionsLength - 2
].messageId!
)
.then(message => {
message.edit({ components: [] });
});
} }
return interaction.reply({ return interaction.reply({

View file

@ -16,19 +16,25 @@
* 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 { CommandInteraction,PermissionFlagsBits,SlashCommandBuilder } from "discord.js"; import {
CommandInteraction,
PermissionFlagsBits,
SlashCommandBuilder
} from "discord.js";
import { dt } from "../main"; import { dt } from "../main";
import { BotClient } from "../bot"; import { BotClient } from "../bot";
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName("confessbanlist") .setName("confessbanlist")
.setDescription("Get the current ban list") .setDescription("Get the current ban list")
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages) .setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages);
export async function execute(interaction: CommandInteraction) { export async function execute(interaction: CommandInteraction) {
const bannedMembers = dt.getBans(interaction.guild?.id!); 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) { for (const member of bannedMembers) {
const identifiedMember = await BotClient.users.fetch(member.user); const identifiedMember = await BotClient.users.fetch(member.user);
@ -39,5 +45,5 @@ export async function execute(interaction: CommandInteraction) {
return interaction.reply({ return interaction.reply({
content: content, content: content,
ephemeral: true ephemeral: true
}) });
} }

View file

@ -16,7 +16,17 @@
* 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 { 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 { BotClient, BOT_TOKEN, deployCommands } from "./bot";
import { commands } from "./commands"; import { commands } from "./commands";
import { StoreMan } from "./storeman"; import { StoreMan } from "./storeman";
@ -60,8 +70,11 @@ BotClient.on(Events.InteractionCreate, async interaction => {
} }
if (interaction.customId === "submitConfession") { if (interaction.customId === "submitConfession") {
const messageContent: string = interaction.fields.getTextInputValue("confessionInput"); const messageContent: string =
// const attachment: string = interaction.getTextInputValue("confessionAttachment"); interaction.fields.getTextInputValue("confessionInput");
const attachment: string = interaction.fields.getTextInputValue(
"confessionAttachment"
);
try { try {
if (dt.isBanned(interaction.guild?.id!, interaction.user.id)) { if (dt.isBanned(interaction.guild?.id!, interaction.user.id)) {
@ -84,6 +97,9 @@ BotClient.on(Events.InteractionCreate, async interaction => {
const adminChannel = dt.getGuildInfo(interaction.guild?.id!)?.settings const adminChannel = dt.getGuildInfo(interaction.guild?.id!)?.settings
.modChannel; .modChannel;
const isAttachment = (text: string) =>
text && (text.startsWith("http://") || text.startsWith("https://"));
const color = getRandomColor(); const color = getRandomColor();
const messageId = StoreMan.genId(); const messageId = StoreMan.genId();
const userConfessionEmbed = new EmbedBuilder() const userConfessionEmbed = new EmbedBuilder()
@ -92,6 +108,8 @@ BotClient.on(Events.InteractionCreate, async interaction => {
// @ts-ignore // @ts-ignore
.setDescription(messageContent); .setDescription(messageContent);
isAttachment(attachment) && userConfessionEmbed.setImage(attachment);
const adminConfessionEmbed = new EmbedBuilder() const adminConfessionEmbed = new EmbedBuilder()
.setColor(color) .setColor(color)
.setTitle(`Anonymous Confession \`${messageId}\``) .setTitle(`Anonymous Confession \`${messageId}\``)
@ -108,13 +126,16 @@ BotClient.on(Events.InteractionCreate, async interaction => {
} }
); );
isAttachment(attachment) && adminConfessionEmbed.setImage(attachment);
const submitConfessionButton = new ButtonBuilder() const submitConfessionButton = new ButtonBuilder()
.setCustomId("submitConfession") .setCustomId("submitConfession")
.setLabel("Submit a Confession") .setLabel("Submit a Confession")
.setStyle(ButtonStyle.Primary); .setStyle(ButtonStyle.Primary);
const actionRow = new ActionRowBuilder<ButtonBuilder>() const actionRow = new ActionRowBuilder<ButtonBuilder>().setComponents(
.setComponents(submitConfessionButton); submitConfessionButton
);
const message = await ( const message = await (
BotClient.channels.cache.get(confessChannel!) as TextChannel BotClient.channels.cache.get(confessChannel!) as TextChannel
@ -123,7 +144,9 @@ BotClient.on(Events.InteractionCreate, async interaction => {
components: [actionRow] components: [actionRow]
}); });
const collector = message.createMessageComponentCollector({ componentType: ComponentType.Button }); const collector = message.createMessageComponentCollector({
componentType: ComponentType.Button
});
collector.on("collect", i => { collector.on("collect", i => {
if (i.customId === "submitConfession") { if (i.customId === "submitConfession") {
@ -140,17 +163,25 @@ BotClient.on(Events.InteractionCreate, async interaction => {
messageId, messageId,
interaction.user.displayName, interaction.user.displayName,
interaction.user.id, 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) { if (confessionsLength >= 2) {
await (BotClient.channels.cache.get(confessChannel!) as TextChannel).messages.fetch( await (
dt.getGuildInfo(interaction.guild?.id!)?.confessions[confessionsLength - 2].messageId! BotClient.channels.cache.get(confessChannel!) as TextChannel
).then(message => { ).messages
message.edit({ components: [] }); .fetch(
}); dt.getGuildInfo(interaction.guild?.id!)?.confessions[
confessionsLength - 2
].messageId!
)
.then(message => {
message.edit({ components: [] });
});
} }
return interaction.reply({ return interaction.reply({
@ -161,6 +192,6 @@ BotClient.on(Events.InteractionCreate, async interaction => {
logger.error("An error occured:", err); logger.error("An error occured:", err);
} }
} }
}) });
BotClient.login(BOT_TOKEN); BotClient.login(BOT_TOKEN);

View file

@ -26,27 +26,31 @@ import {
const submit = new ModalBuilder() const submit = new ModalBuilder()
.setCustomId("submitConfession") .setCustomId("submitConfession")
.setTitle("Submit Confession") .setTitle("Submit Confession");
const confessionInput = new TextInputBuilder() const confessionInput = new TextInputBuilder()
.setCustomId("confessionInput") .setCustomId("confessionInput")
.setLabel("Confession") .setLabel("Confession")
.setRequired(true) .setRequired(true)
.setMaxLength(2000) .setMaxLength(2000)
.setStyle(TextInputStyle.Paragraph) .setStyle(TextInputStyle.Paragraph);
// TODO: Add support for attachments const attachmentInput = new TextInputBuilder()
// .setCustomId("confessionAttachment")
// const attachmentInput = new TextInputBuilder() .setLabel("Attachment (optional)")
// .setCustomId("confessionAttachment") .setRequired(false)
// .setLabel("Attachment (optional)") .setStyle(TextInputStyle.Short);
// .setRequired(false)
// .setStyle(TextInputStyle.Short)
const actionRow = new ActionRowBuilder<ModalActionRowComponentBuilder>() const confessionRow =
.addComponents(confessionInput); new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(
// .addComponents(confessionInput, attachmentInput); confessionInput
);
submit.addComponents(actionRow); const attachmentRow =
new ActionRowBuilder<ModalActionRowComponentBuilder>().addComponents(
attachmentInput
);
submit.addComponents(confessionRow, attachmentRow);
export { submit }; export { submit };

View file

@ -38,14 +38,16 @@ export class StoreMan {
id: string, id: string,
author: string, author: string,
authorId: string, authorId: string,
content: string content: string,
attachment?: string
): Confession { ): Confession {
return { return {
id: id, id: id,
messageId: message.id, messageId: message.id,
author: author, author: author,
authorId: authorId, authorId: authorId,
content: content content: content,
attachment: attachment
}; };
} }
@ -122,7 +124,8 @@ export class StoreMan {
id: string, id: string,
author: string, author: string,
authorId: string, authorId: string,
content: string content: string,
attachment?: string
): boolean { ): boolean {
const guildId = message.guild?.id; const guildId = message.guild?.id;
@ -134,7 +137,14 @@ export class StoreMan {
} }
guild.confessions.push( guild.confessions.push(
StoreMan.toConfession(message, id, author, authorId, content) StoreMan.toConfession(
message,
id,
author,
authorId,
content,
attachment
)
); );
this.saveFile(); this.saveFile();
return true; return true;
@ -243,7 +253,9 @@ export class StoreMan {
if (guild.id === guildId) { if (guild.id === guildId) {
if (this.getConfession(guildId, confessionId)) { if (this.getConfession(guildId, confessionId)) {
guild.settings.bans = guild.settings.bans.filter(ban => { 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(); this.saveFile();

View file

@ -22,6 +22,7 @@ export interface Confession {
author: string; author: string;
authorId: string; authorId: string;
content: string; content: string;
attachment?: string;
} }
export interface ConfessionBan { export interface ConfessionBan {