AppCommand 获取无穷无尽的参数

问题描述 投票:0回答:1

原文:

@client.tree.command()
@app_commands.describe(members='The members you want to get the joined date from; defaults to the user who uses the command')
async def joined(interaction: discord.Interaction, members: Optional[List[discord.Member]]):
    """Says when a members joined."""
    # If no member is explicitly provided then we use the command user here
    members = members or [interaction.user]
    text = ""
    for member in members:
        text += f'{member} joined {discord.utils.format_dt(member.joined_at)}\n'
    # The format_dt function formats the date time into a human readable representation in the official client
    await interaction.response.send_message(text)

members: Optional[List[discord.Member]]

我是: /加入会员1 会员2 会员3 ...

错误: 不支持的类型注释typing.List[discord.member.Member] 文件“*”,第 * 行,位于 异步 def 加入(交互:discord.Interaction,成员:可选[List[discord.Member]]): TypeError: 不支持的类型注释 Typing.List[discord.member.Member]

python python-3.x discord.py
1个回答
0
投票

不和谐斜杠命令不支持无尽的参数/可变选项。最简单的解决方案是每个命令只允许一个成员,并让用户多次运行它。

如果您坚持每个命令使用多个成员,则需要通过创建

str
参数来使用解决方法,使用户在其中放置多个提及,然后解析它们。这是使用正则表达式的示例:

import re

@client.tree.command()
@app_commands.describe(mentions='The members you want to get the joined date from; defaults to the user who uses the command')
async def joined(interaction: discord.Interaction, mentions: Optional[str]):
    if not mentions:
        members = [interaction.user]
    else:
        members = []
        for user_id in re.findall(r"\d{17,19}", mentions):
            member = interaction.guild.get_member(int(user_id))
            if member:
                members.append(member)

需要服务器成员特权意图

参考

© www.soinside.com 2019 - 2024. All rights reserved.