Issue sending file via Discord bot (Python)

2024/11/14 18:30:30
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_messageawait client.send_file(channel, picture)
NameError: name 'channel' is not defined
Answer

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.

https://en.xdnf.cn/q/119096.html

Related Q&A

Matplotlib installation on Mavericks

Im having problem while installing matplotlib. Im using Mavericks and it complains about a deprecated NumPy API both installing via pip and installing from source (following the instructions here https…

Exact string search in XML files?

I need to search into some XML files (all of them have the same name, pom.xml) for the following text sequence exactly (also in subfolders), so in case somebody write some text or even a blank, I must …

Integrate a function by the trapezoidal rule- Python

Here is the homework assignment Im trying to solve:A further improvement of the approximate integration method from the last question is to divide the area under the f(x) curve into n equally-spaced tr…

Kivy module not found in vscode (Mac)

I have installed Kivy and when I used the IDLE app that came with Python I can import it and it runs perfectly. However, when I try to import it in vscode I get the error: ModuleNotFoundError: No modul…

How to get latest unique entries from sqlite db with the counter of entries via Django ORM

I have a SQLite db which looks like this:|ID|DateTime|Lang|Details| |1 |16 Oct | GB | GB1 | |2 |15 Oct | GB | GB2 | |3 |17 Oct | ES | ES1 | |4 |13 Oct | ES | ES2 | |5 |15 Oct | ES | ES3 …

What does this code %.8f% do in python? [duplicate]

This question already has answers here:What does % do to strings in Python? [duplicate](4 answers)Closed 6 years ago.I am editing a code line to pass the rate in quotes:OO000OO00O0O0O000 [rate]=O0O0OO…

How to append a selection of a numpy array to an empty numpy array

I have a three .txt files to which I have successfully made into a numpy array. If you are curious these files are Level 2 data from the Advanced Composition Experiment (ACE). The particular files are …

Error saving and loading a list of matrices

I have a list "data_list", and I would save it in order to load it in another script. First of all I converted it in an array, in this way:data_array = np.array(data_list)Then I saved it:np.s…

Trying to interact with HTML page elements, but none of them are found

Im trying to scrape a webpage using Selenium, but when I try to pass the XPath of a button, I get an error saying that this element does not exist. I tried with another website, and it worked perfectly…

Duplicating an XML element and adding it to a specific position in XML file using python

I have a xml file in which content looks like this: xml_content_to_search = <Document ProviderID="TD" DecimalMarker="comma" Website="https://erc-viewer.sap.com/"> &l…