url.js gives error

Code:
const { SlashCommandBuilder } = require('discord.js');
const fetch = require('node-fetch');

const data = new SlashCommandBuilder()
.setName('url')
.setDescription('Add an emoji from a PNG URL')
.addStringOption(option =>
option.setName('url')
.setDescription('The URL of the emoji image (PNG)')
.setRequired(true))
.addStringOption(option =>
option.setName('name')
.setDescription('The name you want to give to the emoji')
.setRequired(true));

async function execute(interaction) {
const emojiURL = interaction.options.getString('url');
const emojiName = interaction.options.getString('name');

try {
const response = await fetch(emojiURL);
if (!response.ok) {
return await interaction.reply('Failed to fetch the image. Make sure the URL is valid and accessible.');
}
const emojiData = await response.buffer();

// Check if the file is a PNG
if (!emojiURL.endsWith('.png')) {
return await interaction.reply('The provided URL is not a PNG image.');
}

// Add emoji to the guild
await interaction.guild.emojis.create(emojiData, emojiName)
.then(emoji => interaction.reply(`Emoji ${emoji.name} added successfully!`))
.catch(error => {
console.error('Error adding emoji:', error);
interaction.reply('Failed to add the emoji. Make sure the URL is valid and accessible, and you have the necessary permissions to add emojis.');
});
} catch (error) {
console.error('Error adding emoji:', error);
await interaction.reply('Failed to add the emoji. Make sure the URL is valid and accessible.');
}
}

module.exports = {
data: data,
execute: execute
};
const { SlashCommandBuilder } = require('discord.js');
const fetch = require('node-fetch');

const data = new SlashCommandBuilder()
.setName('url')
.setDescription('Add an emoji from a PNG URL')
.addStringOption(option =>
option.setName('url')
.setDescription('The URL of the emoji image (PNG)')
.setRequired(true))
.addStringOption(option =>
option.setName('name')
.setDescription('The name you want to give to the emoji')
.setRequired(true));

async function execute(interaction) {
const emojiURL = interaction.options.getString('url');
const emojiName = interaction.options.getString('name');

try {
const response = await fetch(emojiURL);
if (!response.ok) {
return await interaction.reply('Failed to fetch the image. Make sure the URL is valid and accessible.');
}
const emojiData = await response.buffer();

// Check if the file is a PNG
if (!emojiURL.endsWith('.png')) {
return await interaction.reply('The provided URL is not a PNG image.');
}

// Add emoji to the guild
await interaction.guild.emojis.create(emojiData, emojiName)
.then(emoji => interaction.reply(`Emoji ${emoji.name} added successfully!`))
.catch(error => {
console.error('Error adding emoji:', error);
interaction.reply('Failed to add the emoji. Make sure the URL is valid and accessible, and you have the necessary permissions to add emojis.');
});
} catch (error) {
console.error('Error adding emoji:', error);
await interaction.reply('Failed to add the emoji. Make sure the URL is valid and accessible.');
}
}

module.exports = {
data: data,
execute: execute
};
4 Replies
d.js toolkit
d.js toolkit9mo 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! - Marked as resolved by OP
</JustTheDev>
</JustTheDev>OP9mo ago
I get this error:
const fetch = require('node-fetch');
^

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\OE104655\Videos\JEMOJI DC Workspace\node_modules\node-fetch\src\index.js from C:\Users\OE104655\Videos\JEMOJI DC Workspace\commands\emoji\url.js not supported.
Instead change the require of index.js in C:\Users\OE104655\Videos\JEMOJI DC Workspace\commands\emoji\url.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\OE104655\Videos\JEMOJI DC Workspace\commands\emoji\url.js:2:15)
at Object.<anonymous> (C:\Users\OE104655\Videos\JEMOJI DC Workspace\deploy-commands.js:18:19) {
code: 'ERR_REQUIRE_ESM'
}

Node.js v18.18.0
const fetch = require('node-fetch');
^

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\OE104655\Videos\JEMOJI DC Workspace\node_modules\node-fetch\src\index.js from C:\Users\OE104655\Videos\JEMOJI DC Workspace\commands\emoji\url.js not supported.
Instead change the require of index.js in C:\Users\OE104655\Videos\JEMOJI DC Workspace\commands\emoji\url.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (C:\Users\OE104655\Videos\JEMOJI DC Workspace\commands\emoji\url.js:2:15)
at Object.<anonymous> (C:\Users\OE104655\Videos\JEMOJI DC Workspace\deploy-commands.js:18:19) {
code: 'ERR_REQUIRE_ESM'
}

Node.js v18.18.0
when i run the deploy-commands.js
Kinect3000
Kinect30009mo ago
You need to downgrade node-fetch to a version that supports ejs This isn’t an issue w/ d.js
</JustTheDev>
</JustTheDev>OP9mo ago
oh
Want results from more Discord servers?
Add your server