[英]Discord Python bot that deletes messages in a certain channel which are not commands
[英]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.