【问题标题】:discord py - save a user posted image and delete the old onediscord py - 保存用户发布的图像并删除旧图像
【发布时间】:2021-04-09 03:37:36
【问题描述】:

我的机器人应该嵌入特定频道中的所有消息。他删除了每条不在嵌入中的消息,并自动将它们放入嵌入中。现在,这行得通,但我的用户上传的图片有问题。

我的问题:如何将上传用户的图片保存在“日志通道”中并从该图片中获取新的不和谐图片链接?

我需要“新的不和谐图片链接”将其放入嵌入中。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我试过“pic.save(pic.url)”,但机器人说没有这样的文件或目录:'media.discordapp.net/attachments/792561722090848266/…' 但正如你所见,文件存在。
  • 嗨。如果您还没有,请使用tour 并阅读How to Ask。分享您的代码,包括您迄今为止尝试过的解决方案,以便其他人可以更好地帮助您

标签: discord.py discord.py-rewrite


【解决方案1】:

discord.Message 实例包含attachments 属性,它将为您提供邮件附件的列表。然后,您可以使用 discord.Attachment.to_file() 获取 discord.File 实例,您可以在嵌入中使用它。

例子:

message = SomeMessageInstance
# realistically you should be looping over the attachments, and sending an embed for each
file = await message.attachments[0].to_file()
file.filename = 'image.png'
embed = discord.Embed()
embed.set_image(url='attachment://image.png')

await Channel.send(file=file, embed=embed)

【讨论】:

  • 但我只需要几天的临时图像。不是永远。
  • 将其转换为 discord.File 实例不会保存图像。
猜你喜欢
  • 1970-01-01
  • 2020-11-22
  • 2013-01-25
  • 1970-01-01
  • 2017-01-19
  • 1970-01-01
  • 1970-01-01
  • 2012-05-31
  • 2011-04-08
相关资源
最近更新 更多