Reputation: 1301
I want to make a command that shows member count but not including bots. My code:
@bot.command()
async def members(ctx):
await ctx.send(f"Members in {ctx.guild.name}: {ctx.guild.member_count}")
However this shows count of bots and members together. Is there a way I can do it show members only?
Upvotes: 1
Views: 875
Reputation: 149
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")
Upvotes: 1
Reputation: 304
You can try something like this,
user_count = len([x for x in ctx.guild.members if not x.bot])
Upvotes: 3