首页 > 解决方案 > Discord.py 嵌入本地文件

问题描述

我尝试将本地设备中的文件添加到 discord.py 嵌入中。下面是我的代码,虽然它不起作用请帮助我

import discord
from discord.ext import commands

client = commands.Bot(command_prefix="!")

@client.command
async def pika(ctx):
  file = discord.File("‪E:\Python\discord.py\Pikachu1.jpg", filename="image.jpg")
  embed = discord.Embed(color=0xFFFFFF)
  embed.set_image(url="E:\Python\discord.py\Pikachu1.jpg")
  await ctx.send(file=file, embed=embed)

标签: pythonerror-handlingdiscorddiscord.py

解决方案


要将本地文件用于嵌入的图像,请使用 Discord 的attachment://协议。(归因于卢卡斯)。

file = discord.File("path/to/my/image.png", filename="image.png")
embed = discord.Embed()
embed.set_image(url="attachment://image.png")
await channel.send(file=file, embed=embed)

https://discordpy.readthedocs.io/en/stable/faq.html#how-do-i-use-a-local-image-file-for-an-embed-image


推荐阅读