【问题标题】:Uploading a txt with discord.py on_message()使用 discord.py on_message() 上传 txt
【发布时间】:2021-04-06 08:35:45
【问题描述】:

我正在尝试通过 on_message() 事件发送带有 discord.py 的文本文件。到目前为止,我做的最多的是:

with open('file.txt', 'rb') as fp:
  await message.channel.send(file=discord.file(fp, 'TestFile'))

(我显然启动了机器人并且 on_message 事件有效)。

无论何时运行它都会给出错误提示“TypeError: 'module' object is not callable”。这种情况下的“模块”是import discord。我已经在这个问题上卡住了 2 天,如果能提供任何帮助,我将不胜感激。

【问题讨论】:

  • 能否提供完整的代码示例,本文给出的代码不足以重现问题。

标签: python file discord discord.py


【解决方案1】:

您可能正在寻找discord.File,而不是discord.file

with open('file.txt', 'rb') as fp:
  await message.channel.send(file=discord.File(fp, 'TestFile'))

如果你要做一些实验......

>>> import discord
>>> type(discord.file)
<class 'module`>
>>> type(discord.File)
<class 'type'>
>>>

...您会看到,是的,discord.file 是一个模块,而您正在寻找 discord.File 类型。

【讨论】:

  • 谢谢。由于一件微不足道的事情,我已经为此困扰了很长时间,我无法用语言来解释我是多么感激。在过去的两天里,我不敢相信一个字母的大写让我感到困惑。也感谢您如此快速的回答,这意味着很多。
  • @AverageLua 没问题!它发生在我们最好的人身上。
猜你喜欢
  • 1970-01-01
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 2020-12-18
  • 1970-01-01
相关资源
最近更新 更多