code

mail@pastecode.io avatar
unknown
javascript
a month ago
7.7 kB
3
Indexable
Never
const { SlashCommandBuilder } = require('@discordjs/builders');
const { ChannelType } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('embedbuilder')
        .setDescription('Send a custom embed [DEV ONLY]. A.K.A EmbedBuilder, SendEmbed')
        .addChannelOption(option =>
            option.setName('channel')
                .setDescription('The channel to send the embed to.')
                .setRequired(true)
                .addChannelTypes([ChannelType.GUILD_TEXT]))
        .addBooleanOption(option =>
            option.setName('show-timestamp')
                .setDescription('Show timestamp on the embed')
                .setRequired(false))
        .addStringOption(option =>
            option.setName('image-url')
                .setDescription('Image URL (optional)')
                .setRequired(false)),

    async execute(interaction) {
        try {
            // Check if the user has any of the admin roles
            const adminRoleIds = config.adminRoleId;
            const member = interaction.guild.members.cache.get(interaction.user.id);
            
            // Check if adminRoleIds is an array and user has any of these roles
            const hasAdminRole = Array.isArray(adminRoleIds) && adminRoleIds.some(roleId => member.roles.cache.has(roleId));

            if (!hasAdminRole) {
                return interaction.reply({
                    content: "You do not have permission to use this command.",
                    ephemeral: true,
                });
            }

            const modal = new ModalBuilder()
                .setCustomId('embedModal')
                .setTitle('Embed Builder');

            const titleInput = new TextInputBuilder()
                .setCustomId('title')
                .setLabel('Title')
                .setStyle(TextInputStyle.Short)
                .setRequired(true);

            const descriptionInput = new TextInputBuilder()
                .setCustomId('description')
                .setLabel('Description')
                .setStyle(TextInputStyle.Paragraph)
                .setRequired(true);

            const colorInput = new TextInputBuilder()
                .setCustomId('color')
                .setLabel('Color (hex code, optional)')
                .setStyle(TextInputStyle.Short)
                .setRequired(false);

            const footerInput = new TextInputBuilder()
                .setCustomId('footer')
                .setLabel('Footer (optional)')
                .setStyle(TextInputStyle.Short)
                .setRequired(false);

            const thumbnailUrlInput = new TextInputBuilder()
                .setCustomId('thumbnailUrl')
                .setLabel('Thumbnail URL (optional)')
                .setStyle(TextInputStyle.Short)
                .setRequired(false);

            modal.addComponents(
                new ActionRowBuilder().addComponents(titleInput),
                new ActionRowBuilder().addComponents(descriptionInput),
                new ActionRowBuilder().addComponents(colorInput),
                new ActionRowBuilder().addComponents(footerInput),
                new ActionRowBuilder().addComponents(thumbnailUrlInput)
            );

            await interaction.showModal(modal);

            const filter = (i) => i.customId === 'embedModal' && i.user.id === interaction.user.id;
            interaction.awaitModalSubmit({ filter, time: 60000 })
                .then(async modalInteraction => {
                    const title = modalInteraction.fields.getTextInputValue('title');
                    const description = modalInteraction.fields.getTextInputValue('description');
                    const color = modalInteraction.fields.getTextInputValue('color') || Colors.Green;
                    const footer = modalInteraction.fields.getTextInputValue('footer');
                    const thumbnailUrl = modalInteraction.fields.getTextInputValue('thumbnailUrl');
                    const showTimestamp = interaction.options.getBoolean('show-timestamp') || false;
                    const imageURL = interaction.options.getString('image-url');

                    const embed = new EmbedBuilder()
                        .setTitle(title)
                        .setDescription(description)
                        .setColor(color);

                    if (footer) embed.setFooter({ text: footer });
                    if (thumbnailUrl) embed.setThumbnail(thumbnailUrl);
                    if (showTimestamp) embed.setTimestamp();
                    if (imageURL) embed.setImage(imageURL);

                    const sendButton = new ButtonBuilder()
                        .setCustomId('sendButton')
                        .setLabel('Send')
                        .setStyle(ButtonStyle.Success);

                    const editButton = new ButtonBuilder()
                        .setCustomId('editButton')
                        .setLabel('Edit')
                        .setStyle(ButtonStyle.Primary);

                    const cancelButton = new ButtonBuilder()
                        .setCustomId('cancelButton')
                        .setLabel('Cancel')
                        .setStyle(ButtonStyle.Danger);

                    const actionRow = new ActionRowBuilder().addComponents(sendButton, editButton, cancelButton);

                    await modalInteraction.reply({ embeds: [embed], components: [actionRow], ephemeral: true });

                    const buttonFilter = (i) => ['sendButton', 'editButton', 'cancelButton'].includes(i.customId) && i.user.id === interaction.user.id;
                    const collector = modalInteraction.channel.createMessageComponentCollector({ filter: buttonFilter, time: 60000 });

                    collector.on('collect', async buttonInteraction => {
                        if (buttonInteraction.customId === 'sendButton') {
                            const channel = interaction.options.getChannel('channel');
                            if (channel.type === 'GUILD_TEXT' && channel.permissionsFor(interaction.user).has(Permissions.FLAGS.SEND_MESSAGES)) {
                                await channel.send({ embeds: [embed] });
                                await buttonInteraction.update({ content: 'Embed sent successfully!', embeds: [], components: [], ephemeral: true });
                            } else {
                                await buttonInteraction.update({ content: 'You do not have permission to send messages in the chosen channel.', embeds: [], components: [], ephemeral: true });
                            }
                        } else if (buttonInteraction.customId === 'editButton') {
                            await buttonInteraction.update({ content: 'Please edit your embed.', components: [], ephemeral: true });
                            await interaction.showModal(modal);  // Restart the process for editing
                        } else if (buttonInteraction.customId === 'cancelButton') {
                            await buttonInteraction.update({ content: 'Embed creation canceled.', embeds: [], components: [], ephemeral: true });
                        }
                    });
                })
                .catch(console.error);

        } catch (error) {
            console.error('Error executing embedbuilder:', error);
            interaction.reply({
                content: 'An error occurred while executing the command.',
                ephemeral: true
            });
        }
    },
};
Leave a Comment