繁体   English   中英

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

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

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

请注意,这个 python 机器人在我的 PC 上运行,而不是托管。 它仅在一个(我的)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

关于从哪里开始的任何想法? 任何帮助都是极好的。 :)

您可以遍历公会的所有成员,获取一个角色,然后检查该角色是否在成员的角色菜单中,如下所示:

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

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

在此处查看文档: https://discordpy.readthedocs.io/en/latest/api.html

你实际上可以很容易地做到这一点。 因此,我将逐步添加代码并构建命令。

让我们开始。

第 1 步:创建具有一些权限限制的 function。

这里将定义 function 并限制其使用。 现在我们将只设置Administrator权限。 您可以随时更改它并添加任何权限,例如: manage_rolesmanage_channels或 discord 的任何权限。

您需要先编写此命令和其他导入命令。

from discord.ext import commands

(如果您已经在代码中添加了它,请忽略它。)

在这里,我们用附加的文本消息定义了它,说明它正在启动:

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

现在我们已经定义了这个,让我们进入下一步。

第 2 步:为所需的角色定义变量。

根据您的要求,我假设您已经拥有这些角色,因此我根据它们的名称定义了这些角色。 那么名称可以随时更改,因此请根据您使用的服务器进行更改。

以下是定义的角色:

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")

在第一个中,我不确定名称是什么,所以自己添加。

第 3 步:创建并清空列表以添加成员以进行显示。

此列表将包含具有“活动”角色的成员。 这里是:

memberlist = []

第 4 步:创建一个包含内容的循环。

根据您的要求,我创建了一个循环遍历每个成员并找到所需的 output。

首先,它检查成员是否具有“加入 21 天或更少”的角色。 如果他们有它,代码只是传递并移动到下一个成员。

其次,它检查角色"Absent With Notice" 如果他们具有此角色,则代码将传递给另一个用户并且什么也不做。

第三,它检查用户是否具有“Team1”角色。 如果他们确实有它,那么如果里面有内容并且没有else条件。 因此,如果该成员具有“Team1”角色,那么它会检查另一个角色“Active”。 如果用户有这个角色,脚本会将他们添加到我们memberlist创建的成员列表中。 这里也没有else条件。 如果需要,您可以自己添加它们。 只需将中间if s 更改为elif就可以了。

这是代码:

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}")

这对我有用,因为我自己尝试过。 这里有一些截图来证明它确实有效,并向您展示它是如何工作的。

命令如何工作。


这是你想用你的代码做什么的最简单的实际方法。 希望对您有所帮助,如果您遇到任何错误,请在评论中告诉他们。 :)

谢谢::D

暂无
暂无

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

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