Пример #1
0
    def _build_voting_status(self, message_id, chat_id, voting_finished):
        cur = yield self.db.execute('SELECT count(*), sum(vote_yes::int) FROM votes_history WHERE message_id = %s AND '
                                    'original_chat_id = %s', (message_id, chat_id))

        total_votes, approves = cur.fetchone()
        if total_votes == 0:
            percent_yes = 0
            percent_no = 0
            approves = 0
        else:
            percent_yes = approves / total_votes
            percent_no = 1 - percent_yes

        max_thumbs = 8

        thumb_ups = Emoji.THUMBS_UP_SIGN * round(max_thumbs * percent_yes)
        thumb_downs = Emoji.THUMBS_DOWN_SIGN * round(max_thumbs * percent_no)

        message = [pgettext('Beginning of poll message', 'Current poll progress:'),
                   npgettext('Count of voted moderators', '{cnt} vote', '{cnt} votes', total_votes).format(
                       cnt=total_votes)]

        if voting_finished or self.settings.get('public_vote', True):
            if voting_finished:
                more_yes = more_no = ''
            else:
                to_win_yes = self.settings['votes'] - approves
                more_yes = npgettext('Votes left to make a decision', '{cnt} more to win', '{cnt} more to win', to_win_yes) \
                    .format(cnt=to_win_yes)
                to_win_no = self.settings['votes'] - total_votes + approves
                more_no = npgettext('Votes left to make a decision', '{cnt} more to win', '{cnt} more to win', to_win_no) \
                    .format(cnt=to_win_no)

                more_yes = pgettext('ignore', ' ({})').format(more_yes)
                more_no = pgettext('ignore', ' ({})').format(more_no)
                more_yes.locale = self.locale
                more_no.locale = self.locale

            message.append("{thumb_up}{thumbs_up} — {percent_yes}%{more_yes}\n"
                           "{thumb_down}{thumbs_down} — {percent_no}%{more_no}\n"
                           .format(thumb_up=Emoji.THUMBS_UP_SIGN, thumbs_up=thumb_ups,
                                   percent_yes=round(percent_yes * 100), thumb_down=Emoji.THUMBS_DOWN_SIGN,
                                   thumbs_down=thumb_downs, percent_no=round(percent_no * 100), more_yes=more_yes,
                                   more_no=more_no))

        if voting_finished:
            message.append(pgettext('Poll finished', 'Poll is closed.'))
            if approves >= self.settings['votes']:
                cur = yield self.db.execute('SELECT is_published FROM incoming_messages WHERE bot_id = %s AND '
                                            'id = %s AND original_chat_id = %s', (self.bot_id, message_id, chat_id))

                row = cur.fetchone()
                if row and row[0]:
                    message.append(pgettext('Vote successful, message is published', 'The message is published.'))
                else:
                    message.append(pgettext('Vote successful', 'The message will be published soon.'))
            else:
                message.append(pgettext('Vote failed', 'The message will not be published.'))

        return message
Пример #2
0
def check_freq(bot, **kwargs):
    if 'message' not in kwargs or not bot.settings.get('msg_freq_limit') or \
                    kwargs['message']['chat']['id'] == bot.moderator_chat_id:
        return False

    fl = bot.settings['msg_freq_limit']

    msgs = yield get_messages_count(bot.db, kwargs['message']['from']['id'],
                                    bot.bot_id, fl[1])
    if msgs < fl[0]:
        return False

    freq_limit_msg_str = npgettext('Messages count', '{msg} message',
                                   '{msg} messages', fl[0]).format(msg=fl[0])
    freq_limit_days_str = npgettext('Days', '{n} day', '{n} days',
                                    fl[1]).format(n=fl[1])
    freq_limit_str = pgettext('Frequency limit', '{messages_str} per {days_str}') \
        .format(messages_str=freq_limit_msg_str, days_str=freq_limit_days_str)

    msg = pgettext(
        'Out of limits',
        'Unfortunately, you\'re out of limits! You can send only {freq_limit_str}'
    )

    yield bot.send_message(msg.format(freq_limit_str=freq_limit_str),
                           reply_to_message=kwargs['message'])
Пример #3
0
def help_command(bot, message):
    report_botan(message, 'slave_help')
    delay_str = npgettext('Delay between channel messages', '{delay} minute',
                          '{delay} minutes', bot.settings['delay'])
    timeout_str = npgettext('Voting timeout', '{timeout} hour',
                            '{timeout} hours', bot.settings['vote_timeout'])
    power_state = 'on' if bot.settings.get('power') else 'off'
    power_state_str = pgettext('Boolean settings', power_state)
    public_vote_state = 'on' if bot.settings.get('public_vote') else 'off'
    public_vote_state_str = pgettext('Boolean settings', public_vote_state)
    selfvote_state = 'on' if bot.settings.get('selfvote') else 'off'
    selfvote_state_str = pgettext('Boolean settings', selfvote_state)
    start_web_preview_state = 'on' if bot.settings.get(
        'start_web_preview') else 'off'
    start_web_preview_state_str = pgettext('Boolean settings',
                                           start_web_preview_state)
    voteswitch_state = 'on' if bot.settings.get('allow_vote_switch') else 'off'
    voteswitch_state_str = pgettext('Boolean settings', voteswitch_state)
    tag_polls_state = 'on' if bot.settings.get('tag_polls') else 'off'
    tag_polls_state_str = pgettext('Boolean settings', tag_polls_state)

    if bot.settings.get('msg_freq_limit'):
        fl = bot.settings['msg_freq_limit']
        freq_limit_msg_str = npgettext('Messages count', '{msg} message',
                                       '{msg} messages',
                                       fl[0]).format(msg=fl[0])
        freq_limit_days_str = npgettext('Days', '{n} day', '{n} days',
                                        fl[1]).format(n=fl[1])
        freq_limit_str = pgettext('Frequency limit', '{messages_str} per {days_str}') \
            .format(messages_str=freq_limit_msg_str, days_str=freq_limit_days_str)
    else:
        freq_limit_str = pgettext('No frequency limit', 'unlimited')

    msg = pgettext('/help command response', 'bot.help.response') \
        .format(current_delay_with_minutes=delay_str.format(delay=bot.settings['delay']),
                current_votes_required=bot.settings['votes'],
                current_timeout_with_hours=timeout_str.format(timeout=bot.settings['vote_timeout']),
                thumb_up_sign=Emoji.THUMBS_UP_SIGN, thumb_down_sign=Emoji.THUMBS_DOWN_SIGN,
                current_start_message=bot.settings['start'], power_state=power_state_str,
                public_vote_state=public_vote_state_str,
                current_text_limit={'min': bot.settings['text_min'], 'max': bot.settings['text_max']},
                selfvote_state=selfvote_state_str, start_web_preview_state=start_web_preview_state_str,
                current_freqlimit=freq_limit_str, voteswitch_state=voteswitch_state_str,
                tag_polls_state=tag_polls_state_str)

    try:
        yield bot.send_message(msg,
                               reply_to_message=message,
                               parse_mode=bot.PARSE_MODE_MD,
                               disable_web_page_preview=True)
    except:
        yield bot.send_message(msg,
                               reply_to_message=message,
                               disable_web_page_preview=True)
Пример #4
0
 def format_top_votes(row):
     return npgettext(
         'Votes count',
         '{votes_cnt} vote (with {votes_yes_cnt} {thumb_up_sign})',
         '{votes_cnt} votes (with {votes_yes_cnt} {thumb_up_sign})',
         row[0]).format(votes_cnt=format_number(row[0], bot.language),
                        votes_yes_cnt=format_number(row[1], bot.language),
                        thumb_up_sign=Emoji.THUMBS_UP_SIGN)
Пример #5
0
 def format_top_votes(row):
     return npgettext(
         'Votes count',
         '{votes_cnt} vote with {votes_yes_cnt} {thumb_up_sign} ({votes_percent}%)',
         '{votes_cnt} votes with {votes_yes_cnt} {thumb_up_sign} ({votes_percent}%)',
         row[0]).format(votes_cnt,
                        votes_yes_cnt,
                        thumb_up_sign=Emoji.THUMBS_UP_SIGN,
                        votes_percent=100 * votes_yes_cnt // votes_cnt)
Пример #6
0
def help_command(bot, message):
    report_botan(message, 'slave_help')
    delay_str = npgettext('Delay between channel messages', '{delay} minute', '{delay} minutes', bot.settings['delay'])
    timeout_str = npgettext('Voting timeout', '{timeout} hour', '{timeout} hours', bot.settings['vote_timeout'])
    power_state = 'on' if bot.settings.get('power') else 'off'
    power_state_str = pgettext('Boolean settings', power_state)
    public_vote_state = 'on' if bot.settings.get('public_vote') else 'off'
    public_vote_state_str = pgettext('Boolean settings', public_vote_state)
    selfvote_state = 'on' if bot.settings.get('selfvote') else 'off'
    selfvote_state_str = pgettext('Boolean settings', selfvote_state)
    start_web_preview_state = 'on' if bot.settings.get('start_web_preview') else 'off'
    start_web_preview_state_str = pgettext('Boolean settings', start_web_preview_state)
    voteswitch_state = 'on' if bot.settings.get('allow_vote_switch') else 'off'
    voteswitch_state_str = pgettext('Boolean settings', voteswitch_state)
    tag_polls_state = 'on' if bot.settings.get('tag_polls') else 'off'
    tag_polls_state_str = pgettext('Boolean settings', tag_polls_state)

    if bot.settings.get('msg_freq_limit'):
        fl = bot.settings['msg_freq_limit']
        freq_limit_msg_str = npgettext('Messages count', '{msg} message', '{msg} messages', fl[0]).format(msg=fl[0])
        freq_limit_days_str = npgettext('Days', '{n} day', '{n} days', fl[1]).format(n=fl[1])
        freq_limit_str = pgettext('Frequency limit', '{messages_str} per {days_str}') \
            .format(messages_str=freq_limit_msg_str, days_str=freq_limit_days_str)
    else:
        freq_limit_str = pgettext('No frequency limit', 'unlimited')

    msg = pgettext('/help command response', 'bot.help.response') \
        .format(current_delay_with_minutes=delay_str.format(delay=bot.settings['delay']),
                current_votes_required=bot.settings['votes'],
                current_timeout_with_hours=timeout_str.format(timeout=bot.settings['vote_timeout']),
                thumb_up_sign=Emoji.THUMBS_UP_SIGN, thumb_down_sign=Emoji.THUMBS_DOWN_SIGN,
                current_start_message=bot.settings['start'], power_state=power_state_str,
                public_vote_state=public_vote_state_str,
                current_text_limit={'min': bot.settings['text_min'], 'max': bot.settings['text_max']},
                selfvote_state=selfvote_state_str, start_web_preview_state=start_web_preview_state_str,
                current_freqlimit=freq_limit_str, voteswitch_state=voteswitch_state_str,
                tag_polls_state=tag_polls_state_str)

    try:
        yield bot.send_message(msg, reply_to_message=message, parse_mode=bot.PARSE_MODE_MD,
                               disable_web_page_preview=True)
    except:
        yield bot.send_message(msg, reply_to_message=message, disable_web_page_preview=True)
Пример #7
0
def polls_list_command(bot, message):
    cur = yield bot.db.execute('SELECT original_chat_id, id FROM incoming_messages WHERE '
                               'is_voting_success = False AND is_voting_fail = False AND is_published = False AND '
                               'bot_id = %s ORDER BY created_at ASC',
                               (bot.bot_id,))

    pending = cur.fetchall()

    if len(pending):
        polls_cnt_msg = npgettext('Polls count', '{cnt} poll', '{cnt} polls', len(pending)).format(cnt=len(pending))
        reply_part_one = pgettext('/pollslist reply message', 'There is {polls_msg} in progress:') \
            .format(polls_msg=polls_cnt_msg)
        yield bot.send_message(reply_part_one, reply_to_message=message)

        for (original_chat_id, message_id) in pending:
            yield bot.send_moderation_request(original_chat_id, message_id)
    else:
        yield bot.send_message(pgettext('/pollslist reply on empty pending-polls list',
                                        'There is no polls in progress.'),
                               reply_to_message=message)
Пример #8
0
 def format_top_messages(row):
     return npgettext('Messages count', '{messages_cnt} message', '{messages_cnt} messages', row[0]) \
         .format(messages_cnt=format_number(row[0], bot.language))
Пример #9
0
    def _build_voting_status(self, message_id, chat_id, voting_finished):
        cur = yield self.db.execute('SELECT count(*), sum(vote_yes::int) FROM votes_history WHERE message_id = %s AND '
                                    'original_chat_id = %s', (message_id, chat_id))

        total_votes, approves = cur.fetchone()
        if total_votes == 0:
            percent_yes = 0
            percent_no = 0
            approves = 0
        else:
            percent_yes = approves / total_votes
            percent_no = 1 - percent_yes

        max_thumbs = 8

        thumb_ups = Emoji.THUMBS_UP_SIGN * round(max_thumbs * percent_yes)
        thumb_downs = Emoji.THUMBS_DOWN_SIGN * round(max_thumbs * percent_no)

        message = [pgettext('Beginning of poll message', 'Current poll progress:'),
                   npgettext('Count of voted moderators', '{cnt} vote', '{cnt} votes', total_votes).format(
                       cnt=total_votes)]

        if voting_finished or self.settings.get('public_vote', True):
            if voting_finished:
                more_yes = more_no = ''
            else:
                to_win_yes = self.settings['votes'] - approves
                more_yes = npgettext('Votes left to make a decision', '{cnt} more to win', '{cnt} more to win', to_win_yes) \
                    .format(cnt=to_win_yes)
                to_win_no = self.settings['votes'] - total_votes + approves
                more_no = npgettext('Votes left to make a decision', '{cnt} more to win', '{cnt} more to win', to_win_no) \
                    .format(cnt=to_win_no)

                more_yes = pgettext('ignore', ' ({})').format(more_yes)
                more_no = pgettext('ignore', ' ({})').format(more_no)
                more_yes.locale = self.locale
                more_no.locale = self.locale

            message.append("{thumb_up}{thumbs_up} — {percent_yes}%{more_yes}\n"
                           "{thumb_down}{thumbs_down} — {percent_no}%{more_no}\n"
                           .format(thumb_up=Emoji.THUMBS_UP_SIGN, thumbs_up=thumb_ups,
                                   percent_yes=round(percent_yes * 100), thumb_down=Emoji.THUMBS_DOWN_SIGN,
                                   thumbs_down=thumb_downs, percent_no=round(percent_no * 100), more_yes=more_yes,
                                   more_no=more_no))

        if voting_finished:
            message.append(pgettext('Poll finished', 'Poll is closed.'))
            tags = ''
            if approves >= self.settings['votes']:
                cur = yield self.db.execute('SELECT is_published FROM incoming_messages WHERE bot_id = %s AND '
                                            'id = %s AND original_chat_id = %s', (self.bot_id, message_id, chat_id))

                row = cur.fetchone()
                if row and row[0]:
                    msg = pgettext('Vote successful, message is published', 'The message is published.')
                    if self.settings.get('tag_polls'):
                        tags = ' #accepted'
                else:
                    msg = pgettext('Vote successful', 'The message will be published soon.')
                    if self.settings.get('tag_polls'):
                        tags = ' #accepted #queued'
            else:
                msg = pgettext('Vote failed', 'The message will not be published.')
                if self.settings.get('tag_polls'):
                    tags = '#rejected'

            message.append(pgettext('Ignore', '{}{}').format(msg, tags))

        return message