Dinty
Dinty

Reputation: 642

Discord.js Issue with Sending a DM to a user

I am trying to get the bot to DM me whenever someone reports an issue with said bot but every time the command is run, I keep getting the following console error:

               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)

Here is my code:

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;

Note: that is not my actual user ID.

Another Note: The snippet is part of a switch loop and the const is at the top of my code, not in the loop itself

I would really appreciate if someone knew the solution to this issue.

Upvotes: 0

Views: 354

Answers (3)

Draityalan
Draityalan

Reputation: 710

This seems like the aimed user isn't cached, you can use .fetch() method to be sure to get it in case it is not cached (If it is available in cache the bot will use cache instead)

// 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.
});

If you only need the user var in your report case, you can do it like so :

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;

Upvotes: 1

Viriato
Viriato

Reputation: 141

Tried it on my end and it worked fine, my only guess is that that user hasn't been cached by the bot yet. Does the user of that ID share a guild with the bot? Read here

.users - All of the User objects that have been cached at any point, mapped by their IDs

Upvotes: 1

Blobslam
Blobslam

Reputation: 13

are you sure that bot is your client? const bot = new Discord.Client(options); bot.users.cache.get('698238773657483344'); if you have client as your client try client.users.cache.get('id')

Look here

Upvotes: 1

Related Questions