如何在discord.py中使用discord.File发送图片?含多图发送失败问题
在discord.py中用discord.File发送多张图片的正确方式
你当前代码的问题在于,把三个图片路径一次性传给了单个discord.File实例——discord.File的构造函数只能接收一个文件路径,没法同时处理多个文件。要发送多张图片,得用ctx.send()的files参数,传入多个discord.File对象组成的列表。
修正后的代码示例
基础写法
@bot.command() async def image_file(ctx): # 为每张图片创建独立的discord.File对象 img1 = discord.File("--.png") img2 = discord.File("---.png") img3 = discord.File("----.png") # 通过files参数传入文件列表 await ctx.send(files=[img1, img2, img3])
简洁写法(适合批量处理)
@bot.command() async def image_file(ctx): # 把所有图片路径存在列表里 image_paths = ["--.png", "---.png", "----.png"] # 用列表推导式生成discord.File对象列表 await ctx.send(files=[discord.File(path) for path in image_paths])
额外注意事项
- 要确保图片路径正确,程序有权限访问这些文件,不确定的话可以用绝对路径(比如
"C:/images/--.png"或者"/home/user/images/--.png") - Discord单条消息最多支持10个附件,总文件大小不能超过8MB(普通服务器权限下)
内容的提问来源于stack exchange,提问作者Schiffskapitän
相关产品推荐
相关产品推荐

