Discord.py重写||发送文件

时间:2019-03-16 14:43:08

标签: python-3.x discord.py discord.py-rewrite

我需要在消息中附加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”阅读时,会发生相同的事情;如果仅指定文件的路径,也会发生同样的事情 如何在邮件中附加文件?

1 个答案:

答案 0 :(得分:1)

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

from discord import File

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