繁体   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