将DM发送给用户的Discord.js问题

时间:2020-05-19 12:49:28

标签: javascript node.js discord discord.js

每当有人报告该机器人有问题时,我都试图让该机器人DM给我,但是每次运行该命令时,我都会不断收到以下控制台错误:

               user.send('An issue was reported by ' + message.author.username + ' in ' + message.channel.name + ', ' + message.guild.name + ':\n' + args[1-100]);
                 ^

TypeError: Cannot read property 'send' of undefined
    at Client.<anonymous> (C:\Users\me\Desktop\Bot\index.js:50:18)
    at Client.emit (events.js:310:20)
    at MessageCreateAction.handle (C:\Users\me\Desktop\Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\me\Desktop\Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\me\Desktop\Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\Users\me\Desktop\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\me\Desktop\Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\me\Desktop\Bot\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:310:20)
    at Receiver.receiverOnMessage (C:\Users\me\Desktop\Bot\node_modules\ws\lib\websocket.js:800:20)

这是我的代码:

const user = bot.users.cache.get('698238773657483344');
 case 'report':
            message.channel.bulkDelete(1);
            message.channel.send('report sent')
            console.log('An issue was reported by ' + message.author.username + ' in ' + message.channel.name + ', ' + message.guild.name + ': ' + args);

            user.send('An issue was reported by ' + message.author.username + ' in ' + message.channel.name + ', ' + message.guild.name + ':\n' + args[1-100]);
            break;

注意:那不是我的实际用户ID。

另一个注意事项:代码段是switch循环的一部分,而const位于我的代码的顶部,而不是循环本身

如果有人知道该问题的解决方案,我将不胜感激。

3 个答案:

答案 0 :(得分:1)

您确定机器人是您的客户端吗? const bot = new Discord.Client(options); bot.users.cache.get('698238773657483344'); 如果您有客户作为您的客户,请尝试 client.users.cache.get('id')

here

答案 1 :(得分:1)

在我的终端上对其进行了尝试,并且运行良好,我唯一的猜测是该用户尚未被该漫游器缓存。该ID的用户是否与机器人共享公会?阅读here

.users- 在任何时候都已缓存的所有User对象,均按其ID映射

答案 2 :(得分:1)

这似乎没有缓存目标用户,您可以使用.fetch() method确保在未缓存的情况下获取它(如果在缓存中可用,则机器人将使用缓存)

// This will fetch the user with the 698238773657483344 ID.
bot.users.fetch("698238773657483344").then(user => {
    // Up to you here to adapt to your code, at this state, if user is undefined
    // it means the ID is wrong / there is an issue while fetching.
});

如果在user情况下只需要report var,则可以这样做:

case 'report':
    bot.users.fetch("698238773657483344").then(user => {
        message.channel.bulkDelete(1);
        message.channel.send('report sent');
        console.log('An issue was reported by ' + message.author.username + ' in ' + message.channel.name + ', ' + message.guild.name + ': ' + args);

        user.send('An issue was reported by ' + message.author.username + ' in ' + message.channel.name + ', ' + message.guild.name + ':\n' + args[1-100]);
    });

    break;