Discord bot 获取文件附件并上传

Discord bot getting a file attachement and reuploading it

我想制作一个机器人,重新发送 image/file 发送到频道。

机器人获取文件的 url (message.attachments[0].url),但是当我想将其作为文件发送时,它显示 FileNotFoundError: [Errno 2] 没有那个文件或目录: ....png

该机器人正在工作并重新发送图像一次,但大约 50 次后,就没有了。

async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith("anon ") or message.content.startswith("Anon "):
        if message.attachments:
            await message.channel.send(f"{message.content[5:]}", file=discord.File(message.attachments[0].url))

感谢您的帮助!

您需要将附件转换为文件。您可以像这样使用 to_file() 函数:

await message.channel.send(f"{message.content[5:]}", file=await message.attachments[0].to_file()