Python discord bot 用于检查所有用户的特定角色,该角色也具有特定角色

时间:2021-05-05 04:05:53

标签: python discord discord.py

好的,所以我正在用 python 编写我的第一个 Discord 机器人,但我在查找如何准确地执行此操作时遇到了麻烦。我将结合 python、visual basic 命令(因为我比 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

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

2 个答案:

答案 0 :(得分:2)

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

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

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

在此处查看文档:{​​{3}}

答案 1 :(得分:0)

实际上您可以轻松完成。因此,我将逐步添加代码并构建命令。

让我们开始吧。

步骤 1:创建具有一些权限限制的函数。

此处将定义函数并限制其使用。现在我们将创建一个只有 Administrator 的权限。您可以随时更改它并添加任何权限,例如:manage_rolesmanage_channels 或任何不和谐权限。

您需要先将此命令与其他导入命令一起编写。

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

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

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

其次检查角色“Absent With Notice”。如果他们有这个角色,那么代码会传递给另一个用户并且什么都不做。

第三,它检查用户是否具有“Team1” 角色。如果他们确实有它,那么如果里面有内容并且没有 else 条件。因此,如果成员具有 “Team1” 角色,则它会检查另一个角色“活动”。如果用户具有此角色,脚本会将其添加到我们刚刚创建的 memberlist 中。这里也没有 else 条件。如果需要,您可以自己添加它们。只需将中间的 if 更改为 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}")

这对我有用,因为我自己尝试过。下面是一些屏幕截图,以证明它确实有效,并向您展示了它的实际工作原理。

How the command works.


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

谢谢! :D