Change Role icon (Emoji)

I want to change the icon for a specific roll with an emoji. I'm trying to do that, but I'm getting some problems. I hope you can help me. I'm not an expert in djs.
Code:
  if (selectedOption === 'changeIcon') {
                    let iconEmbed = new MessageEmbed().setDescription('Please provide a new icon URL or emoji for the role within 1 minute.');
                    await interaction.reply({ embeds: [iconEmbed], ephemeral: true });
    
                    const iconFilter = response => response.author.id === message.author.id;
                    const collectedIcon = await interaction.channel.awaitMessages({ filter: iconFilter, max: 1, time: 60000 });
    
                    if (collectedIcon.size > 0) {
                        let newIcon = collectedIcon.first().content;
    
                        try {
                            // If it's an emoji, check if it's a valid emoji
                            if (newIcon.startsWith('<:') && newIcon.endsWith('>')) {
                                // Custom emoji
                                const emojiId = newIcon.match(/\d+/)[0];
                                newIcon = `https://cdn.discordapp.com/emojis/${emojiId}.png`;
                            } else if (/^https?:\/\/.+\.(jpg|jpeg|png|gif)$/i.test(newIcon)) {
                                // URL to an image
                            } else {
                                throw new Error('Invalid URL or emoji');
                            }
    
                            await role.setIcon(newIcon);
                            let successEmbed = new MessageEmbed().setDescription('Role icon changed successfully.');
                            await interaction.followUp({ embeds: [successEmbed], ephemeral: true });
                        } catch (error) {
                            let errorEmbed = new MessageEmbed().setDescription('Failed to change role icon. Ensure the URL is valid or the emoji is accessible.');
                            await interaction.followUp({ embeds: [errorEmbed], ephemeral: true });
                        }
                    } else {
                        let timeoutEmbed = new MessageEmbed().setDescription('Time expired. Role icon not changed.');
                        await interaction.followUp({ embeds: [timeoutEmbed], ephemeral: true });
                    }
                }

error:
Unhandled promise rejection: Error: Invalid URL or emoji
    at InteractionCollector.<anonymous> (C:\Users\itsre\Europeans Province Boys\سطح المكتب\ProSystem\backup.js:270:39)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
image.png
Was this page helpful?