首页 > 解决方案 > 从选择中指定要提及的人数

问题描述

我正在尝试制定一个命令,在语音频道中选择多个人并提及他们。

目前,我已经做到了,所以提到了一个人:

@commands.command()
async def hns(self, message):
    await message.channel.send(choice(tuple(member.mention for member in message.author.voice.channel.members if not member.bot)))
    await message.channel.send("You have been chosen to seek.")

我一直在尝试在命令中添加一个参数,例如arg,但我不确定从那里去哪里。

可能的解决方案:

我不习惯使用 message 参数,所以我不确定,但也许使用它ctx会允许更多?

有任何想法吗?

标签: pythondiscord.pydiscord.py-rewrite

解决方案


使用装饰器的命令中的第一个参数始终是上下文对象,它们只是ctx按约定调用。这意味着您的messagearg 实际上是上下文对象。

另一种可能的解决方案可能是改组成员列表,然后选择其中的 x 个,如下所示:

@commands.command()
async def hns(self, ctx, amount: int):
    members = [m.mention for m in ctx.author.voice.channel.members if not m.bot]
    random.shuffle(members) # shuffles it in place, i.e. doesn't return the list
    selected = members[:amount]
    await ctx.send(f"{', '.join(selected)}, you've been chosen to seek!")

该命令的用法是:
!hns 3
随机选择 3 个用户。这将避免必须使用硬编码值,尽管如果您愿意,可以这样做。


参考:


推荐阅读