'random image sender discord py python 3.8
im new to discord py i want a command that will send a random image from a file and send it but i can get it to work i tried to use Discord bot to send a random image from the chosen file one but still could not get it to work so then i tried this but i still no work if anyone can help me plz do
#put your images into the image folder
#images that are uploaded will be moved to the sent_images folder so they won't be reposted
import discord,os,random,asyncio
from datetime import datetime
from discord.ext import commands
bot=commands.Bot(command_prefix='!')
send_time='12:00' #time is in 24hr format
image_channel_id='ChannelID' #channel ID to send images to, replace with your channel ID
image_types=('.jpg','.png','.gif','.jpeg') #add image types if needed
folders={'Image Folder':'images','Sent Folder':'sent_images'}
for folder in folders.values():
if not os.path.isdir(folder):
os.mkdir(folder)
@bot.event
async def on_ready():
print(bot.user.name)
print(bot.user.id)
async def send_image():
for item in os.walk('./{}'.format(folders['Image Folder'])):
images=list(pic for pic in item[2] if pic.lower().endswith(image_types))
if len(images) == 0:
await bot.send_message(image_channel,"Oops! I'm all out of images to send. Notify my owner!")
else:
image= random.choice(images)
await bot.send_file(image_channel,'./{}/{}'.format(folders['Image Folder'],image))
os.rename('./{}/{}'.format(folders['Image Folder'],image), './{}/{}'.format(folders['Sent Folder'],image))
async def time_check():
global image_channel
await bot.wait_until_ready()
image_channel=bot.get_channel(image_channel_id)
while not bot.is_closed:
now=datetime.strftime(datetime.now(),'%H:%M')
if now == send_time:
await send_image()
await asyncio.sleep(60)
bot.loop.create_task(time_check())
bot.run('TOKEN')```
Solution 1:[1]
From your question, I understood that you have a bunch of folders with a bunch of images in them and you want to make a command that sends a random image.
import os, random
@bot.command()
async def random_image(ctx, category: str = None):
root_dir = os.listdir('./images')
# If the user doesn't specify a category send a random image from a random folder
if category is None:
# Getting a random directory and file
dir = random.choice(root_dir)
image = random.choice(os.listdir(os.path.join(root_dir, dir)))
path = os.path.join(dir, image)
elif category in root_dir:
# Getting a random file from the category
image = random.choice(os.listdir(os.path.join(root_dir, category)))
path = os.path.join(dir, image)
else:
await ctx.send("Couldn't find that category")
return
# Sending the image in an embed
file = discord.File(path)
embed = discord.Embed(title=category.title(), colour=discord.Colour.greyple())
embed.set_image(url=f'attachments://{image}')
await ctx.send(file=file, embed=embed)
{prefix}random_image ? will send a random image from a random dir
{prefix}random_image dog ? will send a random image from ./images/dog dir
Let me know if that's what you were asking for.
Solution 2:[2]
@bot.command()
async def random_image(ctx):
Images = ['./Folder/Image_Name.png',
'./Folder/Image_Name.png',
'./Folder/Image_Name.png',
'./Folder/Image_Name.png',
'./Folder/Image_Name.png',
'./Folder/Image_Name.png',
'./Folder/Image_Name.png',
'./Folder/Image_Name.png',
'./Folder/Image_Name.png']
await ctx.send(file=discord.File(f'{random.choice(Images)}'))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Åukasz KwieciÅ„ski |
| Solution 2 | BreakerOfWind |
