简体   繁体   English

Discord.js:类型错误:无法读取 null 的属性(读取“状态”)

[英]Discord.js: TypeError: Cannot read properties of null (reading 'status')

So I have a command that outputs user information.所以我有一个输出用户信息的命令。 Though most fields have no problems, the Status field is rather buggy for me.虽然大多数字段都没有问题,但 Status 字段对我来说还是有问题的。

This is the code for the command:这是命令的代码:

import { Command } from '@sapphire/framework';
import { MessageEmbed } from 'discord.js';

export class UserInfoCommand extends Command {
    constructor(context, options) {
        super(context, {
            ...options,
            name: 'userinfo',
            description: 'Retrives user information.',
            aliases: ['user'],
        });
    }

    async messageRun(message, args) {
        const userInfo = await args.pick('member').catch(() => message.member);
        const roleMap = userInfo.roles.cache.mapValues(roles => roles.name);
        const roleArray = Array.from(roleMap.values());

        const userEmbed = new MessageEmbed()
            .setTitle(`Information about ${userInfo.displayName}#${userInfo.user.discriminator}`)
            .setColor(0xC63D85)
            .setThumbnail(userInfo.displayAvatarURL())
            .addField('ID', `${userInfo.id}`, true)
            .addField('Status', `${userInfo.presence.status}`, true)
            .addField('Account Created:', `${userInfo.user.createdAt}`)
            .addField('Joined on:', `${userInfo.joinedAt}`)
            .addField('Server Nickname:', `${userInfo.displayName}`)
            .addField('Server Roles:', `${roleArray.join(', ')}`);
        return message.channel.send({ embeds: [userEmbed] });
    }
}

When executing the command with me (or a user offline since the bot was started), it throws TypeError: Cannot read properties of null (reading 'status') .当与我一起执行命令时(或自机器人启动以来用户离线),它抛出TypeError: Cannot read properties of null (reading 'status') When I go online and then offline once again, the command works and outputs offline as my status.当我 go 在线然后再次离线时,该命令起作用并输出离线作为我的状态。

I do have the proper intents enabled.我确实启用了正确的意图。

const client = new SapphireClient({
    intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MEMBERS', 'GUILD_PRESENCES'],
    disableMentionPrefix: true,
    typing: true,
    presence: {
        activities: [
            {
                name: 'Captain\'s commands!',
                type: 'LISTENING',
            },
        ],
    },
});

I've tried to use an if statement where if userInfo.presence.status is null then it should throw offline instead but that didn't work out.我尝试使用 if 语句,如果userInfo.presence.status是 null 那么它应该抛出离线但没有成功。 How can I make this work out properly?我怎样才能使这项工作正常进行?

Make sure you have the PRESENCE INTENT option enabled in the applications bot settings确保在应用程序机器人设置中启用了PRESENCE INTENT选项

This setting is required to get presence updates此设置是获取状态更新所必需的

Replaced the line with:将行替换为:

.addField('Status', `${userInfo.presence? userInfo.presence.status : "offline"}`, true) // if presence is truthy, output the string, else, the user is offline

A simple null check that worked.一个简单的 null 检查有效。 Probably my previous method was wrong.可能是我之前的方法不对。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Discord.js - TypeError:无法读取未定义的属性(读取“设置”) - Discord.js - TypeError: Cannot read properties of undefined (reading 'set') TypeError:无法读取未定义的属性(读取“设置”)Discord.js - TypeError: Cannot read properties of undefined (reading 'set') Discord.js 类型错误:无法读取未定义的属性(读取“缓存”)(discord.js) - TypeError: Cannot read properties of undefined (reading 'cache') (discord.js) Discord.js:TypeError:无法读取未定义的属性(读取“has”) - Discord.js: TypeError: Cannot read properties of undefined (reading 'has') Discord.js:TypeError:无法读取未定义的属性(读取“添加”) - Discord.js: TypeError: Cannot read properties of undefined (reading 'add') 类型错误:无法读取未定义的属性(读取“createdTimestamp”)discord.js - TypeError: Cannot read properties of undefined (reading 'createdTimestamp') discord.js 类型错误:无法读取 discord.js 中未定义的属性(读取“发送”) - TypeError: Cannot read properties of undefined (reading 'send') in discord.js discord.js 使用 axios:TypeError:无法读取未定义的属性(读取“短暂”) - discord.js using axios: TypeError: Cannot read properties of undefined (reading 'ephemeral') discord.js v13 类型错误:无法读取未定义的属性(读取“存在”) - discord.js v13 TypeError: Cannot read properties of undefined (reading 'presence') discord.js - 发送嵌入 - 类型错误:无法读取未定义的属性(读取“执行”) - discord.js - Sending an Embed - TypeError: Cannot read properties of undefined (reading 'execute')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM