Leave message discord.js v12 - discord.js

I have a porblem. My leave message. There I have:
client.on("guildMemberAdd", member => {
const welcomeChannel = member.guild.channels.cache.find(
channel => channel.name === "welcome"
);
welcomeChannel.send(`Hosgeldin :heart: ${member}`);
});
client.on("guildMemberRemove", member => {
const welcomeChannel = member.guild.channels.cache.find(
channel => channel.name === "gelen-giden"
);
welcomeChannel.send(`Bye ${member}!`);
});
and then when somebody left then came <#id>.
I want to find his id because then I can make an link of the person who left.
It look like https://discordapp.com/users/732569703142129685 .
And then it should write 'Member' and this 'member' should be the link.
Please in v12.

Discord returns <#id> because the member that left doesn't have more guilds(servers) in common with you and he is no more in your cache. If you need his id for some deletion of rows in db you can get it as member.id

When a user leaves, he is no longer on the guild, so you can't find a channel on a server he isn't anymore in. You'll need to use client.channels.cache.find() in this case.
have a good day.

Related

Getting nicknames of users of my Discord bot

I can see the names, ids and user numbers of the servers where my bot is located, but how can I get the list of users (nicknames)?
You can make use of guild.members.fetch() in order to get all members and then use the nickname property to receive their nicknames. Finally I removed all bots with a simple filter.
const members = (await message.guild.members.fetch())
.filter((m) => !m.user.bot)
.map((m) => m.displayName);
console.log(members);
Working example as a command:
client.on("message", async (message) => {
if (message.author.bot) return;
if (message.content === "!list") {
const members = (await message.guild.members.fetch())
.filter((m) => !m.user.bot)
.map((m) => m.displayName);
console.log(members);
}
});
client.login("your-token");
Thanks to #MrMythical who suggested using displayName only. That property automatically returns the normal username when no nickname has been set for a user.
Users do not have nicknames, only guild members, if you are trying to fetch a list of server member nicknames.
You can use the code snippet from below:
const map = message.guild.members.cache.filter(c=> !c.member.user.bot).map(c=>c.displayName).join('\n');
console.log(map)
The
message.guild.members.cache.filter(c=> !c.member.user.bot)
Filters bots from the list, the
.map(c=>c.displayName).join('\n');
maps the data and only the user nicknames and joins them by paragraph breaks.
If there are any issues, please comment!

discord.js - Send image on member ban?

Hi so I would like my bot to send an image in the general chat when someone gets banned by for example, dyno, but I do not know how to do that, if anyone could help, I would appreciate it!
You'd create a listener for guildBanAdd, and send a message to the particular channel when a user is banned.
client.on('guildBanAdd', (guild, user) => {
if(guild.id === 'GuildID') {
const notificationChannel = guild.channels.cache.find(c => c.name === 'general');
notificationChannel.send('Message', {files: ['image address/url']});
}
});

How do I check what roles my bot has? [Discord.js]

I am trying to find if my bot has a specific role so it can do something else. For example:
client.user.roles.cache.find(r => r.name === 'specific role');
My error:
TypeError: Cannot read property 'cache' of undefined
Let's start of by stating that users do not have roles, therefore you have to fetch the guildMember.
Fetching the guild member is easy;
First of all, you will have to fetch the guild the user is in. For example:
var guild = client.guilds.cache.get('guild-id');
Secondly, you will have to find the user in that guild. For example:
var member = guild.members.cache.get('user-id');
Then you will be able to move forward and fetch the roles
member.roles.cache.find(r => r.name === 'role-name');
The full example:
const Discord = require('discord.js'); //Define discord.js
const client = new Discord.Client(); //Define the client
client.on('message' message => { //Event listener
//Do something here
var guild = client.guilds.cache.get(message.guild.id);
var member = guild.members.cache.get(client.user.id);
member.roles.cache.find(r => r.name === 'role-name');
});
client.login('token'); //Login
So maybe I didnt explain my problem very well but here is what I found working for me message.guild.me.roles.cache.find(r=> r.name === 'a role') thanks to everyone for helping me !!!
You need to resolve the user into a GuildMember, since users don't have roles.
To do that you need a Guild class,after that you can use the guild.member() method to convert it to a member
const guild = client.guilds.cache.get('id of the guild'),
member = guild.member(client.user.id);
member.roles.cache.find(r=> r.name === 'name')
Another issue you might run into here is, the member constant being undefined, this can be solved by using guild.members.fetch(id) or enabling the Privileged Member intent in the Discord Developer Portal

Creating an invite in a GuildCreate event

So I was trying to make my bot send me a DM to every server it joins but I keep getting the API error: Unkown Channel
My code:
bot.on("guildCreate", async guild => {
guild.channels.first().createInvite().then(inv =>
bot.users.get(ownerID).send(`I have been added to **${guild.name}** | ${inv.url}`)
)
});
Okay, this is your problem. I have made the same mistake a few months ago, here's how to fix it.
Since you are using discord.js version 11, guild.channels is indeed a Collection, which you can use .first() on. In this case, you can't do that.
Here's my workaround:
bot.on("guildCreate", async guild => {
var channel;
guild.channels.forEach(c => {
if (c.type === "text" && !channel) channel = c;
});
channel.createInvite({ maxAge: 0 }).then(inv => bot.users.get(ownerID).send(`I have been added to **${guild.name}** | https://discord.gg/${inv.code}`));
});
This basically loops through each channel and finds a valid TextChannel to create an invite.
Hope this helps.

DiscordJS Backdoor Command

I have had reports off my support server of my discord js bot of my bot being abused in multiple ways.
I want to have a way upon launch of my bot to see a list of servers as well as invite links to those servers. I do not know the id of server or anything.
The most I've managed to find out how to do is this
var server = client.guilds.get("idk the id part");
console.log('I am in the following servers:');
server.createInvite().then(invite =>
console.log(server.name + "-" + invite.url)
);
});```
In your ready event (client.on('ready', () => {}), add the following lines:
client.guilds.tap(guild => {
console.log(`Name: ${guild.name} ID: ${guild.id}`);
})
This should output
Name: Test1 ID: 00000000000000
in the console for each server.
Also, considering that making a lot of invites might clog up your bot, and is generally more of a hindrance than help to server admins, you might consider making a createinvite command:
const svID = args[0];
const guild = client.guilds.find(guild => guild.id === svID);
const channel = guild.channels.find(channel => channel.position === 1);
channel.createInvite()
.then(invite => message.reply(invite.code));
You can either wait for the bot to emit it's ready event and loop through the guild collection:
client.once('ready', () => {
// client.guilds should be ready
});
or handle each guild individually:
client.on('guildCreate', (guild) => {
// The guild the bot just joined/connected to
// Should also be emitted when the bot launches
});
Either should work, but my recommendation would be the second approach, as this will also allow you to track join events whilst the bot is running.

Resources