#๐ Need help with buttons
6 messages ยท Page 1 of 1 (latest)
@opaque carbon
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
entries = []
class GiveawayButton(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(label='Enter', style=discord.ButtonStyle.green, custom_id='persistent_giveaway:enter')
async def button_callback(self, interaction: discord.Interaction, button: discord.ui.Button):
author = interaction.user
if str(author.id) not in entries:
entries.append(str(author.id))
await interaction.response.send_message('You have entered the giveaway', ephemeral=True)
else:
await interaction.response.send_message('You have already entered the giveaway', ephemeral=True)
class EndGiveawayButton(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(label='End Giveaway', style=discord.ButtonStyle.danger, custom_id='persistent_giveaway:end')
async def button_callback(self, interaction: discord.Interaction, button:discord.ui.Button):
ctx = await bot.get_context(interaction.message)
if any(role.name in globals.roleRole for role in ctx.author.roles):
if entries:
winnerID: str = random.choice(entries)
winner = f'<@{winnerID}>'
await interaction.response.send_message(f'Congratulations {winner}! You won **{prize}**.', ephemeral=False)
entries.clear()
else:
await interaction.response.send_message('No participants', ephemeral=True)
else:
await interaction.response.send_message("You're not authorized to end this giveaway.", ephemeral=True)
return
@bot.command(name='giveaway')
@commands.check(has_any_role_or_permission)
async def giveaway(ctx: commands.Context, *, prize_str: str = None):
if not prize_str:
error = await ctx.send('Enter a prize for the giveaway', ephemeral=True)
await asyncio.sleep(3)
await ctx.message.delete()
await error.delete()
return
global prize
prize = prize_str
entry_view = GiveawayButton()
end_view = EndGiveawayButton()
embed = discord.Embed(
title='Giveaway',
description=f'{prize}',
color=discord.Color.pink()
)
embed.set_footer(text='Made by vilkaviskietis')
await ctx.send(embed=embed, view=entry_view)
await ctx.send(view=end_view)
await ctx.message.delete()
im tryna restrict the end giveaway button but it jus ain work
anyone can end the giveaway
@opaque carbon
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.