Don't delete message if user has admin? - javascript

Basically I have a custom bot that deletes invite link when they're sent in the chat by users.
However if a user who has administrator permissions sends an invite link, I want the bot to ignore it and not the delete message.
Here is what I have:
client.on("message", async message => {
if (message.content.includes('discord.gg/' || 'discordapp.com/invite/')) {
message.delete()
});

Try this
client.on("message", async message => {
if (message.content.includes('discord.gg') || message.content.includes('discordapp.com/invite/')) {
if(!message.member.hasPermission("ADMINISTRATOR")){
message.delete();
}
});
Note: On future Discord.js updates member.hasPermission() might be replaced with member.permissions.has()
But till now permissions.has() cheates an error for me. So I suggest using hasPermissions()

Inside the if statement, include a line to check if the user has admin permission. Eg:if(message.member.permissions.has('ADMINISTRATOR')) return; would check if the user has an admin permission, and would return/not delete the message if he does. Or you can do the other way around, deleting the message if the user does not have the perms.
Eg:
if(!message.member.permissions.has('ADMINISTRATOR')){
message.delete();
}
Note: These statements has to inside the if statements that check if the message has an invite link.

if (message.content.includes('discord.gg/' || 'discordapp.com/invite/')) {
message.delete()
As of now, this just deletes every message that comes in.
So, what you want to do is to check if the author of the message has the permissions.
You can do this with:
message.member.hasPermission(//permissions here)
So, to add that to your code, you can do:
if (message.content.includes('discord.gg/' || 'discordapp.com/invite/')) {
if(!message.member.hasPermission("ADMINISTRATOR")){
)message.delete()
}
If they don't have Administrator, it's auto deleted.
You Can Read More Here:
https://discordjs.guide/popular-topics/permissions.html

Related

Discord.js || Can bot track ban list?

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

How can I check if someone is replying to a message from someone specific

I am using Discord.js v13
My goal is in a message event, like this:
client.on('messageCreate', async message => {
//Code comes here
})
to check if anyone, no matter who, is replying to any message from a specific person
Using the reference field, you can get whether the message is a reply.
For example
client.on('messageCreate', async message => {
if (message.reference.messageId) { // the message is a reply to another message
let referenceMessage = message.channel.messages.cache.get(message.reference.messageId);
if (referenceMessage.author.id == MY_USER_SNOWFLAKE) {
// The reply was to the specific user!
}
}
})
Keep in mind that the reference message must be cached. If it isn't cached, you can use message.fetchReference().

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.

Check for discord link and delete

Im trying to code my discord bot to prevent advertising other discords.
I've read a lot on this site, but can't find the solution.
I want the bot to search for discord invites in a message, and if this link is not posted by a member that has the kick permission, then it should delete the invite.
if (message.content.includes('discord.gg/'||'discordapp.com/invite/')) { //if it contains an invite link
if(!message.member.hasPermission("KICK_MEMBERS")) {
message.delete() //delete the message
.then(message.member.send(ms.INVITELINK));
}}
Includes only accepts one condition. You have to use separate include statements if you're using || for each of the strings you're trying to catch. If you want to check for multiple conditions try using some()
if (message.content.includes('discord.gg/') || message.content.includes('discordapp.com/invite/')) { //if it contains an invite link
if (!message.member.hasPermission("KICK_MEMBERS")) {
message.delete() //delete the message
.then(message.member.send(ms.INVITELINK));
}
}

Sending private messages to user

I'm using the discord.js library and node.js to create a Discord bot that facilitates poker. It is functional except the hands are shown to everyone, and I need to loop through the players and send them a DM with their hand.
bot.on("message", message => {
message.channel.sendMessage("string");
});
This is the code that sends a message to the channel when any user sends a message. I need the bot to reply in a private channel; I've seen dmChannel, but I do not understand how to use it. I have the username of the member that I want to send a message to.
An example would be appreciated.
Edit:
After looking around for a user object, I found that I can get all of the users using the .users property of the client (bot). I will try using the user.sendMessage("string") method soon.
In order for a bot to send a message, you need <client>.send() , the client is where the bot will send a message to(A channel, everywhere in the server, or a PM). Since you want the bot to PM a certain user, you can use message.author as your client. (you can replace author as mentioned user in a message or something, etc)
Hence, the answer is: message.author.send("Your message here.")
I recommend looking up the Discord.js documentation about a certain object's properties whenever you get stuck, you might find a particular function that may serve as your solution.
To send a message to a user you first need to obtain a User instance.
Obtaining a User instance
use the message.author property of a message the user sent .
call client.users.fetch with the user's id
Once you got a user instance you can send the message with .send
Examples
client.on('message', (msg) => {
if (!msg.author.bot) msg.author.send('ok ' + msg.author.id);
});
client.users.fetch('487904509670337509', false).then((user) => {
user.send('hello world');
});
The above answers work fine too, but I've found you can usually just use message.author.send("blah blah") instead of message.author.sendMessage("blah blah").
-EDIT- : This is because the sendMessage command is outdated as of v12 in Discord Js
.send tends to work better for me in general than .sendMessage, which sometimes runs into problems.
Hope that helps a teeny bit!
If your looking to type up the message and then your bot will send it to the user, here is the code. It also has a role restriction on it :)
case 'dm':
mentiondm = message.mentions.users.first();
message.channel.bulkDelete(1);
if (!message.member.roles.cache.some(role => role.name === "Owner")) return message.channel.send('Beep Boing: This command is way too powerful for you to use!');
if (mentiondm == null) return message.reply('Beep Boing: No user to send message to!');
mentionMessage = message.content.slice(3);
mentiondm.send(mentionMessage);
console.log('Message Sent!')
break;
Just an FYI for v12 it's now
client.users.fetch('487904509670337509', false).then((user) => {
user.send('heloo');
});
where '487904509670337509' is an id number.
If you want to send the message to a predetermined person, such as yourself, you can set it so that the channel it would be messaging to would be their (your) own userID. So for instance, if you're using the discord bot tutorials from Digital Trends, where it says "to: ", you would continue with their (or your) userID. For instance, with how that specific code is set up, you could do "to: userID", and it would message that person. Or, if you want the bot to message you any time someone uses a specific command, you could do "to: '12345678890'", the numbers being a filler for the actual userID. Hope this helps!
This is pretty simple here is an example
Add your command code here like:
if (cmd === `!dm`) {
let dUser =
message.guild.member(message.mentions.users.first()) ||
message.guild.members.get(args[0]);
if (!dUser) return message.channel.send("Can't find user!");
if (!message.member.hasPermission('ADMINISTRATOR'))
return message.reply("You can't you that command!");
let dMessage = args.join(' ').slice(22);
if (dMessage.length < 1) return message.reply('You must supply a message!');
dUser.send(`${dUser} A moderator from WP Coding Club sent you: ${dMessage}`);
message.author.send(
`${message.author} You have sent your message to ${dUser}`
);
}
Make the code say if (msg.content === ('trigger') msg.author.send('text')}

Categories