Python 从discord.py中的discord服务器中获取随机用户
我想让机器人从discord服务器中随机选择2个用户,但每次我这样做时,机器人只选择他,而不选择服务器中的任何其他人,下面是我的代码Python 从discord.py中的discord服务器中获取随机用户,python,discord.py,Python,Discord.py,我想让机器人从discord服务器中随机选择2个用户,但每次我这样做时,机器人只选择他,而不选择服务器中的任何其他人,下面是我的代码 @bot.command() 异步def随机(ctx): users=ctx.guild.members user1=选择(用户) user2=选择(用户) 等待ctx.send(f“{user1}a bolossé{user2}”) 谢谢你以后的回答 非常确定您没有启用意向。成员 intents=discord.intents.default() intent
@bot.command()
异步def随机(ctx):
users=ctx.guild.members
user1=选择(用户)
user2=选择(用户)
等待ctx.send(f“{user1}a bolossé{user2}”)
谢谢你以后的回答 非常确定您没有启用
意向。成员
intents=discord.intents.default()
intents.members=True
bot=commands.bot(…,intents=intents)
请记住在中启用它们
另外,要获得两个随机用户,您可以使用random.sample
,这样就不会得到重复的用户
user1,user2=random.sample(用户,2)
参考:
意图。members=True
。这允许您的机器人访问不同公会的成员。另外,确保在代码中放入intents=discord.intents.all()
,以激活所有意图。如果你得到一个关于这个的控制台错误,转到Bot部分,转到上面说的特权网关意图并激活它们的地方
因此,您的代码应该如下所示:
bot = commands.Bot(command_prefix = "prefix",intents = intents)
intents = discord.intents.all()
intents.members = True
@bot.command()
async def random(ctx):
users = ctx.guild.members
user1 = choice(users)
user2 = choice(users)
await ctx.send(f"{user1} a bolossé {user2}")
这不是随机的。选择?请调试代码,你会得到一个名称错误