Home > front end >  Interaction.author isn't defined but I already defined "interaction"
Interaction.author isn't defined but I already defined "interaction"

Time:02-12

Code::

fs.writeFile("./logs.txt", `Hug command executed by ${interaction.author}`, function(err) {
            if(err) {
                return console.log(err)
            }
            console.log("The file has been saved!");
        });

logs.txt: Logs.txt file

Console: Command prompt

Yes I did define interaction when making the slash command "async execute(interaction){ rest of code }"

CodePudding user response:

interaction.author does not exist

Instead you should use interaction.user

Your code will look like

fs.writeFile("./logs.txt", `Hug command executed by ${interaction.user}`, function(err) {
            if(err) {
                return console.log(err)
            }
            console.log("The file has been saved!");
        });

You can also get the tag/username by adding .tag, .username

Read docs to help yourself out

CodePudding user response:

So my IRL friend came online and helped me, he told me instead of going to each command and putting the same code there he told me to go to index.js and put this code

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    var member = interaction.user.tag

    const command = client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }

    fs.appendFile("logs.txt", `${interaction.commandName} command executed by ${member}\n`, function(err) {
        if(err) {
            return console.log(err)
        }
        console.log("The file has been saved!");
    });
});
  • Related