Home > database >  Discord bot - send DM to mentioned user
Discord bot - send DM to mentioned user

Time:06-03

I want make bot that will send message to user that I mention for example I will type !hello @thomas#5555

And bot will send DM message "hello" to this user

if(message.content.startsWith(prefix   "sendHello")){
    const taggedUser = message.mentions.users.first();
    const user = client.users.cache.get(taggedUser.id);
    user.send('Hello');
    }

What's wrong? Keep getting this: "ReferenceError: client is not defined"

I defined client on the top of the code like this: "const Client = new Disocrd.Client();"

CodePudding user response:

Since you defined your client as Client, you need to use that instead of client. You could also use message.client to access it from the message object.

CodePudding user response:

First make sure you are initializing your Client correctly.

Try:

const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

With discord.js v13 you need to add the intents you want your Client to receive. The Intents.FLAGS.GUILDS intents option is necessary for your client to work properly. You can see all Intents here.

Also for your command code, if someone tagged a User you don't need to fetch the User again. Just use the taggedUser variable.

const taggedUser = message.mentions.users.first(); // get User
if (!taggedUser) return message.channel.send('Please mention a user to fetch.'); // Check to see if a User was tagged.
taggedUser.send('Hi') // send to User
  • Related