【问题标题】:How can I check the role of a message author + How do I DM specific roles on Discord.py?如何检查消息作者的角色 + 如何在 Discord.py 上 DM 特定角色?
【发布时间】:2021-02-05 01:08:38
【问题描述】:

所以我正在制作一个 Discord 机器人,当有人违反规则时,它会 DM 角色类型的角色。这是我的第一个 discord 机器人,并且真的刚刚离开了 GitHub 上的示例。我似乎找不到如何检查角色或 DM 每个角色。我发现了类似的东西

if message.author.server_privileges("kick_members"):

还有检查角色的东西。虽然它总是说“成员没有属性'server_privileges”或类似的东西。无论如何,我希望你们能帮助我。这些是我需要完成的最后一件事。这是我的代码:

class MyClient(discord.Client):
   async def on_ready(self):
       print("")
       print('Logged on as', self.user)

   async def on_message(self, message):
       reasons = []

       # don't respond to ourselves
       if message.author == self.user:
           return

       if CheckBot.susCheck(self, message.content, susWords):
           reasons.append("Being sus")
           # DM staff, admins, and headstaff
           report = "Caught " + str(message.author) + " being sus with the following message: \n" + message.content
           print("")
           print("####################-END-####################")
           print("")
           print(report)

       if CheckBot.badCheck(self, message.content, badWords):
           reasons.append("Saying bad words")
           # DM staff, admins and headstaff
           report = "Caught " + str(message.author) + " saying bad words with the following message: \n" + message.content
           print("")
           print("####################-END-####################")
           print("")
           print(report)

       if #Check if message author has certain role:
           if CheckBot.spamCheck(self, message.content, 100000000000000000):
               reasons.append("Spam")
               # DM staff, admins, and headstaff
               report = "Caught " + str(message.author) + " spamming with the following message: \n" + message.content
               print("")
               print("####################-END-####################")
               print("")
               print(report)
       else:
           if CheckBot.spamCheck(self, message.content, 55):
               reasons.append("Spam")
               # DM staff, admins, owner, and headstaff
               report = "Caught " + str(message.author) + "spamming with the following message: \n" + message.content
               print("")
               print("####################-END-####################")
               print("")
               print(report)

CheckBot 是我创建的一个类,其中包含检查某人是否违反规则的功能。你推荐的任何东西都会很棒而且很有帮助。

【问题讨论】:

    标签: python python-3.x discord.py


    【解决方案1】:

    您可以使用Guild.get_role 获取discord.Role 实例,并将其与in 关键字和Member.roles 进行比较

    role = message.guild.get_role(ROLE_ID_HERE)
    if role in message.author.roles:
        # Has the role, do something here
    

    参考:

    【讨论】:

    • 谢谢,虽然我应该在服务器上执行 "\@ROLE_NAME" 时输入角色 ID 吗?因为现在即使是角色正确的人也会收到 115 个垃圾邮件限制(对于非 mod 将其更新为 115 个字符)。这也是我的 if 语句:if role3 in message.author.roles or role4 in message.author.roles or role5 in message.author.roles:
    • 不,只有 ID 本身是一个没有<&> 的整数
    • 成功了,非常感谢。现在我只需要弄清楚如何 DM 人,然后我就完成了!再次感谢您。
    【解决方案2】:

    看起来我找到了如何使用 discord.py 向人们发送消息。您只需使用 API 文档中的 send function。希望这可以帮助其他需要这个的人:D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-16
      • 2021-08-15
      • 2021-07-20
      • 2020-08-06
      • 1970-01-01
      • 2018-08-27
      • 2021-08-05
      • 1970-01-01
      相关资源
      最近更新 更多