如何制作按钮倍增器

How to make a button multiplier

我正在创建一个机器人,它会基于 for 循环创建许多按钮,这就是我想要成功完成的事情: (我正在使用 discord_components,出于某种原因,没有标签,我知道为什么)

import discord
from discord_components import *

client = discord.Client()
intents = discord.Intents.default()
intents.members = True
intents.reactions = True
client = commands.Bot(command_prefix = "!", intents = intents)
client.remove_command('help')
@client.event
async def on_ready():
    print("ok")
    DiscordComponents(client)
@client.command()
async def create_buttons(ctx, numofbuttons: int, label):
    buttons = []
    for i in range(numofbuttons):
    buttons.append(Button(style=ButtonStyle.blue,label=label))
    await ctx.send("this message has buttons!",components=buttons)
    a = await client.wait_for("button_click")
    await ctx.send(f"{a.label} was clicked!")

这没有用,我假设是因为当您向列表添加函数时,您添加的是它 returns 而不是函数本身(如果您明白我的意思,对不起,我不是这方面的专家)。有没有什么办法解决这一问题?我正在处理的问题更复杂,绝对必须包含一个 for 循环。我为更易于阅读的更简单版本创建了上面的示例。我正在使用“await client.wait_for”,因为对于实际项目,我需要一种方法来获取按钮的标签

首先,正确命名函数,在createbutton

之间加下划线

numberofbuttons 参数标记为 int 会删除 TypeError.

的提升

* 放在 label 之前将 label 参数标记为 multi-line 参数。

您可以使用视图模块添加按钮,这是一个例子:

import discord
from discord.ui import Button, View

client = discord.Client()
intents = discord.Intents.default()
intents.members = True
intents.reactions = True
client = commands.Bot(command_prefix = "!", intents = intents)
client.remove_command('help')

@client.command()
async def create_buttons(ctx, numofbuttons: int, *, label: str):
    buttons = []
    view = View()
    for i in range(numofbuttons):
       view.add_item(Button(style=ButtonStyle.blue, label=label))
    await ctx.send("this message has buttons!",view=view)