<commit_before>import os
import random
import logging

import arrow
import telegram

from leonard import Leonard
from libs.timezone import local_time
from libs.utils import FakeMessage

telegram_client = telegram.Bot(os.environ['BOT_TOKEN'])
bot = Leonard(telegram_client)
bot.collect_plugins()

RETURN_MESSAGE_HOURS = list(range(8, 22))


def main():
    for key in bot.redis.scan_iter(match='user:*:registered'):
        if bot.redis.get(key).decode('utf-8') != '1':
            # TODO: Add reminder about registration
            continue
        _, u_id, _ = key.decode('utf-8').split(':')

        time = local_time(bot, int(u_id))
        if time.hour not in RETURN_MESSAGE_HOURS:
            continue

        if bot.user_get(u_id, 'return_sent'):
            continue

        last_interaction = arrow.get(bot.user_get(u_id, 'last_interaction') or time)
        interaction_delta = time - last_interaction

        if interaction_delta and last_interaction.replace(hours=+1) > time:
            continue

        bot.logger.info('Checking return message to: {}, where list: {}'.format(
            u_id, ([0] * round(interaction_delta.days / 2) + [0]) + [1, 1]
        ))

        result = random.choice(([0] * round(interaction_delta.days / 2) + [0]) + [1, 1])
        if result != 1:
            continue

        bot.user_set(u_id, 'return_sent', time.timestamp, ex=len(RETURN_MESSAGE_HOURS) * 60 * 60)
        m = FakeMessage()
        m.u_id = u_id

        try:
            bot.call_handler(m, 'main-menu')
        except Exception as error:
            bot.logger.error(error)


if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        bot.logger.error(e)<commit_msg>Change order of setting return status<commit_after>import os
import random
import logging

import arrow
import telegram

from leonard import Leonard
from libs.timezone import local_time
from libs.utils import FakeMessage

telegram_client = telegram.Bot(os.environ['BOT_TOKEN'])
bot = Leonard(telegram_client)
bot.collect_plugins()

RETURN_MESSAGE_HOURS = list(range(8, 22))


def main():
    for key in bot.redis.scan_iter(match='user:*:registered'):
        if bot.redis.get(key).decode('utf-8') != '1':
            # TODO: Add reminder about registration
            continue
        _, u_id, _ = key.decode('utf-8').split(':')

        time = local_time(bot, int(u_id))
        if time.hour not in RETURN_MESSAGE_HOURS:
            continue

        if bot.user_get(u_id, 'return_sent'):
            continue

        last_interaction = arrow.get(bot.user_get(u_id, 'last_interaction') or time)
        interaction_delta = time - last_interaction

        if interaction_delta and last_interaction.replace(hours=+1) > time:
            continue

        bot.logger.info('Checking return message to: {}, where list: {}'.format(
            u_id, ([0] * round(interaction_delta.days / 2) + [0]) + [1, 1]
        ))

        result = random.choice(([0] * round(interaction_delta.days / 2) + [0]) + [1, 1])
        if result != 1:
            continue

        m = FakeMessage()
        m.u_id = u_id

        try:
            bot.call_handler(m, 'main-menu')
        except Exception as error:
            bot.logger.error(error)


if __name__ == '__main__':
    try:
        main()
    except Exception as e:
        bot.logger.error(e)
{'fraction_non_alphanumeric': 0.11947431302270012, 'fraction_numerical': 0.011947431302270013, 'mean_word_length': 2.5252631578947367, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3675169', 'n_tokens_mistral': 1098, 'n_tokens_neox': 1053, 'n_words': 252}