Python 3.x Discord.py Hug命令与图像

Python 3.x Discord.py Hug命令与图像,python-3.x,discord,discord.py,Python 3.x,Discord,Discord.py,我正在尝试发出一个拥抱命令来发送一个图像。但一旦发出命令,什么也不会发生。cli中没有错误,通道中也没有消息 @client.command(pass\u context=True) 异步(ctx): chdir(r“保存图像的文件夹中的文件”) hugs=[discord.File('1.jpg')、discord.File('2.jpg')、discord.File('3.jpg')、discord.File('4.gif')、discord.File('5.gif')、discord.Fi

我正在尝试发出一个拥抱命令来发送一个图像。但一旦发出命令,什么也不会发生。cli中没有错误,通道中也没有消息

@client.command(pass\u context=True)
异步(ctx):
chdir(r“保存图像的文件夹中的文件”)
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)
等待ctx.send(file=hugsrandom)

我还尝试在文件夹中发送一个带有bot文件的图像,并取出了
os.chdir
,但仍然没有发送任何图像。

这样做不好
os.chdir()
它可能会产生意想不到的后果。相反,你应该:

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\u DIR
是包含python文件的文件夹的路径。
。/pictures/1.jpg
是该文件夹的相对路径。

您可以扫描文件夹中的所有文件路径,并使用将其添加到列表中

导入全局
hugs=glob.glob(“FULL_PATH_HERE/*”)#这将获取文件夹中的所有文件路径。
#['FULL_PATH_HERE/1.gif','FULL_PATH_HERE/2.jpg']
拥抱=随机选择(拥抱)
发送(file=discord.file(the_hug))

更新2:以下内容对我很有用。。。我几乎可以肯定你的问题是你的文件没有被正确创建。要么就是你的机器人坏了。如果使用调试器(比如pycharms),可以设置断点,查看命令是否正在执行,以及discord.File对象是否正确创建

    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))
更新:我读得不够远,它应该接受一个字符串路径。您是否尝试过在代码中调试以查看打开文件是否成功

有时值得一看源代码。我去看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


它看起来像是在期待一个打开的文件。

但我必须定义每个图像,因为我有20多个图像/gif,所以定义所有图像太难了。像这样吗
hugs=glob.glob(“Path\Path”)['image.jpg','image.jpg','image.jpg','image.jpg','image.jpg']
我尝试发送示例中的图像[here:](),但它甚至没有将图像发送到同一个文件路径中。我可以尝试让它发送消息,以查看它是否能够发送消息。您是否尝试使用调试器并查看是否正确创建了discord.File对象?一个正确创建的discord.File对象应该有函数read(),它应该返回文件的内容。值得一提的是,我只是这么做了,我认为它只会发送图像。在深入阅读文档之后,我看到了一个使用此代码的示例,并意识到它创建了一个文件
python打开('bb8aed07d6c27ef0e96567d6269900a.jpg','rb')作为fp:wait ctx.send(file=discord.file(fp,'test.jpg'))
。我所做的所有调试都是移动东西。Tbh我以前从未做过认真的调试。谢谢!!你帮的不仅仅是告诉我我的错误^^