Beispiel #1
0
    async def _to_zip_emojis(self, ctx: commands.Context, *emojis: str):
        """
        Get a `.zip` archive of a list of emojis.

        The returned `.zip` archive can be used for the `[p]emojitools add fromzip` command.
        """

        async with ctx.typing():
            async with TemporaryDirectory() as temp_dir:
                for e in emojis:
                    try:
                        em = await commands.PartialEmojiConverter().convert(
                            ctx=ctx, argument=e)
                    except commands.BadArgument:
                        return await ctx.send(f"Invalid emoji: {e}")

                    if em.animated:
                        await em.url.save(
                            os.path.join(temp_dir, f"{em.name}.gif"))
                    else:
                        await em.url.save(
                            os.path.join(temp_dir, f"{em.name}.png"))

                aiozip = AioZipStream(await self.getfiles(temp_dir),
                                      chunksize=32768)
                async with NamedTemporaryFile('wb+') as z:
                    async for chunk in aiozip.stream():
                        await z.write(chunk)
                    await z.seek(0)
                    zip_file_obj = discord.File(z.name, filename="emojis.zip")

        return await ctx.send(
            f"{len(emojis)} emojis were saved to this `.zip` archive!",
            file=zip_file_obj)
Beispiel #2
0
    async def _to_zip_server(self, ctx: commands.Context):
        """
        Get a `.zip` archive of all custom emojis in the server.

        The returned `.zip` archive can be used for the `[p]emojitools add fromzip` command.
        """

        async with ctx.typing():
            count = 0
            async with TemporaryDirectory() as temp_dir:
                for e in ctx.guild.emojis:
                    count += 1
                    if e.animated:
                        await e.url.save(
                            os.path.join(temp_dir, f"{e.name}.gif"))
                    else:
                        await e.url.save(
                            os.path.join(temp_dir, f"{e.name}.png"))

                aiozip = AioZipStream(await self.getfiles(temp_dir),
                                      chunksize=32768)
                async with NamedTemporaryFile('wb+') as z:
                    async for chunk in aiozip.stream():
                        await z.write(chunk)
                    await z.seek(0)
                    zip_file_obj = discord.File(
                        z.name, filename=f"{ctx.guild.name}.zip")

        return await ctx.send(
            f"{count} emojis were saved to this `.zip` archive!",
            file=zip_file_obj)
Beispiel #3
0
async def dir_paths() -> Paths:
  async with TemporaryDirectory() as temp:
    yield get_paths(temp)