【问题标题】:discord.js v13 | can't get targeted user's infosdiscord.js v13 |无法获取目标用户的信息
【发布时间】:2021-10-30 07:38:24
【问题描述】:

我正在使用 discord.js 版本 13.1.0 和 node.js 版本 16.7.0,我编写了这段代码以在嵌入中显示用户信息,但问题是它只显示我的信息。

我也试过这个guildMember.user 而不是user,但它无法执行它在控制台TypeError: Cannot read property 'user' of undefined 中显示的命令。

这是命令的代码:

module.exports = {
    data: new SlashCommandBuilder()
        .setName('info')
        .setDescription('Get info about a user or a server!')
        .addSubcommand(subcommand =>
            subcommand
                .setName('user')
                .setDescription('Info about a user')
                .addUserOption(option => option.setName('target').setDescription('The user'))),
    async userInfo = new MessageEmbed()
                .setColor('#0099ff')
                .setTitle('Some title')
                .setAuthor('Some name', interaction.user.displayAvatarURL({ dynamic: true }))
                .setDescription('Some description here')
                .setThumbnail(interaction.user.displayAvatarURL({ dynamic: true }))
                .addFields(
                    { name: 'Hello', value: 'Hello' },
                    { name: '\u200B', value: '\u200B' },
                    { name: 'ID', value: interaction.user.id, inline: true },
                    { name: 'Username', value: interaction.user.tag, inline: true },
                )
                .setTimestamp()
                .setFooter('Some footer text here', 'https://i.imgur.com/AfFp7pu.png');
    if (interaction.options.getSubcommand() === 'user') {
            const user = interaction.options.getUser('target');
            if (user) {
            await interaction.reply({ embeds: [userInfo] });
   };
};

【问题讨论】:

  • 您的机器人中是否启用了特权意图?
  • interaction 定义在哪里?
  • 这段代码甚至不是有效的 javascript...SlashCommandBuilder 为您的命令构建元数据,您应该在函数或 interactionCreate 侦听器中处理交互。
  • @G-Force 是的,我在我的机器人 (index.js) 的主文件中写了 const client = new Client({ intents:[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
  • @MrMythical 在 index.js 文件中我还没有创建事件处理程序,看!代码正在运行我说问题是机器人只返回我的信息,即使我针对某人获取他的信息,希望你能在下面看到我的答案

标签: javascript node.js discord.js bots


【解决方案1】:

无论如何,问题在于它是user.'property or method' 之前的interaction,当您将interaction 放在类user 之前时,您只需指定使用斜杠命令不和谐地命令机器人的用户,当您删除它时,您指定目标用户。我希望你能理解这是你的一个例子:

data: new SlashCommandBuilder()
    .setName('user-tag')
    .setDescription('show a user\'s tag')
    .addUserOption(option => option.setName('target').setDescription('to show the targeted user\'s tag')),

async execute(interaction) {
    const user = interaction.options.getUser('target');
    if (user) {
        //to show targeted user's tag
        return interaction.reply(`the user\'s tag :${user.tag}`);
    } else {
        //to show your tag if you were the commander
        return interaction.reply(`the user\'s tag :${interaction.user.tag}`);
    }

如果您有任何关于interaction的其他信息,您可以将信息来源或在评论中告诉我们。

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 2021-11-03
    • 2021-12-25
    • 1970-01-01
    • 2021-12-24
    • 2021-09-25
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多