0

So i want that the bot @ the user he is talking to because ${member} (i saw that on youtube) doesnt work and so i want to ask what i have to write so that he writes "Hello @(the users name)..." remember please he is writing that as a dm.

const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();

client.on('ready', () => {
    console.log('This Bot is online!');
})

client.login(token);

client.on('guildMemberAdd', member => {
    member.send('Hello ${member}, welcome to the PotatoHost Server!');
});
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
Nico105
  • 188
  • 1
  • 12

2 Answers2

1

The problem isn`t with the member, is with the client.login(), it should always be at the end if the code!

I hope this will help you. have a great day!

Edit:Also, some members have locked dm's, so you should use a try-catch function, and if you got an err send the welcome message in the chat.

A try-catch function works like that:

try{
member.send("message here")
}catch(error){
member.guild.channels.get("here the Id of the channel you want to send the welcome message in").send("message here")
}

if you don't like the idea of sending the message in a channel in your server just put instead:

console.log(error)
AptaLinny
  • 36
  • 2
  • So everything worked now except when i disabled the dm option on my test account and joinend my test server the bot didnt send the welcome message in the channel – Nico105 Apr 14 '20 at 15:00
  • This Bot is online! (node:14224) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send messages to this user at RequestHandler.execute (C:\Users\nicos\OneDrive\Documents\Discord Bots\PotatoHost Bot\node_modules\discord.js\src\rest\RequestHandler.js:170:25) at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:14224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). – Nico105 Apr 14 '20 at 15:02
  • To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:14224) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. – Nico105 Apr 14 '20 at 15:02
1

I had the same problem then I started, this should help you solve the problem:

client.on("guildMemberAdd", async member => {
const dmErr = false;
try {
await member.send()
} catch (error) {
dmErr = true;
} if (dmErr === true) {
member.guild.channels.get("Id of the channel here").send()
}
}); 
AptaLinny
  • 36
  • 2
  • (node:14224) UnhandledPromiseRejectionWarning: TypeError: Assignment to constant variable. at Client. (C:\Users\nicos\OneDrive\Documents\Discord Bots\PotatoHost Bot\index.js:18:15) at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:14224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag ` – Nico105 Apr 14 '20 at 19:55
  • --unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:14224) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. – Nico105 Apr 14 '20 at 19:55
  • give me a photo of the index.js or the entire code, I can't solve this if I can't see the code – AptaLinny Apr 15 '20 at 04:29
  • Do you have discord or something because i can't send a photo here and i cant write the whole code in less than 5 comments – Nico105 Apr 15 '20 at 10:53
  • dm me here: AptaLinny#2327 – AptaLinny Apr 15 '20 at 11:10