问题:通过Discord bot(Python)发送文件

问题:通过Discord bot(Python)发送文件,python,discord,discord.py,Python,Discord,Discord.py,我已经启动并运行了discord bot(所有内容都是用python编写的)。我正在尝试让机器人在命令“!headpats”时在频道中发送.gif。文件被上传,代码编译得很好,但是当!headpats命令是通过discord调用的,编译器会将其输出 if message.content.upper().startswith("!HEADPATS"): time.sleep(1) with open('tenor.gif', 'rb') as picture: awai

我已经启动并运行了discord bot(所有内容都是用python编写的)。我正在尝试让机器人在命令“!headpats”时在频道中发送.gif。文件被上传,代码编译得很好,但是当!headpats命令是通过discord调用的,编译器会将其输出

if message.content.upper().startswith("!HEADPATS"):
    time.sleep(1)
    with open('tenor.gif', 'rb') as picture:
      await client.send_file(channel, picture)

您在
on_消息中看到的所有内容都是收到的消息。如果你想推断
频道
在里面,
服务器
在打开,作者
是谁写的等等,你必须通过
消息
解析这些属性。(
message.channel
message.server
,等等)


如果您使用的是
discord.ext.commands
扩展名,则首先需要将消息解析为:
ctx.message.channel
的属性。

您需要通过消息解析频道:
message.channel
。这附近有个复制品。@PatrickHaugh我想就是这个…@PatrickHaugh把所有东西都修补好了。谢谢你的帮助!这实际上不是一个很好的复制品。我会写一个恰当的答案。
File "main.py", line 106, in on_message
    await client.send_file(channel, picture)
NameError: name 'channel' is not defined