指定从选项中提及的人数

Specifying amount of people to mention from choice

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

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

@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,但我不确定从那里去哪里。

可能的解决方案:

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

有什么想法吗?

使用装饰器的命令中的第一个参数始终是上下文对象,它们只是按照惯例称为 ctx。这意味着您的 message arg 实际上是上下文对象。

另一种可能的解决方案是洗牌成员列表,然后选择 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 个用户。这将避免必须使用硬编码值,但如果您愿意,也可以这样做。


参考文献: