Exemple #1
0
def register(**args):
    """ Register a new event. """
    args["func"] = lambda e: e.via_bot_id is None

    stack = inspect.stack()
    previous_stack_frame = stack[1]
    file_test = Path(previous_stack_frame.filename)
    file_test = file_test.stem.replace(".py", "")
    pattern = args.get("pattern", None)
    disable_edited = args.get("disable_edited", True)

    if pattern is not None and not pattern.startswith("(?i)"):
        args["pattern"] = "(?i)" + pattern

    if "disable_edited" in args:
        del args["disable_edited"]

    reg = re.compile("(.*)")
    if not pattern == None:
        try:
            cmd = re.search(reg, pattern)
            try:
                cmd = cmd.group(1).replace("$",
                                           "").replace("\\",
                                                       "").replace("^", "")
            except:
                pass

            try:
                CMD_LIST[file_test].append(cmd)
            except:
                CMD_LIST.update({file_test: [cmd]})
        except:
            pass

    def decorator(func):
        if not disable_edited:
            bot.add_event_handler(func, events.MessageEdited(**args))
        bot.add_event_handler(func, events.NewMessage(**args))
        if client2:
            client2.add_event_handler(func, events.NewMessage(**args))
        if client3:
            client3.add_event_handler(func, events.NewMessage(**args))
        try:
            LOAD_PLUG[file_test].append(func)
        except Exception:
            LOAD_PLUG.update({file_test: [func]})
        return func

    return decorator
Exemple #2
0
def friday_on_cmd(pattern=None, **args):
    args["func"] = lambda e: e.via_bot_id is None

    stack = inspect.stack()
    previous_stack_frame = stack[1]
    file_test = Path(previous_stack_frame.filename)
    file_test = file_test.stem.replace(".py", "")
    allow_sudo = args.get("allow_sudo", False)

    # get the pattern from the decorator
    if pattern is not None:
        if pattern.startswith("\#"):
            # special fix for snip.py
            args["pattern"] = re.compile(pattern)
        else:
            args["pattern"] = re.compile(cmdhandler + pattern)
            cmd = cmdhandler + pattern
            try:
                CMD_LIST[file_test].append(cmd)
            except:
                CMD_LIST.update({file_test: [cmd]})

    args["outgoing"] = True
    # should this command be available for other users?
    if allow_sudo:
        args["from_users"] = list(Config.SUDO_USERS)
        # Mutually exclusive with outgoing (can only set one of either).
        args["incoming"] = True
        del args["allow_sudo"]

    # error handling condition check
    elif "incoming" in args and not args["incoming"]:
        args["outgoing"] = True

    # add blacklist chats, UB should not respond in these chats
    if "allow_edited_updates" in args and args["allow_edited_updates"]:
        args["allow_edited_updates"]
        del args["allow_edited_updates"]

    # check if the plugin should listen for outgoing 'messages'

    return events.NewMessage(**args)
Exemple #3
0
def command(**args):
    args["func"] = lambda e: e.via_bot_id is None

    stack = inspect.stack()
    previous_stack_frame = stack[1]
    file_test = Path(previous_stack_frame.filename)
    file_test = file_test.stem.replace(".py", "")
    if 1 == 0:
        return print("stupidity at its best")
    else:
        pattern = args.get("pattern", None)
        allow_sudo = args.get("allow_sudo", False)
        allow_edited_updates = args.get("allow_edited_updates", False)
        args["incoming"] = args.get("incoming", False)
        args["outgoing"] = True
        if bool(args["incoming"]):
            args["outgoing"] = False

        try:
            if pattern is not None and not pattern.startswith("(?i)"):
                args["pattern"] = "(?i)" + pattern
        except:
            pass

        reg = re.compile("(.*)")
        if not pattern == None:
            try:
                cmd = re.search(reg, pattern)
                try:
                    cmd = (cmd.group(1).replace("$", "").replace("\\",
                                                                 "").replace(
                                                                     "^", ""))
                except:
                    pass

                try:
                    CMD_LIST[file_test].append(cmd)
                except:
                    CMD_LIST.update({file_test: [cmd]})
            except:
                pass

        if allow_sudo:
            args["from_users"] = list(Config.SUDO_USERS)
            # Mutually exclusive with outgoing (can only set one of either).
            args["incoming"] = True
        del allow_sudo
        try:
            del args["allow_sudo"]
        except:
            pass

        if "allow_edited_updates" in args:
            del args["allow_edited_updates"]

        def decorator(func):
            if not allow_edited_updates:
                bot.add_event_handler(func, events.MessageEdited(**args))
            bot.add_event_handler(func, events.NewMessage(**args))
            try:
                LOAD_PLUG[file_test].append(func)
            except Exception:
                LOAD_PLUG.update({file_test: [func]})
            return func

        return decorator
Exemple #4
0
def friday_on_command(**args):
    stack = inspect.stack()
    previous_stack_frame = stack[1]
    file_test = Path(previous_stack_frame.filename)
    file_test = file_test.stem.replace(".py", "")
    pattern = args.get('pattern', None)
    group_only = args.get('group_only', False)
    allow_sudo = args.get('allow_sudo', True)
    out_going = args.get('out_going', True)
    pm_only = args.get('pm_only', False)
    chnnl_only = args.get('chnnl_only', False)
    disable_errors = args.get('disable_errors', False)
    if out_going:
        args['outgoing'] = True
    else:
        args['outgoing'] = False
    if 'out_going' in args:
        del args['out_going']
    if "chnnl_only" in args:
        del args['chnnl_only']
    if "group_only" in args:
        del args['group_only']
    if "disable_errors" in args:
        del args['disable_errors']
    if 'allow_sudo' in args:
        del args['allow_sudo']
    if allow_sudo:
        args["from_users"] = list(Config.SUDO_USERS)
    if "pm_only" in args:
        del args['pm_only']
    if 'pattern' in args:
        del args['pattern']
    if pattern != None:
        try:
            try:
                args['pattern'] = re.compile(cmdhandler + pattern)
                cmd = cmdhandler + pattern
            except:
                pass
            try:
                CMD_LIST[file_test].append(cmd)
            except:
                CMD_LIST.update({file_test: [cmd]})
        except:
            pass

    def decorator(func):
        async def wrapper(check):
            # Ignore Fwds
            if check.fwd_from:
                return
            # Works Only In Groups
            if group_only and not check.is_group:
                await check.respond("`Are you sure this is a group?`")
                return
            # Works Only in Channel
            if chnnl_only and not check.is_channel:
                await check.respond("This Command Only Works In Channel!")
                return
            # Works Only in Private Chat
            if pm_only and not check.is_private:
                await check.respond("`This Cmd Only Works On PM!`")
                return
            # Don't Give Access To Others Using Inline Search.
            if check.via_bot_id and check.out:
                return
            try:
                await func(check)
            except events.StopPropagation:
                raise events.StopPropagation
            except KeyboardInterrupt:
                pass
            except BaseException as e:
                sedprint.exception(str(e))
                if not disable_errors:
                    TZ = pytz.timezone(Config.TZ)
                    datetime_tz = datetime.now(TZ)
                    text = "ERROR - REPORT\n\n"
                    text += datetime_tz.strftime(
                        "Date : %Y-%m-%d \nTime : %H:%M:%S")
                    text += "\nGroup ID: " + str(check.chat_id)
                    text += "\nSender ID: " + str(check.sender_id)
                    text += "\n\nEvent Trigger:\n"
                    text += str(check.text)
                    text += "\n\nTraceback info:\n"
                    text += str(format_exc())
                    text += "\n\nError text:\n"
                    text += str(sys.exc_info()[1])
                    file = open("error.log", "w+")
                    file.write(text)
                    file.close()
                    try:
                        await check.client.send_file(
                            Config.PRIVATE_GROUP_ID,
                            "error.log",
                            caption="Error LoG, Please Forward To @FridayChat!",
                        )
                    except:
                        await check.client.send_file(
                            bot.uid,
                            "error.log",
                            caption="Error LoG, Please Forward To @FridayChat!",
                        )
                    os.remove("error.log")

        bot.add_event_handler(wrapper, events.NewMessage(**args))
        if client2:
            client2.add_event_handler(wrapper, events.NewMessage(**args))
        if client3:
            client3.add_event_handler(wrapper, events.NewMessage(**args))
        return wrapper

    return decorator
Exemple #5
0
    def decorator(func):
        async def wrapper(check):
            # Ignore Fwds
            if check.fwd_from:
                return
            # Works Only In Groups
            if group_only and not check.is_group:
                await check.respond("`Are you sure this is a group?`")
                return
            # Works Only in Channel
            if chnnl_only and not check.is_channel:
                await check.respond("This Command Only Works In Channel!")
                return
            # Works Only in Private Chat
            if pm_only and not check.is_private:
                await check.respond("`This Cmd Only Works On PM!`")
                return
            # Don't Give Access To Others Using Inline Search.
            if check.via_bot_id:
                return
            try:
                await func(check)
            except events.StopPropagation:
                raise events.StopPropagation
            except KeyboardInterrupt:
                pass
            except BaseException as e:
                sedprint.exception(str(e))
                if not disable_errors:
                    TZ = pytz.timezone(Config.TZ)
                    datetime_tz = datetime.now(TZ)
                    text = "ERROR - REPORT\n\n"
                    text += datetime_tz.strftime(
                        "Date : %Y-%m-%d \nTime : %H:%M:%S")
                    text += "\nGroup ID: " + str(check.chat_id)
                    text += "\nSender ID: " + str(check.sender_id)
                    text += "\n\nEvent Trigger:\n"
                    text += str(check.text)
                    text += "\n\nTraceback info:\n"
                    text += str(format_exc())
                    text += "\n\nError text:\n"
                    text += str(sys.exc_info()[1])
                    file = open("error.log", "w+")
                    file.write(text)
                    file.close()
                    try:
                        await check.client.send_file(
                            Config.PRIVATE_GROUP_ID,
                            "error.log",
                            caption=
                            "Error LoG, Please Forward To @FridayChat!, If You Think Its A Error.",
                        )
                    except:
                        await check.client.send_file(
                            bot.uid,
                            "error.log",
                            caption=
                            "Error LoG, Please Forward To @FridayChat!, If You Think Its A Error.",
                        )
                    os.remove("error.log")

        bot.add_event_handler(wrapper, events.NewMessage(**args))
        if client2:
            client2.add_event_handler(wrapper, events.NewMessage(**args))
        if client3:
            client3.add_event_handler(wrapper, events.NewMessage(**args))
        try:
            CMD_LIST[file_test].append(cmd)
        except:
            CMD_LIST.update({file_test: [cmd]})
        return wrapper