-1

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.

5
  • listdir returns the name of the files, not the complete paths. Commented Nov 16, 2020 at 22:22
  • Oh ok it's a problem lol. Do you have any method I can use ? Commented Nov 16, 2020 at 22:24
  • @LéoCarrez why not just store the path in a variable, then concatenate the path and the file name? Commented Nov 16, 2020 at 22:40
  • 1
    Have a look at glob.glob. I think that is exactly what you're looking for. Commented Nov 16, 2020 at 22:45
  • Because my code works, he takes a random image in my folder but he don't send the picture on discord he return an error like : FileNotFoundError: [Errno 2] No such file or directory: 'the Name Of The Picture'. Commented Nov 16, 2020 at 22:46

2 Answers 2

2

I think that you can solve your problem using glob.glob:

import glob
from pprint import pprint
from random import choice

goats = glob.glob("P:/Goats/*.png")
pprint(goats)
print(choice(goats), "is the chosen one!")

Output:

['P:/Goats\\Goat0.png',
 'P:/Goats\\Goat1.png',
 'P:/Goats\\Goat2.png',
 'P:/Goats\\Goat3.png',
 'P:/Goats\\Goat4.png',
 'P:/Goats\\Goat5.png',
 'P:/Goats\\Goat6.png',
 'P:/Goats\\Goat7.png',
 'P:/Goats\\Goat8.png',
 'P:/Goats\\Goat9.png']
P:/Goats\Goat2.png is the chosen one!
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks your code is worked but i don't know how i can send the picture on channel discord
Use discord.File: channel.send(file=discord.File(fp="path/to/file.png", filename="image.png")).
@LéoCarrez You have to target the channel that the message was sent from. I'm not 100%, but you should be able to access the channel-object via message.channel in your original code.
Do you know how can I do that because i've try to found it on discord.py doc but i don't understand how it's works.
0

I have finally found the answer on several forum and doc. So my code is :

import discord
import glob
from pprint import pprint
from random import choice

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'):

            goat = glob.glob("/home/leo/Bureau/bot-discord/image/*.jpeg")
            pprint(goat)
            image = choice(goat)

            await message.channel.send(file=discord.File(image))

thanks everyone for your answers. Have a good day.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.