Exemple #1
0
def set_admin(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        msg = update.message.text.split(' ', 1)[1]
        msg = msg.replace('@', '')
        if msg != '':
            # bot.getChatMember(update.message.chat_id, str(update.message.user.username))
            user = Session.query(User).filter_by(username=msg).first()

            # if user is None:
            # send_async(bot,
            #     chat_id=update.message.chat.id,
            #   text='No such user')

            #  else:
            adm = Session.query(Admin).filter_by(
                user_id=user.id, admin_group=update.message.chat.id).first()

            if adm is None:
                new_group_admin = Admin(user_id=user.id,
                                        admin_type=AdminType.GROUP.value,
                                        admin_group=update.message.chat.id)

                Session.add(new_group_admin)
                Session.commit()
                send_async(bot,
                           chat_id=update.message.chat.id,
                           text="""Welcome our new administrator: @{}!
    Check the commands list with /help command""".format(user.username))

            else:
                send_async(bot,
                           chat_id=update.message.chat.id,
                           text='@{} already has administrator rights'.format(
                               user.username))
Exemple #2
0
def add_trigger(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        msg = update.message.text.split(' ', 1)
        if len(msg) == 2 and len(
                msg[1]) > 0 or update.message.reply_to_message:
            trigger_text = msg[1].strip()
            trigger = Session.query(Trigger).filter_by(
                trigger=trigger_text).first()
            if trigger is None:
                data = update.message.reply_to_message
                add_trigger_db(data, trigger_text)
                send_async(
                    bot,
                    chat_id=update.message.chat.id,
                    text='The trigger for the phrase "{}" is set.'.format(
                        trigger_text))
            else:
                send_async(
                    bot,
                    chat_id=update.message.chat.id,
                    text='Trigger "{}" already exists, select another one.'.
                    format(trigger_text))
        else:
            send_async(bot,
                       chat_id=update.message.chat.id,
                       text='Your thoughts are not clear, try one more time')
Exemple #3
0
def enable_welcome(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):

        group = update_group(update.message.chat)
        group.welcome_enabled = True
        Session.add(group)
        Session.commit()
        send_async(bot, chat_id=update.message.chat.id, text='Welcome enabled')
Exemple #4
0
def show_welcome(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):

        group = update_group(update.message.chat)
        welcome_msg = Session.query(WelcomeMsg).filter_by(
            chat_id=group.id).first()
        if welcome_msg is None:
            welcome_msg = WelcomeMsg(chat_id=group.id,
                                     message='Hi, %username%!')
            Session.add(welcome_msg)
            Session.commit()

        send_async(bot, chat_id=group.id, text=welcome_msg.message)
Exemple #5
0
def list_admins(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        admins = Session.query(Admin).filter(
            Admin.admin_group == update.message.chat.id).all()
        users = []
        for admin_user in admins:
            users.append(
                Session.query(User).filter_by(id=admin_user.user_id).first())
        msg = 'Administrators list:\n'
        for user in users:
            msg += '{} @{} {} {}\n'.format(user.id, user.username,
                                           user.first_name, user.last_name)

        send_async(bot, chat_id=update.message.chat.id, text=msg)
Exemple #6
0
def del_trigger(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        msg = update.message.text.split(' ', 1)[1]
        trigger = Session.query(LocalTrigger).filter_by(trigger=msg).first()
        if trigger is not None:
            Session.delete(trigger)
            Session.commit()
            send_async(
                bot,
                chat_id=update.message.chat.id,
                text='The trigger for "{}" has been deleted.'.format(msg))
        else:
            send_async(bot,
                       chat_id=update.message.chat.id,
                       text='Where did you see such a trigger? 0_o')
Exemple #7
0
def set_welcome(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):

        group = update_group(update.message.chat)
        welcome_msg = Session.query(WelcomeMsg).filter_by(
            chat_id=group.id).first()
        if welcome_msg is None:
            welcome_msg = WelcomeMsg(chat_id=group.id,
                                     message=update.message.text.split(' ',
                                                                       1)[1])
        else:
            welcome_msg.message = update.message.text.split(' ', 1)[1]
        Session.add(welcome_msg)
        Session.commit()
        send_async(bot,
                   chat_id=update.message.chat.id,
                   text='The welcome text is set.')
Exemple #8
0
def ban(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        username, reason = update.message.text.split(' ', 2)[1:]
        username = username.replace('@', '')
        user = Session.query(User).filter_by(username=username).first()
        if user:
            banned = Session.query(Ban).filter_by(user_id=user.id).first()
            if banned:
                send_async(
                    bot,
                    chat_id=update.message.chat.id,
                    text='This user is already banned. The reason is: .'.
                    format(banned.to_date, banned.reason))
            else:
                banned = Ban()
                banned.user_id = user.id
                banned.from_date = datetime.now()
                banned.to_date = datetime.max
                banned.reason = reason or 'Reason not specified'
                member = Session.query().filter_by(user_id=user.id).first()
                if member:
                    Session.delete(member)
                admins = Session.query(Admin).filter_by(user_id=user.id).all()
                for admin in admins:
                    Session.delete(admin)
                Session.add(banned)
                Session.commit()
                send_async(bot,
                           chat_id=user.id,
                           text='You were banned because: {}'.format(
                               banned.reason))
                send_async(bot,
                           chat_id=update.message.chat.id,
                           text='Soldier successfully banned')
        else:
            send_async(bot,
                       chat_id=update.message.chat.id,
                       text='No such user')
Exemple #9
0
def unban(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        username = update.message.text.split(' ', 1)[1]
        username = username.replace('@', '')
        user = Session.query(User).filter_by(username=username).first()
        if user:
            banned = Session.query(Ban).filter_by(user_id=user.id).first()
            if banned:
                Session.delete(banned)
                Session.commit()
                send_async(bot, chat_id=user.id, text='We can talk again 🌚')
                send_async(
                    bot,
                    chat_id=update.message.chat.id,
                    text='{} is no longer banned.'.format('@' + user.username))
            else:
                send_async(bot,
                           chat_id=update.message.chat.id,
                           text='This soldier is not banned')
        else:
            send_async(bot,
                       chat_id=update.message.chat.id,
                       text='No such user')
Exemple #10
0
def del_admin(bot: Bot, update: Update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        msg = update.message.text.split(' ', 1)[1]
        if msg.find('@') != -1:
            msg = msg.replace('@', '')
            if msg != '':
                user = Session.query(User).filter_by(username=msg).first()
                if user is None:
                    send_async(bot,
                               chat_id=update.message.chat.id,
                               text='No such user')

                else:
                    del_adm(bot, update.message.chat.id, user, session)
        else:
            user = Session.query(User).filter_by(id=msg).first()
            if user is None:
                send_async(bot,
                           chat_id=update.message.chat.id,
                           text='No such user')

            else:
                del_adm(bot, update.message.chat.id, user)
Exemple #11
0
def kick(bot, update):
    if update.message.from_user.id in get_admin_ids(bot,
                                                    update.message.chat_id):
        bot.kick_chat_member(update.message.chat.id)