【发布时间】: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
我想做一个显示成员数量但不包括机器人的命令。我的代码:
@bot.command()
async def members(ctx):
await ctx.send(f"Members in {ctx.guild.name}: {ctx.guild.member_count}")
但是,这显示了机器人和成员的总数。有没有办法只显示会员?
【问题讨论】:
标签: python discord.py
你可以试试这样的,
user_count = len([x for x in ctx.guild.members if not x.bot])
【讨论】:
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")
【讨论】: