如何使用代码区分普通用户和机器人(discord.py)?

huangapple go评论73阅读模式
英文:

how to tell the difference between a normal user and a bot using code (discord.py)

问题

(discord.py) 我正在为一个特定服务器的机器人制作一个命令这个命令将创建一个统计频道显示成员数量机器人数量以及服务器上的所有人包括成员和机器人的总数
目前我有一点代码但我需要帮助处理机器人
```python
@client.tree.command(name='stats', description='为服务器设置统计信息(仅限于 sheepie 或 airone)')
async def stats(interaction: discord.Interaction):
  members = len(client.users)

我什么都没尝试,因为我不知道该如何做这个。
我只需要有人告诉我如何区分机器人和普通用户/人员即可。


<details>
<summary>英文:</summary>

(discord.py) I am making a command for a bot for one specific server. This command will make a statistics channel with the number of members, the number of bots, and the total amount of everybody including members and bots on the server.
I have a little bit of code for now, but I need help with the bots:

@client.tree.command(name='stats', description='set up stats for the server (for sheepie or airone only)')
async def stats(interaction: discord.Interaction):
members = len(client.users)

I have tried nothing because I have no idea how to do this.
All I need is someone to tell me how to distinguish the bots from the normal users/people.

</details>


# 答案1
**得分**: 1

以下是翻译好的内容:

正如评论中有人提到的那样,*`discord.User`类提供了`bot`属性来识别机器人用户。*

至于代码部分,`len(client.users)`将返回您的机器人在**每个**服务器中可见的**所有**用户的计数,包括机器人。[API 参考](https://discordpy.readthedocs.io/en/stable/api.html?highlight=client%20users#discord.Client.users)

我建议在调用命令的服务器中分别计算机器人和用户的数量,可以使用类似以下代码:
```python
@client.command(name='stats', description='为服务器设置统计信息(仅适用于 sheepie 或 airone)')
async def stats(ctx):
    # 创建空列表
    members = []
    bots = []
    # 循环检查用户是否是机器人,并将其添加到相应的列表中
    async for user in ctx.guild.fetch_members(limit=None):
        if user.bot:
            bots.append(user)
        else:
            members.append(user)
    # 获取成员和机器人的数量
    memberNum = len(members)
    botNum = len(bots)

如果您不希望将成员和机器人存储在表格中,只想获取数量,可以使用以下代码:

@client.command(name='stats', description='为服务器设置统计信息(仅适用于 sheepie 或 airone)')
async def stats(ctx):
    # 创建计数器
    membersCount = 0
    botsCount = 0
    # 根据用户是否是机器人递增计数器的值
    async for user in ctx.guild.fetch_members(limit=None):
        if user.bot:
            botsCount += 1
        else:
            membersCount += 1

请注意,如果您的服务器中有很多用户,这可能会有点慢,因为它需要遍历服务器中的每个用户。

此外,您可以使用guild.members而不是guild.fetch_members()。区别在于,guild.fetch_members()是一个API调用,而guild.members是一个属性。如果您的服务器中成员过多,这可能会解决慢的问题。

API 参考

希望这有助于您创建您的 Discord 机器人!

英文:

As someone in the comments said, the discord.User class provides the attribute bot to identify bot users.

As for the code, len(client.users) will return the count of all the users your bot can see in every server it is and it includes bots. API Reference

I'm going to recommend something like this for counting bots and users separately in the guild the command was called from:

@client.command(name=&#39;stats&#39;, description=&#39;set up stats for the server (for sheepie or airone only)&#39;)
async def stats(ctx):
    # Create empty lists
    members = []
    bots = []
    # For loop that checks if the user is a bot and appends it to the right list
    async for user in ctx.guild.fetch_members(limit=None):
        if user.bot:
            bots.append(user)
        else:
            members.append(user)
    # Get the number of members and bots
    memberNum = len(members)
    botNum = len(bots)

And if you don't want the members and bots to be stored in tables and just want the number:

@client.command(name=&#39;stats&#39;, description=&#39;set up stats for the server (for sheepie or airone only)&#39;)
async def stats(ctx):
    # Create the counters
    membersCount = 0
    botsCount = 0
    # For loop that increments the counters based on if the user is a bot or not
    async for user in ctx.guild.fetch_members(limit=None):
        if user.bot:
            botsCount += 1
        else:
            membersCount += 1

Note that if you have many users in your server it can be a little slow because it's going through every user in the guild.

Additionally, you can use guild.members instead of guild.fetch_members(). The difference is that guild.fetch_members() is an API call and guild.members is a property. This could fix the slowness problem if there is too much members in your server.

API Reference

Hope I helped you in making your discord bot!

huangapple
  • 本文由 发表于 2023年5月30日 09:19:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定