Interaction.author 未定义,但我已经定义 "interaction"

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

代码:

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

控制台: Command prompt

是的,我在执行斜杠命令“async execute(interaction){ 时确实定义了 interaction 其余代码 }"

所以我的 IRL 朋友上线并帮助了我,他告诉我不要去每个命令并把相同的代码放在那里他告诉我去 index.js 并把这个代码

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

interaction.author不存在

你应该使用 interaction.user

您的代码将如下所示

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

您还可以通过添加 .tag.username

来获得 tag/username

阅读docs来帮助自己