HardcoreUserbot icon indicating copy to clipboard operation
HardcoreUserbot copied to clipboard

Add files via upload

Open panddaa opened this issue 5 years ago • 0 comments

Friendly Telegram (telegram userbot)

Copyright (C) 2018-2019 The Authors

This program is free software: you can redistribute it and/or modify

it under the terms of the GNU Affero General Public License as published by

the Free Software Foundation, either version 3 of the License, or

(at your option) any later version.

This program is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License

along with this program. If not, see https://www.gnu.org/licenses/.

import asyncio import ast import time import logging from io import BytesIO from telethon.tl import functions from .. import loader, utils

logger = logging.getLogger(name)

try: from PIL import Image except ImportError: pil_installed = False else: pil_installed = True

def register(cb): cb(AutoProfileMod())

@loader.tds class AutoProfileMod(loader.Module): """Automatic stuff for your profile :P""" strings = {"name": "Automatic Profile", "missing_pil": "You don't have Pillow installed", "missing_pfp": "You don't have a profile picture to rotate", "invalid_args": "Missing parameters, please read the docs", "invalid_degrees": "Invalid number of degrees to rotate, please read the docs", "invalid_delete": "Please specify whether to delete the old pictures or not", "enabled_pfp": "Enabled profile picture rotation", "pfp_not_enabled": "Profile picture rotation is not enabled", "pfp_disabled": "Profile picture rotation disabled", "missing_time": "Time was not specified in bio", "enabled_bio": "Enabled bio clock", "bio_not_enabled": "Bio clock is not enabled", "disabled_bio": "Disabled bio clock", "enabled_name": "Enabled name clock", "name_not_enabled": "Name clock is not enabled", "disabled_name": "Name clock disabled", "how_many_pfps": "Please specify how many profile pictures should be removed", "invalid_pfp_count": "Invalid number of profile pictures to remove", "removed_pfps": "Removed {} profile pic(s)"}

def __init__(self):
    self.bio_enabled = False
    self.name_enabled = False
    self.pfp_enabled = False
    self.raw_bio = None
    self.raw_name = None

def config_complete(self):
    self.name = self.strings["name"]

async def client_ready(self, client, db):
    self.client = client

async def autopfpcmd(self, message):
    """Rotates your profile picture every 60 seconds with x degrees, usage:
       .autopfp <degrees> <remove previous (last pfp)>
       Degrees - 60, -10, etc
       Remove last pfp - True/1/False/0, case sensitive"""

    if not pil_installed:
        return await utils.answer(message, self.strings["missing_pil"])

    if not await self.client.get_profile_photos("me", limit=1):
        return await utils.answer(message, self.strings["missing_pfp"])

    msg = utils.get_args(message)
    if len(msg) != 2:
        return await utils.answer(message, self.strings["invalid_args"])

    try:
        degrees = int(msg[0])
    except ValueError:
        return await utils.answer(message, self.strings["invalid_degrees"])

    try:
        delete_previous = ast.literal_eval(msg[1])
    except (ValueError, SyntaxError):
        return await utils.answer(message, self.strings["invalid_delete"])

    with BytesIO() as pfp:
        await self.client.download_profile_photo("me", file=pfp)
        raw_pfp = Image.open(pfp)

        self.pfp_enabled = True
        pfp_degree = 0
        await self.allmodules.log("start_autopfp")
        await utils.answer(message, self.strings["enabled_pfp"])

        while self.pfp_enabled:
            pfp_degree = (pfp_degree + degrees) % 360
            rotated = raw_pfp.rotate(pfp_degree)
            with BytesIO() as buf:
                rotated.save(buf, format="JPEG")
                buf.seek(0)

                if delete_previous:
                    await self.client(functions.photos.
                                      DeletePhotosRequest(await self.client.get_profile_photos("me", limit=1)))

                await self.client(functions.photos.UploadProfilePhotoRequest(await self.client.upload_file(buf)))
                buf.close()
            await asyncio.sleep(60)

async def stopautopfpcmd(self, message):
    """Stop autobio cmd."""

    if self.pfp_enabled is False:
        return await utils.answer(message, self.strings["pfp_not_enabled"])
    else:
        self.pfp_enabled = False

        await self.client(functions.photos.DeletePhotosRequest(
            await self.client.get_profile_photos("me", limit=1)
        ))
        await self.allmodules.log("stop_autopfp")
        await utils.answer(message, self.strings["pfp_disabled"])

async def autobiocmd(self, message):
    """Automatically changes your account's bio with current time, usage:
        .autobio '<message, time as {time}>'"""

    msg = utils.get_args(message)
    if len(msg) != 1:
        return await utils.answer(message, self.strings["invalid_args"])
    raw_bio = msg[0]
    if "{time}" not in raw_bio:
        return await utils.answer(message, self.strings["missing_time"])

    self.bio_enabled = True
    self.raw_bio = raw_bio
    await self.allmodules.log("start_autobio")
    await utils.answer(message, self.strings["enabled_bio"])

    while self.bio_enabled is True:
        current_time = time.strftime("%H:%M")
        bio = raw_bio.format(time=current_time)
        await self.client(functions.account.UpdateProfileRequest(
            about=bio
        ))
        await asyncio.sleep(60)

async def stopautobiocmd(self, message):
    """Stop autobio cmd."""

    if self.bio_enabled is False:
        return await utils.answer(message, self.strings["bio_not_enabled"])
    else:
        self.bio_enabled = False
        await self.allmodules.log("stop_autobio")
        await utils.answer(message, self.strings["disabled_bio"])
        await self.client(functions.account.UpdateProfileRequest(about=self.raw_bio.format(time="")))

async def autonamecmd(self, message):
    """Automatically changes your Telegram name with current time, usage:
        .autoname '<message, time as {time}>'"""

    msg = utils.get_args(message)
    if len(msg) != 1:
        return await utils.answer(message, self.strings["invalid_args"])
    raw_name = msg[0]
    if "{time}" not in raw_name:
        return await utils.answer(message, self.strings["missing_time"])

    self.name_enabled = True
    self.raw_name = raw_name
    await self.allmodules.log("start_autoname")
    await utils.answer(message, self.strings["enabled_name"])

    while self.name_enabled is True:
        current_time = time.strftime("%H:%M")
        name = raw_name.format(time=current_time)
        await self.client(functions.account.UpdateProfileRequest(
            first_name=name
        ))
        await asyncio.sleep(60)

async def stopautonamecmd(self, message):
    """ Stop autoname cmd."""

    if self.name_enabled is False:
        return await utils.answer(message, self.strings["name_not_enabled"])
    else:
        self.name_enabled = False
        await self.allmodules.log("stop_autoname")
        await utils.answer(message, self.strings["disabled_name"])
        await self.client(functions.account.UpdateProfileRequest(
            first_name=self.raw_name.format(time="")
        ))

async def delpfpcmd(self, message):
    """ Remove x profile pic(s) from your profile.
    .delpfp <pfps count/unlimited - remove all>"""

    args = utils.get_args(message)
    if not args:
        return await utils.answer(message, self.strings["how_many_pfps"])
    if args[0].lower() == "unlimited":
        pfps_count = None
    else:
        try:
            pfps_count = int(args[0])
        except ValueError:
            return await utils.answer(message, self.strings["invalid_pfp_count"])
        if pfps_count <= 0:
            return await utils.answer(message, self.strings["invalid_pfp_count"])

    await self.client(functions.photos.DeletePhotosRequest(await self.client.get_profile_photos("me",
                                                                                                limit=pfps_count)))

    if pfps_count is None:
        pfps_count = _("all")
    await self.allmodules.log("delpfp")
    await utils.answer(message, self.strings["removed_pfps"].format(str(pfps_count)))

panddaa avatar May 02 '20 14:05 panddaa