Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

javascript - Discord.js can't use "send" function to DM a person because it is undefined?

I'm new from discord.js and I had recently meet some problem here. I can't use "send" function to DM a person because it is undefined. This is how my thing looks like

const user = client.users.cache.get('KillerHK#3150');
 user.send('hello there');

It shows up TypeError: Cannot read property 'send' of undefined. Yes I have that

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

event handler thingy

question from:https://stackoverflow.com/questions/65641804/discord-js-cant-use-send-function-to-dm-a-person-because-it-is-undefined

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

2 things you did wrong (or that im aware of)

1 (MAIN ISSUE): When you tried to .get a user, it can only accept an ID, not the username & tag. Try using

client.users.cache.get('your id, for example 2983190921391').

2: You have to use await because it takes time fetching the user. Put it in an async function and use

let user = await client.users.cache.get('your user id again')


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...