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 });
}
}
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)
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)
No description
9 Replies
d.js toolkit
d.js toolkit7mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Toast
Toast7mo ago
You aren't really checking the unicode emojis. The wrench emoji in your screenshot is actually unicode and does not have an URL that I'm aware of.
kuroiii
kuroiiiOP7mo ago
I did not understand that, but what I understood is that I do not put any link to any emoji. If that is what you mean, then I know the newIcon and put the emoji id and then in png format. I think you do not mean that, but this is what I understood i installed all intents (3276799)
d.js docs
d.js docs7mo ago
We highly recommend only specifying the intents you actually need. - Note, that 98303, 32767 or whatever other magic number you read that represents "all intents", gets outdated as soon as new intents are introduced. - The number will always represent the same set of intents, and will not include new ones. There is no magic "all intents" bit.
Amgelo
Amgelo7mo ago
@FAISALᴱᵃ ^^
kuroiii
kuroiiiOP7mo ago
v13
Squid
Squid7mo ago
The message content that you collected is 🔧 . The first if condition fails since that does not start with <:, and the else if condition fails since it does not match the regex, so the logic goes to the else block.
Amgelo
Amgelo7mo ago
the tag applies to both v13 and v14
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server