python - 从选择中指定要提及的人数
问题描述
我正在尝试制定一个命令,在语音频道中选择多个人并提及他们。
目前,我已经做到了,所以提到了一个人:
@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
会允许更多?
有任何想法吗?
解决方案
使用装饰器的命令中的第一个参数始终是上下文对象,它们只是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 个用户。这将避免必须使用硬编码值,尽管如果您愿意,可以这样做。
参考:
推荐阅读
- android - 如何在primarySwatch小部件中为应用栏添加颜色,但颜色来自(fromARGB和fromRGBO)
- r - 在 R 中,如何让 update() 找到在调用 update() 的函数中创建的变量?
- node.js - 如何在 Linux 中的 Node.js 中并行启动 2 个程序
- performance - Perf 中的 CPU_CYCLES 是什么?
- ruby-on-rails-3 - 我如何使用 Active Query Methods 获得同样的结果?
- python - 如何加快 pandas.rolling weighted_average 或其他功能?
- flutter - ListView.builder 的构建器项目溢出
- javascript - React Material-ui 每次都耗时太长
- flutter - 在颤动中连接推送器私人频道时遇到问题
- firebase - 设置 GOOGLE_APPLICATION_CREDENTIALS