【问题标题】:Discord bot getting a file attachement and reuploading itDiscord bot 获取文件附件并上传
【发布时间】:2022-01-09 20:37:07
【问题描述】:

我想制作一个机器人,重新发送发送到频道的图像/文件。

bot 获取的是文件的 url (message.attachments[0].url),但是当我想将它作为文件发送时,它说 FileNotFoundError: [Errno 2] No such文件或目录:....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))

感谢您的帮助!

【问题讨论】:

  • discord.File 需要一个路径,而不是 URL。
  • 那么它是如何工作的呢? (第一次工作,然后没有)
  • 不确定,但应该不会。
  • file=await discord.File(message.attachments[0].url) 如果我把 await 放在前面

标签: python discord discord.py bots


【解决方案1】:

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

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

【讨论】:

  • 谢谢你的作品!
  • 实际上它也是这样工作的:file=await discord.File(message.attachments[0].url) 哇,所以我只需要一个 AWAIT。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-10
  • 2021-08-10
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
  • 2018-10-30
  • 2019-08-12
相关资源
最近更新 更多