# made by Jerry for [ultroid]
# exclusive for @moi_ultroid


""" 
✘ Commands Available -

•  {i}mygroups  </>
 List of all the Groups which you are a part Of.

 TIP: use [ `{i}mygroups pv` ] to send file
        in your saved message.
"""

import asyncio
import os

from . import *


@ultroid_cmd(pattern="mygroups?(.*)")
async def _(event):
    if event.fwd_from:
        return
    here = event.chat_id
    args = event.pattern_match.group(1)
    e1 = await eor(event, "`Processing...`")
    group_list = []
    dialogue = await ultroid_bot.get_dialogs()
    for dialog in dialogue:
        if dialog.is_group or dialog.is_channel:
            ids = await ultroid_bot.get_entity(dialog)
            try:
             
                    info = f"{ids.id}:  {ids.title}"
                    group_list.append(info)
            except BaseException:
                pass
            except Exception:
                continue

    if len(group_list) > 0:
        await e1.edit('`Done, Now Creating a File 🖨️`')
        with open('mygroups.txt', 'w') as book:
            for groups_channels in group_list:
                book.write(groups_channels + '\n')
        await asyncio.sleep(1)
        caption = f'List of Groups I am a part Of [total: {len(group_list)}]'
        if args and "pv" in args:
            await ultroid_bot.send_file("me", "mygroups.txt", caption=caption)
            await e1.respond("`File sent to your Saved Message`")
        else:
            await ultroid_bot.send_file(here, "mygroups.txt", caption=caption)
        os.remove("mygroups.txt")
        await e1.delete()
    else:
        await e1.edit("`Sed, I'm not in any Groups :/ `")


HELP.update({f"{__name__.split('.')[1]}": f"{__doc__.format(i=HNDLR)}"})
