简体   繁体   English

Python discord 机器人检查所有用户的特定角色,该角色也具有特定角色

[英]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.好的,所以我正在 python 中编写我的第一个 Discord 机器人,但我在查找如何准确执行此操作时遇到了麻烦。 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.我将结合 python、视觉基本命令(因为我比 python 更了解这些命令)和简单的英语来编写我需要它做的事情,以便更好地了解我想要完成的工作。

Just to note, this python bot is running on my PC, not a hosted.请注意,这个 python 机器人在我的 PC 上运行,而不是托管。 And it is only in one (my) discord server.它仅在一个(我的)discord 服务器中。

@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在此处查看文档: 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.第 1 步:创建具有一些权限限制的 function。

Here will define the function and restrict its use from others.这里将定义 function 并限制其使用。 For now we will make is an Administrator only permission.现在我们将只设置Administrator权限。 You can always change it and add any permissions like: manage_roles or manage_channels or any permissions of discord.您可以随时更改它并添加任何权限,例如: manage_rolesmanage_channels或 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.第 2 步:为所需的角色定义变量。

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.第 3 步:创建并清空列表以添加成员以进行显示。

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.第 4 步:创建一个包含内容的循环。

As per you demand I have created a loop that iterate through every member and find the desired output.根据您的要求,我创建了一个循环遍历每个成员并找到所需的 output。

First it checks if the member have the role of "Joining 21 days or Less" .首先,它检查成员是否具有“加入 21 天或更少”的角色。 If they have it, the code simply passes and moves to next member.如果他们有它,代码只是传递并移动到下一个成员。

Second it checks for the role "Absent With Notice" .其次,它检查角色"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.第三,它检查用户是否具有“Team1”角色。 If they do have it then if does the content inside and there is no else condition.如果他们确实有它,那么如果里面有内容并且没有else条件。 So if the member has "Team1" role then it checks for another role "Active".因此,如果该成员具有“Team1”角色,那么它会检查另一个角色“Active”。 If the user has this role, the script adds them to the memberlist we have just created.如果用户有这个角色,脚本会将他们添加到我们memberlist创建的成员列表中。 Here also there is no else condition.这里也没有else条件。 You can add them yourself if you want.如果需要,您可以自己添加它们。 Just change the middle if s to elif and you'll be good.只需将中间if s 更改为elif就可以了。

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谢谢::D

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取具有特定角色 discord bot 的所有成员的列表 - get list of all members with specific role discord bot Python中的Discord Bot-我试图从具有特定角色的所有人中随机挑选一个人 - Discord Bot in Python - Im trying to pick a random person from all the people with a specific role Discord.py 从 1 个特定角色中删除所有用户 - Discord.py Remove all users from 1 specific role 不和谐中特定角色的用户列表(Python) - userlist of a specific role in the discord (Python) 如何在 python 中使用 discord 异步获取具有特定角色的 discord 服务器中所有用户的列表? (里面的代码) - How do I get a list of all users in my discord server with a specific role using discord async in python? (code inside) 试图编写一个禁止所有用户的 python discord 机器人程序。 机器人只禁止没有角色的人? - Trying to program a python discord bot that bans all users. The bot only bans people without a role? 使用 Discord Py 计算具有特定角色的所有成员 - Count all members with a specific role with Discord Py 我如何还可以检查成员是否具有特定角色是 if 语句? 仅当您具有特定角色时才需要 elif 执行? - how can I also check if a member has a specific role is an if statement? need the elif to only execute if you have a specific role? 每次具有特定角色的用户在任何频道发言时,如何让机器人发送消息 | Discord.py - How to have the bot send a message every time a user with a specific role speaks in any channel | Discord.py Python Discord 机器人 - 反应角色 - Python Discord Bot - Reaction Role
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM