Discord.js || Can bot track ban list? - javascript

There are a lot of to ban a user in a server, you can do it manually or with another bot. I want my bot to scan a banlist, and if someone will get banned, it will send a message "user (id) has been banned
I started writing smt but now, I had no idea what can I do to check it.
EDIT: It's working now, but it still need to find this user id.
client.on('guildBanAdd', message => {
client.user.cache.find() //idk how to define it
const channel1 = client.channels.cache.find(channel => channel.id === "158751278127895");
channel1.send('12512');
})

You don't have to fetch bans because the guildBanAdd event parses a GuildBan object,which als includes a User object.
client.on('guildBanAdd', ban => {
console.log(ban.user.username); // Username
})
message.channel won't work because there isn't a message object.
If you want to get the executor (moderator) who performed the ban, kick etc.
There is a good guide for this Working with Audit Logs

Related

how to make a bot Private message on command?

Wanted to make the bot dm a person who uses the command to get a hint based on the puzzle role they had.
did watch a YT vid that showed to dm a person based on a trigger. That didn't help as it needs too many triggers.
the bot should dm a person on what role they have
so I do know we need to use if (message.member.roles.cache.some(role => role.name === 'Puzzle 1')
Not completely sure on how to make it :'(
You can use message.author.send("YOUR MESSAGE HERE") for sending a private message to the user.
If you want to check user role by name before sending DM, you can make something like that:
if (message.member.roles.cache.some(role => role.name === 'Puzzle 1')){
message.author.send("YOUR MESSAGE HERE")
}
IMPORTANT NOTE: If a user disabled Allow direct messages from server members on privacy & safety settings, the bot can't send a message to a user. So I recommend you to use try/catch.

Greeting message only when a user gets a specific role

I have this verification system on my server, in which a new member must react to a message to obtain a role that gives permission to see the other channels. So, I wanted to make my bot send a message on a specific channel, greeting them, only when a member gets a specific role (verified)
The problem is: anyone can react in the message to get the verified role over and over again, causing the bot to spam, and I still haven't figured out a way to make this welcome message be sent only once per user
Here is my code:
client.on('guildMemberUpdate', (oldMember, newMember) => {
const channel = client.channels.cache.get('channelID');
// If the role(s) are present on the old member object but no longer on the new one (i.e role(s) were removed)
const removedRoles = oldMember.roles.cache.filter(role => !newMember.roles.cache.has(role.id));
if (removedRoles.size > 0) console.log(`The roles ${removedRoles.map(r => r.name)} were removed from ${oldMember.displayName}.`);
// If the role(s) are present on the new member object but are not on the old one (i.e role(s) were added)
const addedRoles = newMember.roles.cache.filter(role => !oldMember.roles.cache.has(role.id));
if (addedRoles.size > 0){
if (newMember.roles.cache.some(role => role.name === 'teste')) {
let embed = new Discord.MessageEmbed()
.setTitle("♡﹕welcome!")
.setDescription("lalalala")
.setColor("#FFB6C1")
.setThumbnail("https://cdn.discordapp.com/attachments/806974794461216813/817737054745526304/giffy_2.gif")
channel.send(`Welcome ${oldMember.user}`, embed)
}
console.log(`The roles ${addedRoles.map(r => r.name)} were added to ${oldMember.displayName}.`);
}
});
The code you have added to the guildMemberUpdate event listener is being used to send a welcome message when a verified role is added to a member. It is not running any checks to see if the member has already triggered a welcome message.
It sounds like when a member removes their reaction from the message, it also removes their verified role. You should look at where in your code you are removing roles, and change it so the verified role isn't removed after being added.
If this isn't possible, then consider saving a state in a file or database that can be checked before sending a welcome message.

How do I post a welcome message when my discord bot joins a server

I am new to JavaScript and I have made a small discord bot but I cannot find a way to make it have a welcome message when it joins the server. I want it to say something like "Hello my name is {bot-name}, type !help to view my commands." Does anyone know how to do this?
You can use the guildCreate event that is emitted whenever the bot joins a guild. You need to find a channel though where you can post your message.
To find a channel you can use the .find() method that returns the first channel where the given function returns a truthy value.
client.on('guildCreate', (guild) => {
const channelNames = ['general', 'welcome']
const channel = guild.channels.cache.find(ch => channelNames.includes(ch.name))
if (channel)
channel.send(
`Hello my name is ${client.user.username}, type \`!help\` to view my commands.`
)
.catch(console.error);
})

How to Make a discord bot send a message in the servers that it is in?

So I'm trying to make a bot that send a message to all the servers that it is in, but the problem is that I don't really know how to make the user chose the specific channel that he wants, and that I don't know how to make the bot send a message to the servers that the bot is in. If someone could help, or tell me about a tutorial that would be appreciated!
Problem #1: How to make a user chose a channel that that the bot sends the message in.
Problem #2: How to Make the bot send a message to all the servers that he is in.
If you would like to state all servers a bot is in, you can use a forEach method. Like so:
let servernames = "";
bot.guilds.cache.forEach(guild => {
servernames += `${guild.name}, `;
});
message.channel.send(`**${servernames.slice(0, servernames.length - 2)}**`)
However, if you are looking to send a message to every server regardless, you would still need a forEach method, simply join with the .send function. Example:
bot.guilds.cache.forEach(guild => {
guild.defaultChannel.send("hi")
});
If you want to send a message to a specific channel, you need to cache it and find using the channel ID. Example:
let channelSend = bot.channels.cache.find(channel => channel.id === 'channel id here')
channelSend.send("hi")

Problems with messageDelete in Discord js

I try to make my bot on Discord server. Want to make a function, which will copy all deleted message in text channel, but, messageDelete hear only deleted message which was writing after bot start. When I delete message which make earlier bot start, its not work.
{
client.on ("messageDelete", messageDelete =>{
let channel = client.channels.find(channel => channel.name === 'log-deleted-message')
console.log(`Deleted :${messageDelete.content}`)
channel.send(`${messageDelete.author.username} write : ${messageDelete.content}`
})
}
The above answer is now outdated.
With Discord.js V12 you can now Cache messages on the messageDelete event, You would need to enable Partials. Once this is enabled, you can fetch the message beforehand like this:
if(message.partial){
let msg = await message.fetch()
console.log(msg.content)
}
That will then log the content of the previously uncached message.
messageDelete is an event that is called when a message is deleted while the bot is on. If a message is deleted before the bot is turned on there is no way to recover it, which is why it's known as deleted. The only way to accomplish the goal that you want is to leave the bot on permanently. Read more in the docs if you want more information.

Categories