I wanted to make a bot that DMs a user as soon as they react to a message sent by that bot. The DM has a link to a form, and I want to disable that link once they clicked on the url. This is why I decided to use a button instead of a URL, so I can disable it once they clicked on the button. I was also able to create the button using this code
class SimpleView(discord.ui.View):
def __init__(self):
super().__init__(timeout=None)
@discord.ui.button(label="Upload Audition Now",
style=discord.ButtonStyle.url,
url = 'https://google.com'
)
await [#and here is where I would make the link open]
I tried adding the url property and stuff, but I am probably doing it wrong. Can someone please tell me what the correct code for opening a url on a button press is? Thanks! This is in python by the way.
You have to instantiate an instance of the discord.ui.Button
class manually to use a url
. However, a button with a URL doesn't send an interaction to the bot - there's therefore no callback that can be used to disable the button when it's pressed and no way of knowing when it is pressed. You would have to handle the user clicking it multiple times on the web end if you're in control of the URL.
You could add a timeout
to the view
so that it times out after a set period of time - this would limit the number of times a user would be able to click on it. I've included that in the example below.
class SimpleView(discord.ui.View):
def __init__(self):
super().__init__(timeout=30) # times out after 30 seconds
button = discord.ui.Button(label='Upload Audition Now', style=discord.ButtonStyle.url, url='https://google.com')
self.add_item(button)
async def on_timeout(self):
# set the view to None so that the buttons are no longer available
# or you could just disable the buttons if you want
await self.message.edit(content="Link timed out", view=None)