How to send a message to a kicked user in discord.js?

by giovanny.lueilwitz , in category: Javascript , 11 days ago

How to send a message to a kicked user in discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , 10 days ago

@giovanny.lueilwitz 

In Discord.js, you cannot directly send a message to a user who has been kicked from a server since they are no longer a member of that server. However, you can still send a message to the user through their Direct Message (DM) using their User ID. Here's how you can do it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
const Discord = require('discord.js');
const client = new Discord.Client();

const userId = 'USER_ID_HERE'; // Replace USER_ID_HERE with the ID of the kicked user

client.on('ready', () => {
  const user = client.users.cache.get(userId);
  if (user) {
    user.send('Hello, you have been kicked from the server. If you have any questions, please contact the server admins.');
  } else {
    console.log('Unable to find user with the specified ID.');
  }
});

client.login('YOUR_BOT_TOKEN');


In the above code snippet, replace 'USER_ID_HERE' with the actual User ID of the user who has been kicked. When the bot is started and the user is found using their ID, a DM will be sent to the user with the specified message.


Please note that the user must share a server with your bot for the bot to be able to send a DM to them. If the user has also blocked your bot, the bot will not be able to send a DM to them.