[英]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. 不知道您的完整代码,这段代码似乎接近实现你想要做什么,它只是第二if
条件的格式不正确,造成问题。
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. 假设您将*roleID*
和roleID
替换为各自的漫游器和受信任角色ID,则*roleID*
始终返回True
而不管您在字符串中输入了什么,因为它不是空的。 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. 这时无论是正确的or
几乎被忽略的,并且if
语句将始终返回True
,删除通过这段代码的所有消息。
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. 还要注意的另一件事是,您应该对两个roleID
检查都使用列表理解,而不是仅仅使用一次,因此您必须将其保存到变量中。
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! 希望我能帮上忙!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.