简体   繁体   中英

How to make a discord bot which deletes messages with attachments if the author doesn't have certain roles

Basically as in the question. I want to create a bot which will delete a message with an attachment if the author of the message does not have either the bot or trusted role within my server.

Currently i have this:

 **if message.attachments:
       if not "*roleID*" or "roleID" in [role.id for role in message.author.roles]:
           await client.delete_message(message)
           await client.send_message(message.channel, "Sorry mate only trusted members or bots can attach things to prevent spam")**

Without knowing your full code, this snippet seems close to achieving what you want to do, it's just the second if condition that is malformed and causing problems.

Assuming you replace *roleID* and roleID with the respective bot and trusted role IDs, *roleID* will always return True regardless of what you put into the string, because it is not empty. Whatever is right from the or is pretty much disregarded at this point and the if statement will always return True , deleting any message that passes through this piece of code.

Another thing to note is that you should use the list comprehension you've made for both roleID checks instead of just one, so you'll have to save that into a variable.

Try to see if the following works:

if len(message.attachments) > 0:
    author_roles = [role.id for role in message.author.roles]
    # replace both botRoleId and trustedRoleID with the respective IDs (as strings, because role.id is a string as well)
    if "botRoleID" not in author_roles or "trustedRoleID" not in author_roles:
        await client.delete_message(message)
        await client.send_message(message.channel, "Sorry mate only trusted members or bots can attach things to prevent spam")

I hope I could help!

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