'How to make a button multiplier
I am creating a bot that would create a number of buttons based on a for loop, this is what I want to succeed in doing: (I'm using discord_components, for some reason, there isn't a tag for that, idk why)
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!")
this didn't work, I assumed because when you add a function to a list, you are adding what it returns instead of the function itself(if you know what I mean, sorry I'm not an expert at this stuff). is there any way to fix this? the problem I'm dealing with is more complex and absolutely has to include a for loop. I created the sample above for a simpler version that is much more easier to read. I am using "await client.wait_for" because for the real project, I need a way to get the button's label
Solution 1:[1]
First, name the function properly, put an underscore between create and button
Marking the numberofbuttons parameter as int removes raising of TypeError.
Putting the * before label marks the label parameter as a multi-line parameter.
You can use the view module to add buttons, this is an example:
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sandy |
