【问题标题】:Discord.py Hug command with imageDiscord.py 带有图像的拥抱命令
【发布时间】:2020-11-25 16:32:52
【问题描述】:

我正在尝试发送一个发送图像的拥抱命令。但是在发送命令后,什么也没有发生。 cli没有错误,频道也没有消息。

@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)

我还尝试在包含 bot 文件的文件夹中发送一张图片,然后取出 os.chdir,但仍然没有发送任何内容。

【问题讨论】:

    标签: python-3.x discord discord.py


    【解决方案1】:

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

    【讨论】:

    • 但我必须定义每一张图片,因为我有 20 多张图片/GIF,所以定义所有图片的工作量太大。
    【解决方案2】:

    您可以扫描文件夹中的所有文件路径,并使用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))
    

    【讨论】:

    • 这样吗? hugs = glob.glob("Path\Path") ['image.jpg', 'image.jpg', 'image.jpg', 'image.jpg']
    【解决方案3】:

    UPDATE2:以下内容对我有用...我几乎可以肯定您的问题是您的文件没有正确创建。无论是那个还是你的机器人都以其他方式被破坏了。如果您使用调试器(例如 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
    
    

    它看起来好像在等待一个打开的文件。

    【讨论】:

    • 我尝试从示例 [here:] (discordpy.readthedocs.io/en/latest/…) 发送图像,但它甚至没有在同一个文件路径器中发送图像。我可以尝试让它发送一条消息,看看它是否能够发送一条消息。
    • 您是否尝试过使用调试器并查看您的 discord.File 对象是否被正确创建?一个正确创建的 discord.File 对象应该有函数 read(),它应该返回文件的内容。
    • 为了它的价值,我刚刚做到了
    • 老实说,我认为它只会发送图像。在深入阅读文档后,我看到了一个带有此代码的示例,并意识到它创建了一个文件。 python with open('bb8aed07d6c27ef0e96567d62699900a.jpg', 'rb') as fp: await ctx.send(file=discord.File(fp, 'test.jpg')) 我用同一文件夹中的 bot 文件和图像文件进行了尝试,但仍然没有。我所做的所有调试都是在移动东西。 Tbh 我以前从未做过认真的调试。
    • 谢谢!!你的帮助不仅仅是告诉我我的错误^^
    猜你喜欢
    • 2021-09-26
    • 1970-01-01
    • 2021-09-29
    • 2020-11-24
    • 2021-04-20
    • 1970-01-01
    • 2022-12-10
    • 2021-01-26
    • 2021-08-27
    相关资源
    最近更新 更多