Skip to content
← Back to Community
How to make my bot DM
Profile icon
dj741

So I am making a discord bot with discord.js and I need to find out how to make it DM a user if any way please say bellow

Voters
Profile icon
TKRAUL
Profile icon
dj741
Comments
hotnewtop
Profile icon
cookieisgud

Yes you can! If you have their ID, it is very easy.

const user = client.users.cache.fetch("<USER ID>"); user.send("Hello, world!");

If you don't have the user id, don't worry, you can still do it by username, as long as they are in a server the bot is in too.

const user = client.users.cache.find(user => user.username == "<USER NAME>"); user.send("Hello, worlD!");

please mark as correct answer, thanks!

Profile icon
alanchen12

@cookieisgud Um, I'm not sure the first one will work because you weren't fetching from the cache.

Profile icon
AloegelhiPlaysR

This wont work for some people. Reason: Client is not defined in all of them. My "client" is defined as "Lynx" @cookieisgud

Profile icon
cookieisgud

well most people define it as client lol @AloegelhiPlaysR

Profile icon
cookieisgud

ther fixed, In my use case, it was guaranteed that the user was already in one of the bot's servers so that I didn't have to fetch from the cache. Thanks for pointing it out! @alanchen12

Profile icon
alanchen12

You can use the #send method as seen in the docs

If you want to send to the author of the message, use the #send method or

message.author.send()

If you want to send to a user by id, fetch a user and send using the .send() function. Make sure they are in the cache. To fetch a user, use the #fetch method.
Note: change client to what you defined client as.
Code:

const user = <client>.users.cache.fetch(id); user.send();