Discord js Slash command with embed ReferenceError: interaction is not define

Viewed 26

"title": ${interaction.user.username}'s Profile
ReferenceError: interaction is not defined

const { SlashCommandBuilder } = require('discord.js');
const { EmbedBuilder } = require('discord.js');

const profileEmbed = {
    "type": "rich",
    "color": 0x00fff0,
    "title": `${interaction.user.username}'s Profile`
    "avatar": ?
                
}

module.exports = {
    data: new SlashCommandBuilder()
        .setName('profile')
        .setDescription('Replies with your profile!'),
    async execute(interaction) {
        await interaction.reply({embeds: [profileEmbed]});
    },
};

Is there a way that I can use interactions in "title" and with "avatar" aswell? I get "interaction is not defined" error when I run node deploy-commands.js file

1 Answers

You could always turn the profileEmbed object into a function that returns the object, and just pass in the interaction.

function profileEmbed(interaction) {
  return {
    "type": "rich",
    "color": 0x00fff0,
    "title": `${interaction.user.username}'s Profile`
    "avatar": ?   
  }
}
await interaction.reply({embeds: [profileEmbed(interaction)]})

This may not be what you're looking for in terms of style, but it should accomplish what you're trying to do.

Related