Discord.js: DM'ing a user does not work

Created on 8 Jun 2020  路  2Comments  路  Source: discordjs/discord.js

I've faced an issue regarding DM'ing a specific user.

This command in my bot basically compares today's date with the date of the assignment and it is supposed to notify the user with this assignment if his deadline is near or has expired. Therefore, I take useralert field with user's ID and then transform it into numeric ID (useralertID). On if statement message should be sent to user with this ID when if statement is true.

I defined a user let user = bot.users.cache.get('useralertID'); and sent a message to this user user.send('Works!');.

Unfortunately, instead of messaging a user, it outputs an undefined value or UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined.

I cannot figure out the issue, so any advice will be really helpful!

var i;
var d = new Date;
var month = d.getMonth() + 1;
var day = d.getDate();
const s = await Tags.count();
if (message.member.hasPermission('KICK_MEMBERS')) {
  for (i = 1; i <= s; i++) {
    const tag = await Tags.findOne({
      where: {
        key: i
      }
    });
    if (tag) {
      var date = tag.get('description');
      let useralert = (tag.get("usernameid")).toString();
      let useralertID = useralert.replace(/[<@>]/g, '');
      let deadday = parseInt(date.slice(0, 2));
      let deadmonth = parseInt(date.slice(3, 5));
      let dayn = deadday - day;
      let monthn = deadmonth - month;
      console.log(dayn.toString() + ' ' + monthn.toString() + ' ' + useralertID);
      if (((dayn <= 2) && (monthn == 0)) || (monthn < 0)) {
        let user = bot.users.cache.get('useralertID');
        user.send('Works!');
        return message.channel.send(`Sent msg to ${user.username}!`);
      }
    }
  }

P.S.

  • discord.js v12.2.0,:
  • Node.js v10.15.3:
  • MacOS Mojave 10.14.5:
question (please use Discord instead)

Most helpful comment

Issue resolved. For anyone interested appropriate usage is:

let user = message.guild.members.cache.get(useralertID);
if (user) {
user.send(`works!`).then(() => {
                  return message.channel.send(`Sent msg to user!`);
                            }).catch(() => {
                                return message.channel.send(`User restricted DM's`);
                            })
                        } else {
                              return message.channel.send(`User is not in the guild!`);
                        }

All 2 comments

If you need help with discord.js installation or usage, please go to the discord.js Discord server instead:
https://discord.gg/bRCvFy9
This issue tracker is only for bug reports and enhancement suggestions. You won't receive any basic help here.

Issue resolved. For anyone interested appropriate usage is:

let user = message.guild.members.cache.get(useralertID);
if (user) {
user.send(`works!`).then(() => {
                  return message.channel.send(`Sent msg to user!`);
                            }).catch(() => {
                                return message.channel.send(`User restricted DM's`);
                            })
                        } else {
                              return message.channel.send(`User is not in the guild!`);
                        }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Dmitry221060 picture Dmitry221060  路  3Comments

tom-barnes picture tom-barnes  路  3Comments

DatMayo picture DatMayo  路  3Comments

PassTheMayo picture PassTheMayo  路  3Comments

tiritto picture tiritto  路  3Comments