12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- from asyncio import run
- from telethon import TelegramClient
- from telethon.events import NewMessage
- from telethon.utils import resolve_bot_file_id
- from utils import parse_command, get_link_to_user
- from config import config
- from db import init_db
- from actions import (
- find_action,
- get_random_gif,
- is_admin
- )
- from commands import COMMANDS
- bot = TelegramClient(
- 'openkriemy',
- config.API_ID,
- config.API_HASH
- ).start(bot_token=config.API_TOKEN)
-
- @bot.on(NewMessage)
- async def on_message(event):
- try:
- command = parse_command(event.text)
- except ValueError:
- return
- handler = COMMANDS.get(command.name, None)
- if handler:
- if handler.is_restricted\
- and not await is_admin(bot, event.sender):
- await event.reply('К сожалению, данная команда Вам недоступна.')
- else:
- await handler.handler(bot, event, command)
- return
-
- try:
- action = await find_action(command.name)
- except SyntaxError:
- return
- if not action:
- return
- reply_to = None
- target = await event.get_reply_message()
- if not target:
- try:
- target = await bot.get_entity(command.args[0])
- except (ValueError, IndexError):
- await event.reply('Это действие нужно применить на кого-то!')
- return
- else:
- reply_to = target
- target = target.sender
- if target.id == event.sender.id\
- and not action.can_apply_to_self:
- await event.reply('Данное действие нельзя применять к самому себе...')
- return
-
- await event.delete()
-
- text = action.template.format(**{
- 'initiator': get_link_to_user(event.sender),
- 'target': get_link_to_user(target)
- })
-
- gif = await get_random_gif(action)
- if gif:
- gif = resolve_bot_file_id(gif.file_id)
- await bot.send_message(
- event.peer_id,
- message=text,
- file=gif,
- reply_to=reply_to
- )
-
- with bot:
- bot.loop.run_until_complete(init_db())
- bot.start()
- bot.run_until_disconnected()
|