【问题标题】:Discord.py - How to get member count not including bots?Discord.py - 如何获取不包括机器人的成员数?
【发布时间】:2022-01-13 21:50:13
【问题描述】:

我想做一个显示成员数量但不包括机器人的命令。我的代码:

@bot.command()
async def members(ctx):
    await ctx.send(f"Members in {ctx.guild.name}: {ctx.guild.member_count}")

但是,这显示了机器人和成员的总数。有没有办法只显示会员?

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    你可以试试这样的,

    user_count = len([x for x in ctx.guild.members if not x.bot])
    

    【讨论】:

      【解决方案2】:
      • 这应该可以工作
      import random
      
      import discord
      from discord.ext import commands
      
      intents = discord.Intents().all()  # intents to see the members in the server
      client = commands.Bot(command_prefix="--", intents=intents)
      
      
      # Command
      @client.command(pass_context=True)
      # to make sure that this command will only work on a discord server and not DMs
      @commands.guild_only()
      async def pickwinner(ctx):
          # What you did wrong
          #   for users in ctx.guild.members:
          #       `users` is a single item in the iterable `ctx.guild.names`
      
          # Selecting a user from the iterable (this includes user)
          # winner = random.choice(ctx.guild.members)
      
          # Selecting a user from the iterable (this does not include bots)
          # But the line below will be resource intensive if you are running the command
          #   on a server with a huge amount of members
          allMembers_NoBots = [x for x in ctx.guild.members if not x.bot]
          winner = random.choice(allMembers_NoBots)
      
          await ctx.channel.send(f"Congratulations! {winner} won the price!")
      
      
      client.run("TOKEN")
      
      

      【讨论】:

        猜你喜欢
        • 2021-04-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-02
        • 2022-08-05
        • 2019-06-15
        • 2021-10-04
        • 1970-01-01
        相关资源
        最近更新 更多