Issue sending file via Discord bot (Python)

时间:2018-09-18 19:40:24

标签: python discord discord.py

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

I've got my discord bot up and running (everything's written in python). I'm trying to get the bot to send a .gif in the channel upon the command "!headpats". The file is uploaded and the code compiles fine, but when the !headpats command is called via discord, the compiler spits this out...

File "main.py", line 106, in on_message
    await client.send_file(channel, picture)
NameError: name 'channel' is not defined

1 个答案:

答案 0 :(得分:0)

All you get inside your on_message event is the Message that was received. If you want to infer the channel it's in, the server it's on, the author who wrote it, etc., you'll have to resolve those attributes through the message. (message.channel, message.server, and so on).

If you're using the discord.ext.commands extension, then you first need to resolve the message as an attribute of the invocation context: ctx.message.channel, for example.