小编MrM*_*com的帖子

动态处理视图中的多个按钮| Pycord、Discord.py

我正在从 Novus 切换到 Pycord 并尝试转换按钮功能。我正在尝试获取一个列表并添加按钮 1-n 来表示列表的长度,然后获取所选按钮的 custom_id 。选择后,我想继续该命令并在命令的其余部分中使用 custom_id。到目前为止,这就是我所拥有的:

@bot.slash_command(name='bombs', description='Returns bombs to destroy base and airfield.')
async def bomb(ctx):
    await ctx.interaction.response.defer()
    countries = ["America", "Britain", "China", "France", "Germany", "Italy", "Japan", "Russia", "Sweden"]

    view = DefaultView()
    for number in list(range(1, len(countries) + 1)):
        view.add_item(DefaultButton(label=str(number), custom_id=str(number)))
    country_choice_message = await ctx.interaction.followup.send("Select a country to view bombs from:", view=view)

    timed_out_ = await view.wait()
    if timed_out_:
        view.disable_all_items()
        view.stop()
        await country_choice_message.edit(view=view)
        await ctx.interaction.followup.send("Timed out.", ephemeral=True)
        return

    # country_number = button_custom_id
    # do …
Run Code Online (Sandbox Code Playgroud)

python discord discord.py pycord

2
推荐指数
1
解决办法
2865
查看次数

标签 统计

discord ×1

discord.py ×1

pycord ×1

python ×1