-2

I want my bot to send random images from a locally created folder, for this I tried the following code:

@client.command()
async def hug(ctx):
    await ctx.send(file=discord.File(random.choice(r "F:\Discordbot\hug")))

This one didn't really work though. Where is the error or how can I proceed?

Dominik
  • 3,612
  • 2
  • 11
  • 44
  • Welcome to StackOverflow. Do you face any errors? What is the traceback? You may also take a look at [how to ask a good question](https://stackoverflow.com/help/how-to-ask) – Dominik Mar 17 '21 at 22:23
  • https://stackoverflow.com/questions/306400/how-to-randomly-select-an-item-from-a-list – stijndcl Mar 17 '21 at 23:11
  • @stijndcl This is not really what he is asking/looking for. He wants to select random items out of a folder, not list. – Dominik Mar 17 '21 at 23:13
  • @Dominik ``os.listdir`` returns a ``list``, so looking up how to get files in a folder (which should be the first search result) & combining that with the post I linked _is_ what he's looking for. If he's trying to get things out of a folder, he's also trying to get things out of a list. – stijndcl Mar 18 '21 at 13:18

1 Answers1

1

You need to define the path a little differently and then send it accordingly.

Here is an example:

import random
import os

@client.command()
async def hug(ctx):
    path = random.choice(os.listdir('Full Path/'))
    await ctx.send(file=discord.File("Full Path/"+path))
  • Make sure you copy the whole path
  • Use / instead of \
  • Add a / at the end of the folder path
  • import os and random
Dominik
  • 3,612
  • 2
  • 11
  • 44