【问题标题】:TypeError: Cannot read property 'send' of undefined when trying to direct message a userTypeError:尝试直接向用户发送消息时无法读取未定义的属性“发送”
【发布时间】:2021-03-25 08:24:52
【问题描述】:

我正在尝试创建一个允许我的机器人向人们发送消息的系统,但是在尝试向自上次重新启动以来尚未向机器人发送 dm 的用户发送消息时似乎总是出现错误。

这是我不断收到的错误:

TypeError: Cannot read property 'send' of undefined

这是重要的代码:

            message.channel.send({ embed: dmattempt }).then(sentMessage => {
                try {
                    // Tries to send a message to the user
                    const themsguser = client.users.cache.get(dmId);
                    themsguser.send(`${sentence}`);

这是整个命令的代码:

client.on('message', async message => {
    function directMsgUser(dmUser, dmCommand, dmId) {
        if (message.content.startsWith(`${prefix}${dmCommand}`)) {
            let sentence = message.content.split(' ');
            sentence.shift();
            sentence = sentence.join(' ');
            const dmattempt = {
                color: [0, 0, 255],
                title: `trying to direct message ${dmUser}`,
                description: 'please wait',
                footer: {
                    text: `message sending from ${message.author.tag}`,
                    icon_url: `${message.author.avatarURL()}`,
                },
            };
            const msgsent = {
                color: [0, 255, 0],
                title: `message sent to ${dmUser}`,
                description: `message content: ${sentence}`,
                footer: {
                    text: `message sent by ${message.author.tag}`,
                    icon_url: `${message.author.avatarURL()}`,
                },
            };
            // Sends a message confirming that the message is trying to be sent
            console.log(`Trying to direct message ${dmUser}`);
            message.channel.send({ embed: dmattempt }).then(sentMessage => {
                try {
                    // Tries to send a message to the user
                    const themsguser = client.users.cache.get(dmId);
                    themsguser.send(`${sentence}`);
                    // Edits the message to verify it was sent
                    sentMessage.edit({ embed: msgsent });
                    console.log('Message sent!');
                }
                catch (error) {
                    // Edits the message to verify something went wrong
                    const errorembed = {
                        color: [255, 0, 0],
                        title: 'error!',
                        description: `There was an error messaging ${dmUser}`,
                        footer: {
                            text: `message sent by ${message.author.tag}`,
                            icon_url: `${message.author.avatarURL()}`,
                        },
                    };
                    sentMessage.edit({ embed: errorembed });
                    console.error(error);
                    console.error('There was an error sending a direct message.');
                }
            });
        }
    }
    if (message.content.startsWith(`${prefix}dm`)) {
        directMsgUser(theUser, theCommand, idforuser);
    }
});

请注意,如果要向其发送消息的用户在机器人重新启动后已向机器人发送了 dm,则此命令有效。

提前感谢您的帮助。

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    问题可能是由于您尝试从缓存中获取用户,因此该用户可能尚未在缓存中。如果用户之前已经向机器人发送了 DM,这将解释为什么它可以工作,因为这样做会将它们添加到缓存中。但如果他们之前没有 DM 过该机器人,那么他们可能还没有在用户缓存中。

    您可以使用.fetch(id, cache) 而不是.cache.get(id) 以确保您获取用户,即使他们尚未在缓存中:

    client.users.fetch(dmID, true).then(themsguser => {
        themsguser.send(`${sentence}`);
    });
    

    此方法将使机器人首先检查 ID 为 dmID 的用户是否在缓存中。如果是,它将使用缓存中的值。如果不是,它将通过直接请求 Discord API 来获取用户。并且因为我们将.fetch() 的第二个参数设置为true,该方法还将在获取用户后将其添加到缓存中(如果它尚未在缓存中),因此无需重复请求数据来自 API。

    相关资源:
    https://discord.js.org/#/docs/main/stable/class/UserManager?scrollTo=fetch

    【讨论】:

      猜你喜欢
      • 2021-10-23
      • 2022-01-10
      • 2023-03-09
      • 1970-01-01
      • 2020-06-17
      • 2021-11-02
      • 2021-04-28
      • 2021-03-26
      • 2021-04-12
      相关资源
      最近更新 更多