简体   繁体   中英

Python discord bot to check all users for a specific role that also HAVE a specific role

OK, so I'm writing my first Discord bot in python, and I'm having trouble looking up how to do this exactly. I'm going to write what I need it to do in a combination of python, visual basic commands ( because I know those better than python ) and simple English, so as to give a better idea of what I'm trying to accomplish.

Just to note, this python bot is running on my PC, not a hosted. And it is only in one (my) discord server.

@client.command()
async def team1(ctx):
     await ctx.send('Sending Inactivity Report For Team1')

#Here's where I combine languages just to show what I'm trying to do

users = (Every member in server)

For each users
     if user has joined in 21 days or less then
          goto skip
     end if

     if user has role "Absent With Notice" then
          goto skip
     end if
     if user has role "Team1" then
          if user does NOT have role "Active" then
               await ctx.send('User is inactive and should be warned.')
          end if
     end if
skip:
next users

Any ideas on where to begin? Any help would be awesome. :)

You can iterate through all the members of a guild, fetch a role and then check if that role is in member's rolemenu like this:

role = discord.utils.get(ctx.guild, id = your_role_id)

for member in ctx.guild.members:
    if role in member.roles:
        #do things

Check out the documentation here: https://discordpy.readthedocs.io/en/latest/api.html

You can actually do it easily. So, I will be adding the code step by step and building up the command.

Let's begin.

Step 1: Create the function with some permission restrictions.

Here will define the function and restrict its use from others. For now we will make is an Administrator only permission. You can always change it and add any permissions like: manage_roles or manage_channels or any permissions of discord.

You need to write this command first with the other import commands.

from discord.ext import commands

(Ignore this if you have already added it in your code.)

Here we have defined it with additional text message to say it is starting:

@ndcr.command()
@commands.has_permissions(administrator=True)
async def rolecheck(ctx):
    await ctx.channel.send("Acquiring Inactivity Report!")

Now as we have defined this let's move to next step.

Step 2: Define variables for roles that are required.

According to you demand I assume you have the roles already so I defined those roles on the basis of their names. Well the names can be changes any time so change then according to the Server you are using.

Here are the defined role:

joined_in_21_days_or_before_role = discord.utils.get(ctx.guild.roles, name='ADD THE NAME OF THIS ROLE')
absent_with_notice_role = discord.utils.get(ctx.guild.roles, name='Absent With Notice')
team_role = discord.utils.get(ctx.guild.roles, name="Team1")
active_role = discord.utils.get(ctx.guild.roles, name="Active")

In the first one I wasn't sure what was the name so just add it yourself.

Step 3: Create and Empty list to add members for display.

This list will have the members who do not have the "Active" role. Here it is:

memberlist = []

Step 4: Creating a loop with content inside it.

As per you demand I have created a loop that iterate through every member and find the desired output.

First it checks if the member have the role of "Joining 21 days or Less" . If they have it, the code simply passes and moves to next member.

Second it checks for the role "Absent With Notice" . If they have this role then the code passes to another user and does nothing.

Third it checks if the user has "Team1" role. If they do have it then if does the content inside and there is no else condition. So if the member has "Team1" role then it checks for another role "Active". If the user has this role, the script adds them to the memberlist we have just created. Here also there is no else condition. You can add them yourself if you want. Just change the middle if s to elif and you'll be good.

Here is the code:

for person in ctx.guild.members:
    if joined_in_21_days_or_before_role in person.roles:
        pass
    
    if absent_with_notice_role in person.roles:
        pass

    if team_role in person.roles:
        if active_role not in person.roles:
            memberlist.append(f"{person.name}")
    
await ctx.channel.send(f"Succesfully Completed the Process. \nThe following is the list of Inactive Members. Such a Disgrace. >:( \n \n{memberlist}")

This works for me as I have tried it myself. Here are some screenshots to prove that it really works and show you how it actually works.

命令如何工作。


This was the simplest actual way for what do you want to do with your code. Hope it helps and if you encounter any errors, please tell them in the comments. :)

Thank You: :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM