I'm trying to make a discord bot that sends a random image of goat (which are in a folder) when a user types !goat. But I can't get there and can't really find cases like mine.
Here is my code:
import discord
import random
import os
class MyClient(discord.Client):
async def on_ready(self):
print('Logged in as')
print(self.user.name)
print(self.user.id)
print('------')
async def on_message(self, message):
# we do not want the bot to reply to itself
if message.author.id == self.user.id:
return
if message.content.startswith('!goat'):
with open(random.choice(os.listdir("/home/leo/Bureau/bot-discord/image")), 'rb') as f:
picture = discord.File(f)
await channel.send(channel, picture)
Thanks for your answers.
listdirreturns the name of the files, not the complete paths.glob.glob. I think that is exactly what you're looking for.