首页 > 解决方案 > Discord.py 重写 || 发送文件

问题描述

我需要将chat.log 文件附加到消息中,但出现异常

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\vlad0\AppData\Roaming\Python\Python36\site-packages\discord\client.py", line 227, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\vlad0\Desktop\bot\bot.py", line 660, in on_message
    await channel.send('***Log for ticket #{id}***'.format(id = text),file = f )
  File "C:\Users\vlad0\AppData\Roaming\Python\Python36\site-packages\discord\abc.py", line 752, in send
    raise InvalidArgument('file parameter must be File')
discord.errors.InvalidArgument: file parameter must be File

我的代码:

    with open(file[0]+'/chat.log','r', encoding='UTF-8') as f:
        await channel.send('***Log for ticket #{id}***'.format(id = text),file = f.read() )

当我使用“rb”阅读时会发生同样的事情,如果您只指定文件的路径,也会发生同样的事情 如何将文件附加到消息?

标签: python-3.xdiscord.pydiscord.py-rewrite

解决方案


to 的参数file必须是一个discord.File对象:

from discord import File

await channel.send('***Log for ticket #{id}***'.format(id = text), file=File(file[0]+'/chat.log'))

推荐阅读