Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

python - How can I check the role of a message author + How do I DM specific roles on Discord.py?

So I am making a Discord bot that DMs mod-type roles when someone goes against the rules. This is my first discord bot, and have really just been going off the example on GitHub. I can't seem to find how to check a role or DM everyone in a role. I found stuff like

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

And stuff for checking the role. Although it always says "member has no attribute 'server_privileges" or something like that. Anyways, I was hoping you guys could help me. These are the last things I need to finish. Here's my code:

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: 
" + 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: 
" + 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: 
" + 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: 
" + message.content
               print("")
               print("####################-END-####################")
               print("")
               print(report)

CheckBot is a class I made, that contains the functions that check if someone is going against the rules. Anything you recommend would be great, and helpful.

question from:https://stackoverflow.com/questions/66047188/how-can-i-check-the-role-of-a-message-author-how-do-i-dm-specific-roles-on-dis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can get a discord.Role instance with Guild.get_role and compare it with the in keyword with Member.roles

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

Reference:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...