英文:
Is it possible to dynamically enable/disable arguments in a Python Discord slash command using Pycord and Discord.py based on previous user choice?
问题
我正在尝试创建一个命令,将一个频道注册到数据库中,并具有特定的功能,但我希望仅在先前选择了特定选项时才启用某些参数。
@bot.slash_command(name="my command")
async def command(ctx,
action: Option(name="action", choices=["add", "remove", "info"]),
function: Option(name="function")):
if action == "add":
# ...
elif action == "remove":
# ...
elif action == "info":
# ...
所以我希望当选择"add"或"remove"作为action时,它会禁用"function"参数或将其设置为非必需。我可以使用required=False标志,但我希望如果选择了add/remove操作,function参数将获得required=True标志。
我尝试过使用discord.AutocompleteContext来尝试禁用或启用参数,但这还没有显示出一种方法,只能更改选项。
英文:
I am trying to make an command that registers a channel into the database with a specific function, but iu wish for some arguments to only be enabeled when a specific choice was made previously.
@bot.slash_command(name="my command")
async def command(ctx,
action: Option(name="action", choices=["add", "remove", "info"]),
function:Option(name="function"):
if action == "add":
...
elif "remove":
...
elif "info"
...
So what i want is that when "add" or "remove" are chosen for action, it disables the "function" argument or makes it non-required. I could just use the required=False flag, but i hoped that if the add/remove action is chose, the function will get the required=True flag.
I have tried to play around with discord.AutocompleteContext, but this has not yet shown a way to disable or enable arguments, only to change the choices.
答案1
得分: 0
要实现禁用或根据所选操作使“function”参数变为非必需的所需功能,您可以利用Option类的required参数。默认情况下,所有选项都是必需的,但当选择“add”或“remove”操作时,您可以将required=False
设置为“function”选项。
这是一个更新后的代码示例,其中采用了这种方法:
@bot.slash_command(name="my_command")
async def command(ctx,
action: Option(str, name="action", choices=["add", "remove", "info"]),
function: Option(str, name="function", required=False)
):
if action == "add":
...
elif action == "remove":
...
elif action == "info":
...
英文:
To achieve the desired functionality of disabling or making the "function" argument non-required based on the selected action, you can make use of the required parameter of the Option class. By default, all options are required, but you can set required=False
for the "function" option when "add" or "remove" actions are chosen.
Here's an updated version of your code that incorporates this approach:
@bot.slash_command(name="my_command")
async def command(ctx,
action: Option(str, name="action", choices=["add", "remove", "info"]),
function: Option(str, name="function", required=False)
):
if action == "add":
...
elif action == "remove":
...
elif action == "info":
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论