'Discord.py Hug command with image
I'm trying to make a hug command that sends an image. But upon sending the command, nothing happens. No error in the cli and no message in the channel.
@client.command(pass_context=True)
async def hug(ctx):
os.chdir(r"file to the folder holding the images")
hugs = [discord.File('1.jpg'), discord.File('2.jpg'), discord.File('3.jpg'), discord.File('4.gif'), discord.File('5.gif'), discord.File('6'), discord.File('7.gif'), discord.File('8.gif'), discord.File('9.gif'), discord.File('10.gif'), discord.File('11.gif')]
hugsrandom = random.choise(hugs)
await ctx.send(file=hugsrandom)
I also tried just sending an image inside the folder with the bot file and took out os.chdir but still nothing sent.
Solution 1:[1]
UPDATE2: The following just worked for me... I would almost certainly expect your issue is that your files are not being created properly. Either that or your bot is broken in some other way. If you use a debugger (say pycharms) you could set break points and see if your command is executing and whether or not your discord.File objects are being created properly
async def test(self, ctx):
image = r"C:\Users\name\Desktop\visual-reverse-image-search-v2_intro.jpg"
import discord
await ctx.send(file=discord.File(image))
UPDATE: I did not read far enough, it should accept a string path. Have you tried debugging into the code to see if it's successful at opening your file?
Sometimes it's worth looking at the source code. I went and looked at discord.File.
class File:
"""A parameter object used for :meth:`abc.Messageable.send`
for sending file objects.
Attributes
-----------
fp: Union[:class:`str`, :class:`io.BufferedIOBase`]
A file-like object opened in binary mode and read mode
or a filename representing a file in the hard drive to
open.
.. note::
If the file-like object passed is opened via ``open`` then the
modes 'rb' should be used.
To pass binary data, consider usage of ``io.BytesIO``.
"""
def __init__(self, fp, filename=None, *, spoiler=False):
self.fp = fp
if isinstance(fp, io.IOBase):
if not (fp.seekable() and fp.readable()):
raise ValueError('File buffer {!r} must be seekable and readable'.format(fp))
self.fp = fp
self._original_pos = fp.tell()
self._owner = False
else:
self.fp = open(fp, 'rb')
self._original_pos = 0
self._owner = True
It appears as though it's expecting an opened file.
Solution 2:[2]
It's a bad idea to do os.chdir().
It can have unintended consequences. Instead do:
import os
BASE_DIR = os.path.abspath(__file__)
PICTURE_1_PATH = os.path.join(BASE_DIR, '../pictures/1.jpg')
PICTURE_1_FILE = discord.File(PICTURE_1_PATH)
BASE_DIR is the path of folder containing the python file.
../pictures/1.jpg is the relative path to that folder.
Solution 3:[3]
You can scan all the files paths in the folder and add them to the list using glob.
import glob
hugs = glob.glob("FULL_PATH_HERE/*") # this wil get all the file paths in the foldes.
# ['FULL_PATH_HERE/1.gif', 'FULL_PATH_HERE/2.jpg']
the_hug = random.choice(hugs)
ctx.send(file = discord.File(the_hug))
Solution 4:[4]
hug_gifs = ["","","","","","", "", ""] #Put the hug gifs you would like to use
hug_names = ['', '', ''] #Put the hug names you would like to use
@client.command()
async def hug(ctx, *, member : discord.Member):
embed = discord.Embed(
colour=discord.Colour(0xCE3011),
description=f"{ctx.author.mention} {(random.choice(cuddle_names))} {member.mention}"
)
embed.set_image(url=(random.choice(cuddle_gifs)))
await ctx.send(embed=embed)
#Not images but gifs are better
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 | |
| Solution 2 | Tin Nguyen |
| Solution 3 | Abdulaziz |
| Solution 4 | Migi |
