Discord bot 未能调用调用它的人的用户名

Discord bot failing to call username of person who calls it

我正在尝试让我的机器人在有人告诉它时说一条简单的消息 'hello'。目前,命令本身的代码如下所示:

const { SlashCommandBuilder } = require('@discordjs/builders');
const greeter = "default";
const greetOptions = [
    `Hello, ${greeter}. It's very nice to hear from you today.`
]
module.exports = {
    data: new SlashCommandBuilder()
        .setName('hello')
        .setDescription('say hi to Hal!'),
    execute(message, args) {
        let greeter = message.user.username;
        msg.channel.send(greetOptions[Math.floor(Math.random() * greetOptions.length)]);
    },
};

键入命令时我用来管理的代码如下所示:

let prefix = "hal, ";
client.on('messageCreate', message => {
    if (!message.content.startsWith(prefix)) return;

    const args = message.content.slice(prefix.length);
    const command = args.toLowerCase();
    console.log(`${message.author.tag} called ${command}`);

    if (!client.commands.has(command)) return;

    try {
        client.commands.get(command).execute(message, args);
    } catch (error) {
        console.error(error);
        message.channel.send({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});

当我 运行 它时,它抛出错误,“无法读取未定义的属性(读取 'username')。

如果您想提及某个用户,您可以 ${message.author}。但是如果你想说前任。 Hello, Brandon 然后你需要做 ${message.author.username}。消息 ${message.author.tag} 并不总是有效,我还建议您使用 const user = message.mentions.users.first() || message.author 或简称 const user = message.author,这样您就可以使用 ${user.username}。如果它不告诉我,这可能会修复机器人无法响应的问题。 修复第一个代码并将 msg.channel.send 更改为 message.channel.send.