【问题标题】:How to have a role selection menu (py_cord slash commands)如何拥有角色选择菜单(pycord 斜杠命令)
【发布时间】:2021-12-04 07:26:39
【问题描述】:

您好,我正在开发一个不和谐的机器人,我正在尝试添加选择角色作为此命令的参数。它应该看起来像

这是我现在的代码

@bot.slash_command(name = "setrankroles",guild_ids = [898725831164178442])
async def setroles(ctx,
    role: Option(discord.Role,"What role are you assigning?"),
    rank: Option(str, "What rank goes with that role?", choices=valid_ranks),
    ):

它会产生这个错误

Traceback (most recent call last):
  File "/home/maximummaxx/Documents/Coding/Python/Valorant_dc/main.py", line 205, in <module>
    role: Option(discord.Role,"What role are you assigning?"),
  File "/home/maximummaxx/Documents/Coding/Python/Valorant_dc/env/lib/python3.9/site-packages/discord/commands/commands.py", line 510, in __init__
    to_assign = input_type() if isinstance(input_type, type) else input_type
TypeError: __init__() missing 3 required keyword-only arguments: 'guild', 'state', and 'data'

谢谢

【问题讨论】:

  • 图片上传不正确 - 看不到。
  • 对我来说很好。试试这个i.imgur.com/5oBNq5R.png
  • valid_ranks 设置为什么?
  • @Lloyd 这是一个字符串列表,例如 [“Bronze”,”Silver”,”Daimond”]

标签: python discord.py pycord


【解决方案1】:

第一个问题

valid_ranks 只是一个字符串列表,但这是一个无效的输入。 相反,您需要在valid_ranks 中有OptionChoices

像这样:

from discord.commands import OptionChoice #  Don't forget to import OptionChoice

valid_ranks = [
    OptionChoice(name="Bronze", value="Bronze"), #  Value must be a string.
    OptionChoice(name="Silver", value="Silver"), #  Value must be a string.
    OptionChoice(name="Diamond", value="Diamond") #  Value must be a string.
]

Name 是将出现的选项,value 是分配给它的值。 value 只能是字符串,但可以在后面的代码中进行转换。

第二个问题

您需要为第一个选项添加一些参数。

这是一个应该可以工作的例子。

role: Option(
    discord.Role,
    name="role",
    description="What role are you assigning?",
    guild_ids=servers
)

最终产品

在你完成所有这些之后。它应该看起来像这样。

valid_ranks = [
    OptionChoice(name="Bronze", value="Bronze"), #  Value must be a string.
    OptionChoice(name="Silver", value="Silver"), #  Value must be a string.
    OptionChoice(name="Diamond", value="Diamond") #  Value must be a string.
]


@client.slash_command(name="setrankroles", guild_ids=[898725831164178442])
async def setroles(
        ctx,
        role: Option(
            discord.Role,
            name="role",
            description="What role are you assigning?",
        ),
        rank: Option(str, "What rank goes with that role?", choices=valid_ranks),
):

【讨论】:

  • optionchoice 的东西似乎不起作用,但角色部分很好。
  • 你用from discord.commands import OptionChoice@MaxwellWindland 导入包了吗?
  • 是的,它肯定是进口的。 @劳埃德
  • @MaxwellWindland 你有什么错误吗?
  • 不,没有错误@Lloyd
猜你喜欢
  • 2022-10-05
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 2022-06-20
  • 2016-07-19
  • 1970-01-01
  • 1970-01-01
  • 2022-06-24
相关资源
最近更新 更多