python - 是否可以在discord python中更改发送的文件?

标签 python discord.py

我需要从我的机器更改本地文件(照片)的嵌入。 要发送嵌入,您需要将文件一起发送,但无法更改。 当我尝试这样做时:

我的代码:

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/

相关文章:

Discord.py 重写获取公会成员列表

python - 系统发育树着色

Python Tornado 处理程序警告禁用

Python Spark Streaming 仅运行一次

python - 使用gitignore分隔开发和生产环境

python - 创建一个记录主持人命令的记录器,类似于 'modlogs'

python - 使用 Pandas 聚合数据并保留表结构和列名的更好方法

python - matplotlib中2个y坐标之间的阴影区域

python - Discord.py 如何删除服务器中的所有角色

python - 如何在一条不和谐消息中发送 for 循环的结果?