我需要从我的机器更改本地文件(照片)的嵌入。 要发送嵌入,您需要将文件一起发送,但无法更改。 当我尝试这样做时:
我的代码:
embed = discord.Embed(title = "Title here", description = "",
timestamp = datetime.utcnow(),
color = 0x26ad00)
file = discord.File(f"images/{msg.id}.png")
embed.set_image(url = f"attachment://{msg.id}.png")
await msg.edit(file = file, embed = embed)
我收到以下错误:
TypeError: Object of type File is not JSON serializable
直接使用互联网上的链接时不会出现此类错误(没有来自链接的文件)。我想我需要使用照片 API 或其他方式将照片上传到互联网,但速度很慢,这个问题有解决方案吗?
最佳答案
邮件发送后,您无法更改邮件的附件。不和谐根本不允许这样做。如果你检查discord.py documentation for discord.Message.edit
,您会看到 file
参数不被接受。您的错误是由于 Discord.py 尝试将文件转换为 JSON 以与 API 请求一起发送而引起的
也就是说,有一种解决方法可以更改图像。正如您所指出的,您提供给 discord.Embed.set_image
的 URL 不必指向附件 - 它可以指向 Discord 可以访问的任何图像。如果您要在某处发送包含新图像的消息(例如在只有您和您的机器人可以访问的 secret channel 中),您可以在编辑中使用该附件的 URL,如下所示:
# you need to define secret_channel before this (example below)
secret_channel = bot.get_channel(12345) # where 12345 would be your secret channel id
file = discord.File(f"images/{msg.id}.png")
temp_message = await secret_channel.send(file = file)
attachment = temp_message.attachments[0]
embed = discord.Embed(title = "Title here", description = "",
timestamp = datetime.utcnow(),
color = 0x26ad00)
embed.set_image(url = attachment.url)
await msg.edit(embed = embed)
关于python - 是否可以在discord python中更改发送的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70383667/