简体   繁体   中英

Don't delete message if user has admin?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM