【问题标题】:How to send png, jpg files as bot via url or from specific folder? Discord.py如何通过 url 或从特定文件夹将 png、jpg 文件作为机器人发送?不和谐.py
【发布时间】:2022-01-18 15:44:15
【问题描述】:

好吧,我尝试从特定文件夹发送图片,但机器人找不到它。是我吗,我不能编写可以很好地搜索文件夹的代码。我还希望机器人从 url 发送图像。

这是我的代码:

@commands.command()
    async def picture(self, ctx, picture):
        await ctx.send(file = discord.File(picture))
        await ctx.message.delete()

我只是在学习 Python,因此非常感谢任何帮助。

【问题讨论】:

  • 您是在计算机上运行机器人还是通过网络解释器等在线软件运行机器人?
  • 另外,您介意发布运行命令时遇到的错误吗?顺便欢迎使用 Stack!
  • 这能回答你的问题吗? Discord.py Bot sending file to Discord Channel
  • 我在 replit 上运行机器人。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python discord.py


【解决方案1】:

你可能需要一些这样的代码:

await ctx.send(file=discord.File(r'C:\location\of\the\file.png'))

discord.File 对象的构造函数将文件的path 作为第一个参数。


在你的情况下,你可以像这样编辑你的代码:

async def picture(ctx, path: str) -> None:
    await ctx.send(file=discord.File(path))
    await ctx.message.delete()

如果您想了解有关发送文件的更多信息(我读到您也想通过 url 发送文件),您应该在此链接中编写文档:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-05
    • 2021-08-11
    • 2021-08-19
    • 2021-04-26
    • 2021-03-01
    • 2020-12-18
    • 2021-05-08
    • 2021-08-14
    相关资源
    最近更新 更多