You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.js v13查询指定用户信息仅返回自身信息报错问题

问题修复方案

核心错误原因

  • 代码结构不符合discord.js v13斜杠命令的规范:导出的命令对象缺少必要的execute异步方法,代码逻辑直接写在对象声明内部属于语法错误
  • Embed构造时直接固定使用interaction.user(命令发起者)的信息,没有使用获取到的目标用户参数,自然永远返回发起者自己的信息
  • 直接使用未定义的guildMember变量,才会抛出TypeError报错,要获取服务器成员对象需要主动从交互参数中调用getMember方法,同时需要确认bot已经开启GUILD_MEMBERS网关意图

修复后可用代码

const { SlashCommandBuilder, MessageEmbed } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('info')
        .setDescription('查询用户或者服务器信息')
        .addSubcommand(subcommand =>
            subcommand
                .setName('user')
                .setDescription('查询指定用户的信息')
                .addUserOption(option => option.setName('target').setDescription('要查询的目标用户'))),
    async execute(interaction) {
        // 优先取传入的目标用户,没传参数就默认查询命令发起者的信息
        const targetUser = interaction.options.getUser('target') || interaction.user;
        const targetMember = interaction.options.getMember('target') || interaction.member;

        const userInfo = new MessageEmbed()
            .setColor('#0099ff')
            .setTitle('用户信息查询结果')
            .setAuthor(targetUser.tag, targetUser.displayAvatarURL({ dynamic: true }))
            .setThumbnail(targetUser.displayAvatarURL({ dynamic: true }))
            .addFields(
                { name: '用户ID', value: targetUser.id, inline: true },
                { name: '用户名', value: targetUser.tag, inline: true },
                { name: '加入服务器时间', value: `<t:${parseInt(targetMember.joinedTimestamp / 1000)}:R>`, inline: true },
                { name: '账号注册时间', value: `<t:${parseInt(targetUser.createdTimestamp / 1000)}:R>`, inline: true }
            )
            .setTimestamp()
            .setFooter('查询完成');
        
        await interaction.reply({ embeds: [userInfo] });
    }
};

额外注意事项

启动bot的代码中需要注册GuildMembers意图,示例:

const { Client, Intents } = require('discord.js');
const client = new Client({ 
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MEMBERS, // 必须开启才能正常获取服务器成员信息
        Intents.FLAGS.GUILD_MESSAGES
    ] 
});

如果只需要用户的公开基础信息,不需要服务器专属属性(比如加入时间、身份组),直接用targetUser即可,不需要额外获取GuildMember对象。

内容的提问来源于stack exchange,提问作者MunJitso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 22:00:01