Issue
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
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.
Answered By - bravosierra99
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.