簡體   English   中英

如果作者沒有特定角色,如何制作一個不和諧的bot來刪除帶有附件的郵件

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

基本上與問題相同。 我想創建一個機器人,如果郵件的作者在我的服務器中沒有機器人或受信任的角色,它將刪除帶有附件的郵件。

目前我有這個:

 **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")**

不知道您的完整代碼,這段代碼似乎接近實現你想要做什么,它只是第二if條件的格式不正確,造成問題。

假設您將*roleID*roleID替換為各自的漫游器和受信任角色ID,則*roleID*始終返回True而不管您在字符串中輸入了什么,因為它不是空的。 這時無論是正確的or幾乎被忽略的,並且if語句將始終返回True ,刪除通過這段代碼的所有消息。

還要注意的另一件事是,您應該對兩個roleID檢查都使用列表理解,而不是僅僅使用一次,因此您必須將其保存到變量中。

嘗試看看是否以下工作:

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")

希望我能幫上忙!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM