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));
}
}
Related
I am trying to make it when a user is tagged (eg. MyNameJeff#0001), the bot will automatically respond with you're not allowed to mention them and delete their message.
I tried searching for an event that could handle this but haven't found anything useful so far.
You can check if the collection message.mentions.users includes the guild owner's ID with the has() method. A simple if (message.mentions.users.has(message.guild.ownerId)) will work for you:
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
if (message.mentions.users.has(message.guild.ownerId)) {
message.channel.send('You are not allowed to mention the owner!');
message.delete();
}
});
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
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
I'm making a bot where if you do d!move, the bot will move the channel where the message was sent in under a category via ID. I also want to make it so that whoever does the command has permissions such as MANAGE_CHANNELS, which I've already added. The problem is that when I want to confirm whoever created that channel is the person that activated the command, the bot says yes. I did this on an alt account, where I made the channel and my alt was the one initializing it, and the bot said "success!" I also wanted to make it so if someone else made the channel, and when I did it, it would work because I made the bot know my ID.
I've researched Google and found nothing.
I've tried using a function with fetchAuditlog but get anywhere.
if(!message.channel.client.user.id == message.author || !message.author.id == `329023088517971969`) return message.channel.send("You don't own this channel!")
else message.channel.send("success!");
message.channel.setParent(`576976244575305759`);
I expect the bot to be able to check if the author created the channel, and lead to You don't own this channel if they don't own it. But if they do then the bot moves the channel.
The actual result is the bot moving the channel anyway regardless if they own the channel or not.
As #André has pointed out, channel.client represents the client itself, not the user who created the channel. Also, the last line in your code is not part of the else statement, so that's why it's run regardless of the conditions you defined.
To reach a solution, you can make use of the guild's audit logs. You can search for entries where the user is the message author and a channel was created. Then, all you have left is to check if one of those entries is for the current channel, and run the rest of your code if so.
Sample:
message.guild.fetchAuditLogs({
user: message.author,
type: 'CHANNEL_CREATE'
}).then(logs => {
if (!logs.entries.find(e => e.target && e.target.id === message.channel.id)) return message.channel.send('You don\'t own this channel.');
else {
// rest of code
}
}).catch(err => console.error(err));
When you go <anything>.client.user it will return the bot client.
If you want to see who created the channel you would have to check the Audit Logs or save it internally.
I've checked the documents. Here's what it says for .client about the
channel. It says the person that initialized the channel, or the
person that created it.
On documentation I see this:
The client that instantiated the Channel
instantiated is different of initialized
So, the title briefly explains what I want.
When a member joins my Discord server, I want the bot to assign them the User role automatically. Before that happens, I want to verify if the bot has permission to manage roles and so far I have the following in my code.
/** Event will trigger when a user joins the server **/
bot.on("guildMemberAdd", function(member) {
if (!bot.user.hasPermission("MANAGE_ROLES")) {
var embed2 = new discord.RichEmbed()
.setTitle("Error")
.setDescription("The bot doesn't have the appropriate permissions to assign new members roles automatically.\nTo resolve this problem, go to **Server Settings** and then navigate to the **Roles** option. Next, click on **Bots**, and then click on the slider next to **Manage Roles** to activate it.")
.setColor("#3937a5")
bot.channels.get("466878539980079105").send(embed2);
} else {
member.addRole(member.guild.roles.find("name", "User"));
}
});
If the bot doesn't have the permissions, it will get the attention of staff by posting an alert to the #alert channel.
However, I attempt to join with a new Discord account, it reports in the console that hasPermission isn't a function, and then stops.
Would there be any suggestions on how to resolve this code?
On Discord.js we have users and members. Users is a user of Discord. Member is a member of a Discord Server. The user object doesn't have information about roles, nicknames, permissions. Members do, because they are related to that server.
So if you want to grab the bot as the member of the guild you need to use something like this:
<guild>.me
This will return the member object of the bot from the selected guild.
When a member is added to the guild you get the GuildMember object, and with that you also get the guild. So you can use this:
member.guild.me
And finally to check the bot has permissions:
if(member.guild.me.hasPermission("MANAGE_ROLES"))
Note:
hasPermission() and hasPermissions() will be deprecated in DiscordJS v13 and replaced with permissions.has()
e.g : member.permissions.has(Permissions.FLAGS.SEND_MESSAGES);
the error message is correct, user has no function called hasPermission. but the role property does.
https://discord.js.org/#/docs/main/stable/class/Role?scrollTo=hasPermission