import ormar
from aiogram import types
from aiogram.types import ChatMemberAdministrator, ChatMemberOwner
from sympy.strategies.core import switch

from configurator import config
from dispatcher import dp
import localization
from time import time
import re
import utils
import datetime
from aiogram.utils import exceptions

import random

from models.member import Member
from models.spam import Spam

from ruspam import predict as ruspam_predict

# blacklist = open("blacklist.txt", mode="r").read().split(',')
# blacklist_regexp = re.compile(r'(?iu)\b((—É|[–Ω–∑]–∞|(—Ö–∏—Ç—Ä–æ|–Ω–µ)?–≤–∑?[—ã—å—ä]|—Å[—å—ä]|(–∏|—Ä–∞)[–∑—Å]—ä?|(–æ[—Ç–±]|–ø–æ–¥)[—å—ä]?|(.\B)+?[–æ–∞–µ–∏])?-?([–µ—ë]–±(?!–æ[—Ä–π])|–∏[–ø–±][–∞–µ][—Ç—Ü]).*?|(–Ω[–∏–µ–∞]|[–¥–ø]–æ|—Ä–∞[–∑—Å]|–∑?–∞|—Å(–º–µ)?|–æ(—Ç|–¥–Ω–æ)?|–∞–ø—á)?-?—Ö—É([—è–π–∏–µ—ë—é]|–ª–∏(?!–≥–∞–Ω)).*?|(–≤[–∑—ã]|(—Ç—Ä–∏|–¥–≤–∞|—á–µ—Ç—ã—Ä–µ)–∂–¥—ã|(–Ω|—Å—É–∫)–∞)?-?–±–ª(—è(?!(—Ö|—à[–∫–Ω]|–º–±)[–∞—É–µ—ã–∏–æ]).*?|[–µ—ç][–¥—Ç]—å?)|(—Ä–∞[—Å–∑]|[–∑–Ω]–∞|[—Å–æ]|–≤—ã?|–ø(—Ä[–æ–∏]|–æ–¥)|–∏[–∑—Å]—ä?|[–∞–æ]—Ç)?–ø[–∏–µ—ë]–∑–¥.*?|(–∑–∞)?–ø[–∏–µ]–¥[–∞–æ–µ]?—Ä((–∞—Å)?(–∏(–ª–∏)?[–Ω—â–∫—Ç–ª]—å?)?|(–æ(—á[–µ–∏])?)?–∫|—é–≥)[–∞—É–µ—ã]?|–º–∞–Ω–¥([–∞—É–µ—ã]|–æ–π|[–∞–æ]–≤–æ—à—å?(–µ?–∫[–∞—É–µ])?|—é–∫(–æ–≤|[–∞—É–∏])?)|–º—É–¥([–∞–∏–æ].*?|–µ?–Ω([—å—é–∏—è]|–µ–π))|–º–ª—è([—Ç–¥]—å)?|–ª—è—Ç—å|([–Ω–∑]–∞|–ø–æ)—Ö|–º[–∞–æ]–ª[–∞–æ]—Ñ—å[—è–∏—é])\b')

@dp.message_handler(chat_id=config.groups.main, content_types=["new_chat_members"])
async def on_user_join(message: types.Message):
    """
    Removes "user joined" message.

    :param message: Service message "User joined group
    """

    # remove invite message
    await message.delete()

    # restrict media for new users
    '''await message.bot.restrict_chat_member(chat_id=config.groups.main,
                                          user_id=message.from_user.id,
                                          permissions=types.ChatPermissions(True),
                                          until_date=int(time()) + int(config.groups.new_users_nomedia))'''

    await utils.write_log(message.bot, "–ü—Ä–∏—Å–æ–µ–¥–∏–Ω–∏–ª—Å—è –ø–æ–ª—å–∑–æ–≤–∞—Ç–µ–ª—å "+utils.user_mention(message.from_user), "‚ûï –ù–æ–≤—ã–π —É—á–∞—Å—Ç–Ω–∏–∫")


@dp.message_handler(chat_id=config.groups.main, content_types=[types.ContentType.TEXT, types.ContentType.PHOTO, types.ContentType.DOCUMENT, types.ContentType.VIDEO])
@dp.edited_message_handler(chat_id=config.groups.main, content_types=[types.ContentType.TEXT, types.ContentType.PHOTO, types.ContentType.DOCUMENT, types.ContentType.VIDEO])
async def on_user_message(message: types.Message):
  """
  Process every user message.
  I.e. remove, if profanity is detected.
  Or remove, if spam is detected.
  Also log every user to database.

  :param message: Message in group
  """
  if message.is_automatic_forward:
      await message.reply(random.choice(["–ë–∞–Ω–∞–Ω—á–∏–∫–∏", "–ù—É –∏ –∫—Ç–æ —Ç–µ–ø–µ—Ä—å —Ç—É—Ç –ø–µ—Ä–≤—ã–π üòé", "–Ø –Ω–µ –ø–µ—Ä–≤—ã–π, –Ω–µ –≤—Ç–æ—Ä–æ–π, —è —Ç–∞–∫–∏—Ö –±–∞–Ω—é :3",
                                         "–ó–∞—Ö–æ–¥–∏ –Ω–µ –±–æ–π—Å—è, –≤—ã—Ö–æ–¥–∏ –Ω–µ –ø–ª–∞—á—å :3", "–ê–≤—Ç–æ—Ä –Ω–∏—á–µ–≥–æ –Ω–µ –ø–æ—Å—Ç–∏–ª —É–∂–µ 1 —Å–µ–∫—É–Ω–¥—É, –≤—Å—ë —è—Å–Ω–æ —Å–∫–∞—Ç–∏–ª—Å—è :3",
                                         "–°–∞–º—É—Ä–∞–π –Ω–∞ —Å—Ç—Ä–∞–∂–µ –ø–æ—Ä—è–¥–∫–µ ü´°", "–ü–æ—Å—Ç–∏–º –∂–∏–≤–µ–º :3", "–õ–∞–¥–Ω–æ", "–ü–æ–∫–∞ —Ç—ã —á–∏—Ç–∞–µ—à—å, —è —É–∂–µ –∫–æ–º–º–µ–Ω—Ç –æ—Å—Ç–∞–≤–∏–ª! –£—Å–ø–µ–≤–∞–π üòà",
                                         "–ö—Ç–æ –Ω–∞ —á–µ–º –ø–∏—à–µ—Ç? –Ø –≤–æ—Ç –Ω–∞ Python :3", "–°–∞–º—É—Ä–∞–π –Ω–∞ –º–µ—Å—Ç–µ üòé", "–°–∞–º—É—Ä–∞–π –±–µ–∑ –º–µ—á–∞, –∫–∞–∫ –±–æ—Ç –±–µ–∑ —Ç–æ–∫–µ–Ω–∞ :3",
                                         "–ù—É–ª–µ–≤–æ–π —Ç—É—Ç–± :3", "–û—Ö, —É–º–±–∞—Å–∞ ..", "–ú–∏–Ω—É—Ç–∫–∞ –ø–æ–ª–µ–∑–Ω–æ–π –∏–Ω—Ñ—ã. –°–µ–π—á–∞—Å –ø—Ä–∏–Ω—è—Ç–æ —Å—á–∏—Ç–∞—Ç—å, —á—Ç–æ 1–ú–ë = 1000–ö–ë. –ê 1–ú–∏–ë = 1024 –ö–∏–ë. –ñ–∏–≤–∏ —Å —ç—Ç–∏–º.",
                                         "‚úåÔ∏è –ó–¥–æ—Ä–æ–≤—å—è –∏ –º–∏—Ä–∞ —Ç–µ–±–µ, —á–∏—Ç–∞—é—â–∏–π —ç—Ç–æ :3", "–í—ã—à–µ–ª —ë–∂–∏–∫ –∏–∑ —Ç—É–º–∞–Ω–∞ ... –≤—ã–Ω—É–ª <tg-spoiler>–ø–∞–π—Ç–æ–Ω</tg-spoiler> –∏–∑ –∫–∞—Ä–º–∞–Ω–∞ :3",
                                         "–∂–∞–ª—å, —á—Ç–æ –¥–∞–ª–µ–∫–æ –Ω–µ –≤—Å–µ –ø–æ–π–º—É—Ç –≤ —á–µ–º –∂–µ –¥–µ–ª–æ))) –¥–µ–π—Å—Ç–≤–∏—Ç–µ–ª—å–Ω–æ —Ç–æ–Ω–∫–æ))))) –Ω–µ —Ç–∞–∫ —É–∂ –º–Ω–æ–≥–æ –∏ –æ–±—Ä–∞–∑–æ–≤–∞–Ω–Ω—ã—Ö –≤ –Ω–∞—à–µ –≤—Ä–µ–º—è, –∫—Ç–æ –∑–Ω–∞–µ—Ç, –ø–æ—á–µ–º—É —ç—Ç–æ —Ç–∞–∫ –∏–Ω—Ç–µ—Ä–µ—Å–Ω–æ –∏ –Ω–µ–æ–±—ã—á–Ω–æ))))",
                                         "–° –≤–µ–ª–∏–∫–æ–π —Å–∏–ª–æ–π –ø—Ä–∏—Ö–æ–¥–∏—Ç –≤–µ–ª–∏–∫–∞—è –±–µ–∑–æ—Ç–≤–µ—Ç—Å—Ç–≤–µ–Ω–Ω–æ—Å—Ç—å."]))
      return # auto-forward channel messages should not be checked

  ### Retrieve member record from DB
  try:
    # retrieve existing record
    member = await Member.objects.get(user_id=message.from_user.id)
  except ormar.NoMatch:
    # create new record
    member = await Member.objects.create(user_id=message.from_user.id, messages_count=1)

  # Retrieve tg member object
  tg_member = await message.bot.get_chat_member(message.chat.id, message.from_user.id)

  # Define message text
  msg_text = None

  if message.content_type == types.ContentType.TEXT:
      msg_text = message.text
  elif message.content_type in [types.ContentType.PHOTO, types.ContentType.DOCUMENT, types.ContentType.VIDEO]:
      msg_text = message.caption

  # Quit, if no message to check
  if msg_text is None:
      return

  ###   CHECK FOR PROFANITY & SPAM
  _del = False
  _word = None

  _del, _word = utils.check_for_profanity_all(msg_text)

  # process
  if _del:
    # PROFANITY DETECTED
    if not (tg_member.is_chat_admin() and tg_member.can_restrict_members):
        await message.delete()

        # increase member violations count
        member.violations_count_profanity += 1
        member.reputation_points -= 10 # every profanity message removes some reputation points from user
        await member.update()

    log_msg = msg_text
    if _word:
      log_msg = log_msg.replace(_word, '<u><b>'+_word+'</b></u>')
    log_msg += "\n\n<i>–ê–≤—Ç–æ—Ä:</i> "+utils.user_mention(message.from_user)

    await utils.write_log(message.bot, log_msg, "ü§¨ –ê–Ω—Ç–∏–º–∞—Ç")
  else:
    ### NO PROFANITY, GO CHECK FOR SPAM
    if member.messages_count < int(config.spam.member_messages_threshold) and ruspam_predict(msg_text):
        # SPAM DETECTED
        if not (tg_member.is_chat_admin() and tg_member.can_restrict_members):
            await message.delete()

            # increase member violations count
            member.violations_count_spam += 1
            await member.update()

        log_msg = msg_text
        log_msg += "\n\n<i>–ê–≤—Ç–æ—Ä:</i> " + utils.user_mention(message.from_user)

        # Create DB record on spam message
        spam_rec = await Spam.objects.create(message=msg_text, is_spam=True) # assume is spam by default

        # Generate keyboard with some actions for detected spam
        spam_keyboard = types.InlineKeyboardMarkup()

        # is spam + block user
        spam_keyboard.add(types.InlineKeyboardButton(
            text="‚ùå –≠—Ç–æ —Å–ø–∞–º + –∑–∞–±–ª–æ–∫–∏—Ä–æ–≤–∞—Ç—å –ø–æ–ª—å–∑–æ–≤–∞—Ç–µ–ª—è",
            callback_data=f"spam_ban_{message.from_user.id}")
        )

        # not a spam
        spam_keyboard.add(types.InlineKeyboardButton(
            text="‚ùé –≠—Ç–æ –ù–ï —Å–ø–∞–º",
            callback_data=f"spam_invert_{spam_rec.id}")
        )

        # test msg, remove from db
        spam_keyboard.add(types.InlineKeyboardButton(
            text="–£–±—Ä–∞—Ç—å –∏–∑ –ë–î, —ç—Ç–æ —Ç–µ—Å—Ç",
            callback_data=f"spam_test_{spam_rec.id}_{member.id}")
        )

        # send message with a keyboard to log channel
        await message.bot.send_message(
            config.groups.logs,
            utils.generate_log_message(log_msg, "‚ùå –ê–Ω—Ç–∏–°–ü–ê–ú"),
            reply_markup=spam_keyboard)
    else:
        # increase members messages count (only if message doesn't contain any violations)
        member.messages_count += 1
        await member.update()

@dp.message_handler(chat_id=config.groups.main, content_types=["voice"])
async def on_user_voice(message: types.Message):
    if random.random() < 0.75: # 75% chance to react
        await message.reply(random.choice(["—Ñ—É! –§–£ –Ø –°–ö–ê–ó–ê–õ, –ù–ï–õ–¨–ó–Ø. –ë–†–û–°–¨ –ö–ê–ö–£. –ü–ò–®–ò –¢–ï–ö–°–¢–û–ú.", "–î–∞–≤–∞–π –±–µ–∑ —Ä–µ–∑–∫–∏—Ö –¥–≤–∏–∂–µ–Ω–∏–π! –ü–æ–ª–æ–∂–∏ —Ç–µ–ª–µ—Ñ–æ–Ω –Ω–∞ –ø–æ–ª ... –∏ –±–æ–ª—å—à–µ –Ω–µ –∑–∞–ø–∏—Å—ã–≤–∞–π –ì–°–∫–∏ :3",
                                           "–ì–°–∫–∏ - –±–∏—á —Å–æ–≤—Ä–µ–º–µ–Ω–Ω–æ–≥–æ –æ–±—â–µ—Å—Ç–≤–∞. –î–µ–ª–∞–π –≤—ã–≤–æ–¥—ã, –º–∞–∫–∞—Ä–æ—à–∫–∞ :3", "–ê –≤–æ—Ç –≤ –º–æ—ë –≤—Ä–µ–º—è –ª—é–¥–∏ –ø–∏—Å–∞–ª–∏ —Ç–µ–∫—Å—Ç–æ–º ...",
                                           "–¢–´ –ü–†–ò–®–ï–õ –í –≠–¢–û–¢ –ß–ê–¢! –ù–æ —Ç—ã –ø—Ä–∏—à–µ–ª –±–µ–∑ —É–≤–∞–∂–µ–Ω–∏—è ...", "–°—ç—Ä, –≤—ã–Ω—É–∂–¥–µ–Ω —Å–æ–æ–±—â–∏—Ç—å –≤–∞–º, —á—Ç–æ –æ–±—â–∞—Ç—å—Å—è –ì–°–∫–∞–º–∏ —ç—Ç–æ –ø—Ä–∏–∑–Ω–∞–∫ –æ—Ç—Å—É—Ç—Å—Ç–≤–∏—è –∏–Ω—Ç–µ–ª–ª–µ–∫—Ç–∞.",
                                           "–ì–°–∫–∏ –±–æ–∂ ... –≤—ã–π–¥–∏ —Å —á–∞—Ç–∞, –Ω–µ –ø–æ–∑–æ—Ä—å—Å—è.", "–§—É! –§–£ –Ø –°–ö–ê–ó–ê–õ! –ü–∏—à–∏ —Ç–µ–∫—Å—Ç–æ–º."]))


@dp.message_handler(is_admin=False, chat_id=config.groups.main, content_types=[types.ContentType.TEXT, types.ContentType.PHOTO, types.ContentType.DOCUMENT, types.ContentType.VIDEO])
async def on_user_message_delete_woman(message: types.Message):
    if message.reply_to_message and message.reply_to_message.forward_from_chat and message.reply_to_message.forward_from_chat.id == config.groups.linked_channel:
        if (message.date - message.reply_to_message.forward_date).seconds <= 20: #test
            try:
                await message.delete()
                await utils.write_log(message.bot, f"–£–¥–∞–ª–µ–Ω–æ —Å–æ–æ–±—â–µ–Ω–∏–µ: {message.text}", "ü§ñ –ê–Ω—Ç–∏–±–æ—Ç")
            except exceptions.MessageCantBeDeleted:
                pass


@dp.message_handler(chat_id=config.groups.main, commands="–±—É", commands_prefix="!/")
async def on_bu(message: types.Message):
  await message.reply(random.choice(["–ë—É–≥–∞–≥–∞!", "–ù–µ –ø—É–≥–∞–π —Ç–∞–∫!", "–ë–æ–∂–µ ..", "–ù–µ –º–µ—à–∞–π –º–Ω–µ –¥–µ–ª–∞—Ç—å —Å–ª–æ–∂–Ω—ã–µ –∫–æ–º–ø—å—é—Ç–µ—Ä–Ω—ã–µ –≤—ã—á–∏—Å–ª–µ–Ω–∏—è :3", "–•–≤–∞—Ç–∏—Ç!", "–õ–∞–¥–Ω–æ ...", "–ë–ª —è –∞–∂ –≤–∑–¥—Ä–æ–≥–Ω—É–ª ...", "–¢–∞ –∑–∞ —à–æ :3", "–°—Ç—Ä–∞—à–Ω–∞ –≤—ã—Ä—É–±–∞–π", "–ù–µ —Å–º–µ—à–Ω–æ :3", "–¢–∞–∫ –∏ —Å–µ—Ä–¥–µ—à–Ω—ã–π –ø—Ä–∏—Å—Ç—É–ø –º–æ–∂–Ω–æ —Å–ª–æ–≤–∏—Ç—å!", "–°–∞–º —Ç—ã –±/—É –ø–æ–Ω"]))

@dp.message_handler(chat_id=config.groups.main, commands=["–∫–æ–Ω—Ñ–µ—Ç—ã", "sweets", "—Å–ª–∞–¥–∫–æ–µ", "—Ö—ç–ª–æ—É–∏–Ω", "—Å–ª–∞–¥–æ—Å—Ç–∏"], commands_prefix="!/")
async def on_sweets(message: types.Message):
    ### Retrieve member record from DB
    try:
        # retrieve existing record
        member = await Member.objects.get(user_id=message.from_user.id)
    except ormar.NoMatch:
        return

    if random.random() < 0.05:  # 5% chance to get golden ticket
        await message.reply("–ü–æ–∑–¥—Ä–∞–≤–ª—è—é, –≤ —Ç–≤–æ–µ–π —à–æ–∫–æ–ª–∞–¥–∫–µ –æ–∫–∞–∑–∞–ª—Å—è <u>–∑–æ–ª–æ—Ç–æ–π –±–∏–ª–µ—Ç</u> üé´")
        member.halloween_golden_tickets += 1
        await member.update()
    else:
        if random.random() < 0.25: # 25% to get sweets
            sweets_random = random.randrange(1, 100)
            await message.reply(f"–ù–∞ —Ç–µ–±–µ üç¨ <i>({sweets_random}—à—Ç.)</i>")
            member.halloween_sweets += sweets_random
            await member.update()
        else:
            if random.random() < 0.5: # 50% to get anything sweet
                await message.reply(random.choice(["–î–µ—Ä–∂–∏ —à–æ–∫–æ–ª–∞–¥–∫—É üç´", "–ü–µ—á–µ–Ω—å–∫—É, —Å—ç—Ä üç™", "–í–æ—Ç–± —Ç–µ–±–µ –ø–∏—Ä–æ–≥ ü•ß", "–í–æ—Ç–± üç≠"]))
                member.halloween_sweets += 1
                await member.update()

            else:
                if random.random() < 0.01: # 1% chance to get a pumpkin
                    await message.reply("üéÉ –í–æ—Ç–± —Ç–µ–±–µ —Ç—ã–∫–≤–∞!")
                    member.halloween_sweets += 100 # 1 pumpkin = 100 sweets
                    await member.update()

                else:
                    # no sweets this time :(
                    await message.reply(
                        random.choice(["–•–≤–∞—Ç–∏—Ç —Å —Ç–µ–±—è, —Å–ª–∞–¥–∫–æ–µ–∂–∫–∞ :3", "–ì–∞–¥–æ—Å—Ç–∏ —Ç–µ–±–µ, –∞ –Ω–µ —Å–ª–∞–¥–æ—Å—Ç–∏ –ø–æ–Ω :3\n<i>–ø—Ö–ø—Ö–ø–ø</i>",
                                       "–°–ª–∞–¥–∫–æ–µ –≤—Ä–µ–¥–Ω–æ –¥–ª—è –∑—É–±–æ–≤!", "–•–≤–∞—Ç–∏—Ç –∂—Ä–∞—Ç—å —Å–ª–∞–¥–æ—Å—Ç–∏ –ø–æ–Ω :3",
                                       "–ë—É–¥–µ—à—å –º–Ω–æ–≥–æ –∫—É—à–∞—Ç–± —Å–ª–∞–¥–æ—Å—Ç–µ–π, –∫–æ–¥ –Ω–µ –±—É–¥–µ—Ç –∫–æ–º–ø–∏–ª–∏—Ç—å—Å—è –ø—Ö–ø—Ö–ø—Ö :3"]))

@dp.message_handler(chat_id=config.groups.main, commands=["me", "—è", "info", "–∏–Ω—Ñ–æ", "lvl", "–ª–≤–ª"], commands_prefix="!/")
async def on_me(message: types.Message):
    if message.reply_to_message and not message.reply_to_message.is_automatic_forward:
        user_id = message.reply_to_message.from_user.id
    else:
        user_id = message.from_user.id

    ### Retrieve member record from DB
    try:
        # retrieve existing record
        member = await Member.objects.get(user_id=user_id)
    except ormar.NoMatch:
        return

    tg_member = await message.bot.get_chat_member(message.chat.id, user_id)

    member_level = None
    if isinstance(tg_member, (ChatMemberAdministrator, ChatMemberOwner)) and (tg_member.is_chat_creator() or tg_member.can_restrict_members):
        member_level = random.choice(["üéÉ –ì–ª–∞–≤–Ω–∞—è —Ç—ã–∫–æ–≤–∫–∞ —á–∞—Ç–∞", "üéÉ –ë–µ–∑–≥–æ–ª–æ–≤—ã–π –≤—Å–∞–¥–Ω–∏–∫", "üéÉ –ü–æ–≤–µ–ª–∏—Ç–µ–ª—å –Ω–æ—á–∏", "üéÉ –¢—ã–∫–≤–µ–Ω–Ω—ã–π –≤–ª–∞—Å—Ç–µ–ª–∏–Ω"])
        # member_rep = "üõ° –ù–µ–ø—Ä–∏–∫–æ—Å–Ω–æ–≤–µ–Ω–Ω—ã–π"
    else:
        # if member.messages_count < 100:
        #     member_level = "ü•∑ –ù–æ—É–Ω–µ–π–º"
        # elif 100 <= member.messages_count < 500:
        #     member_level = "üåö –ù–æ–≤–∏—á–æ–∫"
        # elif 500 <= member.messages_count < 1000:
        #     member_level = "üòé –û–ø—ã—Ç–Ω—ã–π"
        # elif 1000 <= member.messages_count < 2000:
        #     member_level = "üòà –í–µ—Ç–µ—Ä–∞–Ω"
        # else:
        #     member_level = "‚≠êÔ∏è –ú–∞—Å—Ç–µ—Ä"

        if member.messages_count < 100:
            member_level = random.choice(["üßõ –ù–µ–∏–∑–≤–µ—Å—Ç–Ω—ã–π –≤–∞–º–ø–∏—Ä", "üéÉ –ù–µ–∏–∑–≤–µ—Å—Ç–Ω–∞—è —Ç—ã–∫–≤–∞", "üê∫ –ë–µ–∑—ã–º—è–Ω–Ω—ã–π –æ–±–æ—Ä–æ—Ç–µ–Ω—å"])
        elif 100 <= member.messages_count < 500:
            member_level = "üåö –í–æ—Å—Ö–æ–¥—è—â–∞—è –ª—É–Ω–∞"
        elif 500 <= member.messages_count < 1000:
            member_level = "üéÉ –¢—ã–∫–æ–≤–∫–∞"
        elif 1000 <= member.messages_count < 2000:
            member_level = "üëª –í–µ—Ç–µ—Ä–∞–Ω –∏—Å–∫—É—Å—Å—Ç–≤–∞ –∑–∞–ø—É–≥–∏–≤–∞–Ω–∏—è"
        else:
            member_level = "‚≠êÔ∏èüéÉ –¢—ã–∫–≤–µ–Ω–Ω—ã–π –º–∞—Å—Ç–µ—Ä"

    if member.reputation_points < -2000:
        member_rep = "‚≠êÔ∏è‚≠êÔ∏è‚≠êÔ∏è‚≠êÔ∏è‚≠êÔ∏è –ü—è—Ç—å –∑–≤—ë–∑–¥ —Ä–æ–∑—ã—Å–∫–∞"
    elif -2000 <= member.reputation_points < -1000:
        member_rep = "‚ò†Ô∏è –û—Å–æ–±–æ –æ–ø–∞—Å–Ω—ã–π"
    elif -1000 <= member.reputation_points < -500:
        member_rep = "üíÄ –¢—ë–º–Ω–∞—è –ª–∏—á–Ω–æ—Å—Ç—å"
    elif -500 <= member.reputation_points < 0:
        member_rep = "üëø –ü–ª–æ—Ö–∞—è –ø–µ—á–µ–Ω—å–∫–∞</i>"
    elif 0 <= member.reputation_points < 100:
        member_rep = "üòê –ù–µ–π—Ç—Ä–∞–ª—å–Ω—ã–π"
    elif 100 <= member.reputation_points < 500:
        member_rep = "üôÇ –•–æ—Ä–æ—à–∞—è –ø–µ—á–µ–Ω—å–∫–∞"
    elif 500 <= member.reputation_points < 1000:
        member_rep = "üòé –ó–≤–µ–∑–¥–∞ —á–∞—Ç–∞"
    else:
        member_rep = "üòá –î–æ–±—Ä–µ–π—à–∏–π –¥–æ–±—Ä—è–∫"

    answer = f"{random.choice(['üë©‚Äçü¶∞','üë®‚Äçü¶≥','üßî','üë©','üë±‚Äç‚ôÄÔ∏è','üßë','üë®','üßî‚Äç‚ôÇÔ∏è','ü§ñ','üòº','üßë‚Äçü¶∞','üßë‚Äçü¶±','üë®‚Äçü¶∞','üë¶'])} <b>–£—á–∞—Å—Ç–Ω–∏–∫ —á–∞—Ç–∞:</b> {utils.user_mention(tg_member.user)}"
    answer += f"\n\n<i>{member_level}</i> <i>(<tg-spoiler>{member.messages_count}</tg-spoiler>)</i>"
    answer += f"\n<i>{member_rep}</i> <i>(<tg-spoiler>{member.reputation_points}</tg-spoiler>)</i>"

    await message.reply(answer)


@dp.message_handler(is_owner = True, chat_id=config.groups.main, commands=["setlvl"], commands_prefix="!")
async def on_setlvl(message: types.Message):
    if not message.reply_to_message:
        await message.reply("–ß–µ–≥–æ —Ç—ã –æ—Ç –º–µ–Ω—è —Ö–æ—á–µ—à—å :3")
        return

    ### Retrieve member record from DB
    try:
        # retrieve existing record
        member = await Member.objects.get(user_id=message.reply_to_message.from_user.id)
    except ormar.NoMatch:
        return

    try:
        member.messages_count = abs(int(utils.remove_prefix(message.text, "!setlvl")))

        if member.messages_count > 100000:
            await message.reply("–ß—Ç–æ –∫—É—Ä–∏—à—å, –¥—Ä—É–≥–∞–ª—ë–∫? :3")
        else:
            await member.update()
            await message.reply("–õ–∞–¥–Ω–æ :3")
    except ValueError:
        await message.reply("O_o –ú–¥–∞")

@dp.message_handler(is_owner = True, chat_id=config.groups.main, commands=["reward"], commands_prefix="!")
async def on_reward(message: types.Message):
    if not message.reply_to_message:
        await message.reply("–ß–µ–≥–æ —Ç—ã –æ—Ç –º–µ–Ω—è —Ö–æ—á–µ—à—å :3")
        return

    points = abs(int(utils.remove_prefix(message.text, "!reward")))

    ### Retrieve member record from DB
    try:
        # retrieve existing record
        member = await Member.objects.get(user_id=message.reply_to_message.from_user.id)
    except ormar.NoMatch:
        return

    try:
        member.reputation_points += points

        if points > 100_000:
            await message.reply("–ù–µ—Ç–± :3")
        else:
            await member.update()
            await message.reply(f"üéÉ <b>–°–ª—É—à–∞—é—Å—å, –ø–æ–≤–µ–ª–∏—Ç–µ–ª—å!</b>\n–£—á–∞—Å—Ç–Ω–∏–∫ —á–∞—Ç–∞ –±–ª–∞–≥–æ—Å–ª–æ–≤–ª—ë–Ω –≤–∞—à–µ–π –º–∏–ª–æ—Å—Ç—å—é, –µ–º—É –Ω–∞—á–∏—Å–ª–µ–Ω–æ <i><b>{points} –æ—á–∫–æ–≤ —Ä–µ–ø—É—Ç–∞—Ü–∏–∏.</b></i>")
    except ValueError:
        await message.reply("O_o –ú–¥–∞")

'''async def on_user_message(message: types.Message):
  """
  Removes messages, if they contain black listed words.

  :param message: Message in group
  """
  _del = False
  _word = None

  for w in blacklist:
    if w in message.text:
      _del = True
      _word = w
      break

  _result = blacklist_regexp.search(message.text)

  if _result:
    _word = _result.group(0)
    _del = True

  if _del:
    await message.delete()

    log_msg = message.text
    if _word:
      log_msg = log_msg.replace(_word, '<u><b>'+_word+'</b></u>')
    log_msg += "\n\n<i>–ê–≤—Ç–æ—Ä:</i> "+message.from_user.full_name+" ("+message.from_user.mention+")"

    await utils.write_log(message.bot, log_msg, "–ê–Ω—Ç–∏–º–∞—Ç")'''

'''@dp.message_handler(chat_id=config.groups.main)
async def do_ro_filter_messages(message: types.Message):
  user = await message.bot.get_chat_member(config.groups.main, message.from_user.id)

  # @TODO: small optimization can be done, by muting not muted members for a minute (or so)
  # but in this case there may be problems with restrictions (after restriction period ended, telegram returns
  # back the default chat restrictions, but not users previous restrictions)
  if builtins.RO and not user.is_chat_admin():
    await message.bot.delete_message(config.groups.main, message.message_id)'''
