#Cannot read properties of undefined (reading 'path') in JS

3 messages · Page 1 of 1 (latest)

peak root
#

Trying to have my discord bot post an image in a channel, and it keeps giving me this error, and I can't figure out why. Here's the code

        async execute(interaction) {
            try {
                const adminRoleId = process.env.ADMIN_ROLE_ID;
                const channelId = process.env.CHANNEL_ID;
                const imagePath = path.join(__dirname, '..', 'images', 'animationv2.gif');
    
                console.log('Image path:', imagePath); // Add debug output to check the path
    
                const isAdmin = interaction.member.roles.cache.has(adminRoleId);
                if (!isAdmin) {
                    return await interaction.reply({
                        content: 'You do not have permission to use this command!',
                        ephemeral: true
                    });
                }
    
                const imageStream = fs.createReadStream(imagePath);
                const attachment = { files: [imageStream] };
    
                const channel = await interaction.client.channels.fetch(channelId);
                if (!channel) throw new Error('Channel not found.');
    
                await channel.send({ content: 'Here is the image:', files: [attachment] });
                await interaction.reply('Image posted successfully!');
            } catch (error) {
                console.error('Error posting image:', error);
                await interaction.reply('There was an error posting the image.');
            }
        },
languid seal
#

Is that valid JS? Join should make a string from the input array using the provided separator? js path = ['foo','bar']; path.join('..', 'images', 'animationv2.gif'); Output: 'foo..bar'

#

Since both the path variable and __dirname are undefined for your example it's hard to guess.