Hey folks! I’m working on a Discord bot and I’m wondering if it’s possible to make a button that starts a file download on the user’s computer when they click it. Here’s what I’ve got so far:
class MyView(discord.ui.View):
def __init__(self, image_url):
super().__init__()
self.image_url = image_url
self.setup_buttons()
def setup_buttons(self):
save_btn = discord.ui.Button(style=discord.ButtonStyle.primary, label='Save Image', row=0)
save_btn.callback = self.save_image
self.add_item(save_btn)
async def save_image(self, interaction: discord.Interaction):
self.children[0].disabled = True
await interaction.response.edit_message(view=self)
img_data = requests.get(self.image_url).content
with open('temp_image.png', 'wb') as img_file:
img_file.write(img_data)
# How to make this download to user's computer?
This code downloads the image to my bot’s directory, but I want users to get the file on their own computers. Is that even possible with discord.py? Any ideas or workarounds? Thanks!
As someone who’s worked on Discord bots before, I can tell you that unfortunately, there’s no way to directly trigger file downloads on users’ devices. It’s a security limitation set by Discord. However, there’s a workaround I’ve used that might help you out.
Instead of trying to download the file to the user’s computer, you can have your bot upload the image to the Discord channel and then provide a download link. Here’s how I’ve done it:
Download the image to your bot’s server.
Use discord.File to upload the image to the channel.
Get the URL of the uploaded file from the message.attachments.
Send that URL to the user.
This way, users can click the link to download the image themselves. It’s not automatic, but it’s the closest you can get within Discord’s limitations. Hope this helps with your project!
Unfortunately, Discord bots can’t directly trigger file downloads on users’ devices for security reasons. However, you can achieve something similar by having your bot upload the file to Discord and then provide a download link. Here’s one approach:
In your callback, download the image to your bot, create a discord.File object, and send it as an attachment. Once sent, retrieve the file’s URL from the message attachments and send that link to the user. Although this doesn’t start an automatic download, it allows users to download the file manually, which is the best workaround given Discord’s limitations.
nah, discord bots can’t force downloads on users devices. security reasons n all. but u can make the bot upload the file to discord and give a link. it’s not automatic but works ok. users just click n download themselves. not perfect but gets the job done