Skip to content

Commit

Permalink
Add image inputs in announce (#29)
Browse files Browse the repository at this point in the history
* feat: add image inputs

* chore: update limit

* chore: change embed logic

* chore: use lowercase

* fix: change custom id
  • Loading branch information
SachinPrasanth777 authored Jan 19, 2024
1 parent 3912a97 commit 145af7f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/commands/announce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,18 @@ export default {
.setLabel("Provide us with some Description")
.setStyle(TextInputStyle.Paragraph)
.setMaxLength(1900);
const Image = new TextInputBuilder()
.setCustomId("image")
.setLabel("Provide us with the Image")
.setStyle(TextInputStyle.Paragraph)
.setMinLength(10)
.setMaxLength(4000)
.setRequired(false);

const firstActionRow = new ActionRowBuilder<TextInputBuilder>().addComponents(Title);
const secondActionRow = new ActionRowBuilder<TextInputBuilder>().addComponents(Description);
modal.addComponents(firstActionRow, secondActionRow);
const thirdActionRow = new ActionRowBuilder<TextInputBuilder>().addComponents(Image);
modal.addComponents(firstActionRow, secondActionRow, thirdActionRow);
await interaction.showModal(modal);
},
} as Command;
50 changes: 47 additions & 3 deletions src/events/handleModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@ import { COLOR, FOOTER_VALUE } from "../config/constant";
import db from "../utils/database";
import { TemplateSchemaType } from "../types";

async function isValidImageUrl(url: string): Promise<boolean> {
try {
const response = await fetch(url, { method: "HEAD" });

if (!response.ok) {
return false;
}

const contentType = response.headers.get("Content-Type");
if (!contentType || !contentType.startsWith("image/")) {
return false;
}

return true;
} catch (error) {
return false;
}
}

export default {
name: Events.InteractionCreate,
once: false,
Expand Down Expand Up @@ -56,20 +75,45 @@ export default {
}

if (action === "announce") {
const embeds: EmbedBuilder[] = [];
const embed = new EmbedBuilder()
.setTitle(title)
.setDescription(description)
.setColor(COLOR.WHITE as ColorResolvable)
.setTimestamp()
.setFooter({ text: FOOTER_VALUE });

if (mention !== "none") {
await channel.send({ content: `📢 Announcement ${mention}`, embeds: [embed] });
const image = interaction.fields.getTextInputValue("image") || "none";
if (image === "none") {
if (mention === "none") {
await channel.send({ embeds: [embed] });
} else {
await channel.send({ content: `📢 Announcement ${mention}`, embeds: [embed] });
}
await interaction.reply({ content: `Embed sent to <#${channel.id}>` });
return;
}
const images = image.split("\n");
const validImages = images.filter(url => isValidImageUrl(url));

await channel.send({ embeds: [embed] });
if (validImages.length > 0) {
const firstImage = validImages.shift();
if (firstImage) {
embed.setImage(firstImage);
}
}
embeds.push(embed);

validImages.forEach(url => {
const newEmbed = new EmbedBuilder().setImage(url).setColor(COLOR.WHITE as ColorResolvable);
embeds.push(newEmbed);
});
if (mention !== "none") {
await channel.send({ content: `📢 Announcement ${mention}`, embeds: embeds });
await interaction.reply({ content: `Embed sent to <#${channel.id}>` });
return;
}
await channel.send({ embeds: embeds });
await interaction.reply({ content: `Embed sent to <#${channel.id}>` });
} else if (action === "echo") {
if (mention !== "none") {
Expand Down

0 comments on commit 145af7f

Please sign in to comment.