【问题标题】:How the I check how many members are online discord.py我如何检查有多少成员在线 discord.py
【发布时间】:2021-12-12 04:09:51
【问题描述】:

此代码试图检查有多少成员在线和离线,但它会出现一条错误消息,指出无法迭代公会。如果代码可以工作,它会发送一条消息,其中包含成员数量以及在线和离线人数,任何帮助将不胜感激。

count1=0
count2=0
  
@client.command()
async def stats(ctx):
        
        for member in ctx.guild:
          if member.status == status.online:
           count1 = +1
          else:
            count2 = +1
        embed = discord.Embed(title=ctx.guild.name+"Stats", color=0x000)
        embed.add_field(name="Member Count", value=ctx.guild.member_count)
        embed.add_field(name="Online", value="f'{coun1}' :green_circle:", inline=True)
        embed.add_field(name="Offline", value ="f'{count2}' :red_circle:", inline = True)
        await ctx.send(embed=embed)

【问题讨论】:

标签: python discord.py


【解决方案1】:

在线会员状态可以是dnd、idle或streaming:

@client.command()
async def stats(ctx):
        count = 0
        for member in ctx.guild.members:  # .members was added
          if member.status != status.oflline:
            count =+ 1
        embed = discord.Embed(title=ctx.guild.name+"Stats", color=0x000)
        embed.add_field(name="Member Count", value=ctx.guild.member_count)
        embed.add_field(name="Online", value="f'{count}' :green_circle:", inline=True)
        embed.add_field(name="Offline", value ="f'{ctx.guild.member_count - count}' :red_circle:", inline = True)
        await ctx.send(embed=embed)

【讨论】:

    【解决方案2】:

    首先我遍历了公会的所有成员,并将他们添加到online_members 列表或offline_members 列表中,并为所有用户存储昵称(这样以后如果您想制作某种类型的用户列表)。另外,我使用了discord.Status.offline 而不是online,因为Discord 还有一些其他状态类型,一些用户不会被计算在内(list of all status types)。

    我的代码(包含成员列表):

    @client.command()
    async def stats(ctx):
        online_members = []
        offline_members = []
        for member in ctx.guild.members:
            if member.status is not discord.Status.offline:
                online_members.append(member.name)
            else:
                offline_members.append(member.name)
    
        embed = discord.Embed(title=f'"{ctx.guild.name}" Stats', color=0x000)
        embed.add_field(name="Member Count", value=ctx.guild.member_count)
        embed.add_field(name="Online", value=f'{len(online_members)} :green_circle:', inline=True)
        embed.add_field(name="Offline", value =f'{len(offline_members)} :red_circle:', inline = True)
        await ctx.send(embed=embed)
    

    如果您更喜欢使用您的代码:

    @client.command()
    async def stats(ctx):
        count=0 #put these at the beginning of your command so it will reset every time, otherwise, your starting number would be the sum of all earlier commands
        for member in ctx.guild.members: #you forgot to get members
            if member.status != discord.Status.offline:
                count += 1 #you can't use "= +1" - "+= 1" is correct
        
        all_users = ctx.guild.member_count
        embed = discord.Embed(title=f'**{ctx.guild.name}** "Stats"', color=0x000)
        embed.add_field(name="Member Count", value=all_users)
        embed.add_field(name="Online", value=f'"{count}" :green_circle:', inline=True) #your "f" was in wrong place
        embed.add_field(name="Offline", value =f'"{all_users - count}" :red_circle:', inline = True)
        await ctx.send(embed=embed)
    

    记得开启intents.membersintents.presences

    【讨论】:

      猜你喜欢
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2013-10-29
      • 2022-01-05
      • 1970-01-01
      • 2021-01-13
      • 2015-05-22
      相关资源
      最近更新 更多