In my discord bot, I have a function that collects deleted messages and funnels them to a channel on a separate server. This works perfectly for text but, refuses to work for images. Does anyone have any tips on how I could get this to work for images?
Here is what I have in my event:
@commands.Cog.listener()
async def on_message_delete(self, message):
#print(f"{message.author} deleted a message in #{message.channel}: {message.content}") #Tells the Console about the deleted message
if message.author == self.client.user:
return
elif message.author.bot == True:
return
now = datetime.now() #Grabs the current time
current_time=now.strftime("%H:%M:%S") #Formats the current time into a readable form.
current_date=now.strftime("%d/%m/%Y") #Formats the current date into a readable form.
delete_embed=discord.Embed(title = f"Message Deleted", description= f'**User:** <@{message.author.id}>\n**Channel:** <#{message.channel.id}>\n**Server:** {message.guild}\n**Message:** \n{message.content}', color=0xbf0404)
delete_embed.set_footer(text=f"Message ID: {message.id}\nDate: {current_date} • Time: {current_time}")
delete_embed.set_author(name =f"{message.author}", icon_url=f"{message.author.avatar_url}")
archive_delete=self.client.get_channel(789080972309168139) #Grabs the ID of the Deleted Messages Channel where the archive is to be stored.
try:
delete_embed.set_image(url=message.attachments[0].url)
except IndexError:
pass
await archive_delete.send(embed=delete_embed)
files=open("DeletedMessages.txt", "a") #Open the text file containing a backup of the deleted messages.
files.write(f"{message.author}'s message in #{message.channel} in {message.guild} was deleted at {current_time}: '{message.content}'\n")
Try using .proxy_url
as opposed to .url
:
delete_embed.set_image(url=message.attachments[0].proxy_url)
Attachment.proxy_url
- "When the message is deleted, this URL might be valid for a few minutes or not valid at all."
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.