Discord bot to send a random image from the chosen file

2024/10/6 1:38:19

I am making a discord bot that randomly chooses an image (images) which is in the same directory (Cats) as the python file(cats.py). This is what my code looks like right now:

Cats = os.path.join(os.path.dirname(__file__), "/images")@client.command()
async def cat(ctx, **kwargs):await ctx.send(choice(Cats))

I am not getting any errors. The bot goes online and when I ping it with ~cat it spits out random letters. I know my issue is with the async (possibly the kwargs) and await line, but cannot pin point exactly what is the issue. I am new to programming bots in Python so there might be a silly mistake that I am overlooking so any lead would be much appreciated!

Answer

If you send it directly by calling send right on the channel you are most likely sending the raw text of the file rather than uploading it as an image.

From the documentation you should be doing this:

Cats = os.path.join(os.path.dirname(__file__), "/images")@client.command()
async def cat(ctx, **kwargs):await ctx.send(file=discord.File(choice(Cats)))

Note that Cats has to a string, i.e the path to the file.

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

Related Q&A

pytest - patched method of a class does not return the mock value

My code is fairly simple but i dont understand what is going on :class MyDb :def some_func( arg ) :....while my test code is :@mock.patch(mypkg.mydb) @pytest.mark.parametrize( func_dummy_value ) :( [ {…

New instance of toplevel classes make overlapping widgets

Im generally new to python and tkinter. Ive been programming maybe about a year or so, and Ive just started to try to make each tkinter toplevel window its own class because Ive heard that its the righ…

Regex End of Line and Specific Chracters

So Im writing a Python program that reads lines of serial data, and compares them to a dictionary of line codes to figure out which specific lines are being transmitted. I am attempting to use a Regul…

Is it possible to scrape webpage without using third-party libraries in python?

I am trying to understand how beautiful soup works in python. I used beautiful soup,lxml in my past but now trying to implement one script which can read data from given webpage without any third-party…

Different model performance evaluations by statsmodels and scikit-learn

I am trying to fit a multivariable linear regression on a dataset to find out how well the model explains the data. My predictors have 120 dimensions and I have 177 samples:X.shape=(177,120), y.shape=(…

Python to search CSV file and return relevant info

I have a csv file with information about some computers on our network. Id like to be able to type from the command line a quick line to bring me back the relevant items from the csv. In the format:$…

Remove all elements matching a predicate from a list in-place

How can I remove all elements matching a predicate from a list IN-PLACE? T = TypeVar("T")def remove_if(a: list[T], predicate: Callable[[T], bool]):# TODO: Fill this in.# Test: a = [1, 2, 3, …

Python-scriptlines required to make upload-files from JSON-Call

Lacking experience & routine for Python-programming. Borrowing examples from forums have made a script which successfully makes a JSON-cal (to Domoticz) and generates & prints the derived JSON-…

python pygame mask collision [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 4 years ago.The com…

How to find max average of values by converting list of tuples to dictionary?

I want to take average for all players with same name. I wrote following code. its showing index error whats the issue?Input: l = [(Kohli, 73), (Ashwin, 33), (Kohli, 7), (Pujara, 122),(Ashwin, 90)]Out…