You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py中App Command无法接收List类型参数报错问题求助

Discord.py中App Command无法接收List类型参数报错问题求助

嗨,我之前也遇到过一模一样的问题,这就来帮你解决!

首先先看你贴出来的代码和报错信息:

你的原代码片段:

@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)

你尝试使用的命令:

/joined Member1 Member2 Member3 ...

遇到的错误:

unsupported type annotation typing.List[discord.member.Member]
File "*", line *, in
async def joined(interaction: discord.Interaction, me...

问题原因

Discord.py的app_commands系统并不支持直接用List[X]作为参数注解来实现多选功能,它有专门的类型来处理这种需要接收多个同类型参数的场景——那就是app_commands.Greedy。

解决方案

只需要把参数的类型注解从Optional[List[discord.Member]]改成Optional[app_commands.Greedy[discord.Member]]就可以了,同时要确保你已经导入了Greedy类型。

修改后的代码如下:

# 先确保导入了Greedy
from discord import app_commands

@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[app_commands.Greedy[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)

补充说明

Greedy类型的作用就是自动收集所有传入的同类型参数,当没有传入任何成员时,members会是None,这时候你的members = members or [interaction.user]逻辑依然能正常工作,完美兼容原来的需求。现在你再用/joined Member1 Member2 Member3这样的命令就不会报错啦!

备注:内容来源于stack exchange,提问作者Vanyla Self

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.16 06:58:16