'Interaction.author isn't defined but I already defined "interaction"

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 }"



Solution 1:[1]

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!");
    });
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shaun2177