如何检查消息作者的角色 + 如何在 Discord.py 上 DM 特定角色?

时间:2021-02-04 14:01:22

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

所以我正在制作一个 Discord 机器人,当有人违反规则时,它会发送 DM 类型的角色。这是我的第一个不和谐机器人,而且真的只是在 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 是我创建的一个类,其中包含检查是否有人违反规则的功能。你推荐的任何东西都会很棒,而且很有帮助。

2 个答案:

答案 0 :(得分:0)

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

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

参考:

答案 1 :(得分:0)

看起来我找到了如何使用 discord.py DM 人。您只需使用 API 文档中的 send function。希望这可以帮助其他需要此功能的人:D