简体   繁体   中英

How to DM some user using 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.

Following 'Discord.js Guide''s instructions, 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!

Here's the code. Thanks in advance!

 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}!`); } } }

client.users its only cached users by client. So if you restart bot, this collection will be empty. You need send message to your bot or send message to channel where bot can handle this message to be cached in this collection. The you way, if you run this command on guild or if this user are member on your guild you can use guild.members.cache.get('ID HERE') or try get guild by id and then in this guild get user by his ID.

Some example:


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 = message.guild.members.cache.get(useralertID);
                if (user) {
                    user.send('Works!').then(() => {
                        return message.channel.send(`Sent msg to ${user.username}!`);
                    }).catch(() => {
                        return message.channel.send(`${user.username} not allow to send DM message!`);
                    })
                } else {
                    return message.channel.send(`the ${useralertID} not guild Member, can\`t send DM to him`);
                }

            }
        }
    }

The error occurs because user is not there or is null. Maybe try bot.users.cache.get("YOURIDHERE").send("hi") .
I cannot really help you any further because you did not tell us what you are trying to do.

Well the issue is that user is undefined like Internal and the error said,

let user = bot.users.cache.get('useralertID')

You're using a string here instead of the variable useralertID

Regardless you should check if user exists, because you are pulling the id from something indirect, not sure what Tags is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM