This repository has been archived on 2024-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
moderator-bot/app.py

82 lines
1.9 KiB
Python
Raw Permalink Normal View History

2022-08-07 04:45:35 -04:00
#!/usr/bin/env python3
import logging
2023-01-04 15:14:32 -05:00
from load import dp, bot, scheduler
from aiohttp.web_app import Application
from aiohttp.web import run_app
from aiogram.webhook.aiohttp_server import (
SimpleRequestHandler,
setup_application
)
2023-01-22 05:27:20 -05:00
2023-01-22 05:27:20 -05:00
import config
2023-02-18 08:16:54 -05:00
import handlers
2022-08-07 04:45:35 -04:00
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
WEBAPP_HOST = '127.0.0.1'
WEBAPP_PORT = 3001
# Don`t touch anything!
WEBHOOK_HOST = f'http://{WEBAPP_HOST}:{WEBAPP_PORT}'
WEBHOOK_PATH = f'/bot{config.bot_token}/'
2023-01-22 05:27:20 -05:00
WEBHOOK_URL = f"{WEBHOOK_HOST}{WEBHOOK_PATH}"
2022-08-07 04:45:35 -04:00
async def on_startup():
2023-01-13 03:22:32 -05:00
from utils.notify_start import notify_started_bot, database_is_empty
2023-01-22 05:27:20 -05:00
DATABASE_EMPTY = database_is_empty()
2023-01-13 03:22:32 -05:00
if DATABASE_EMPTY:
await bot.send_message(
config.second_group_id,
2023-02-18 08:16:54 -05:00
"Member table is empty, run: `!reload`"
)
2022-08-07 04:45:35 -04:00
await notify_started_bot(bot)
2023-01-13 03:22:32 -05:00
2022-08-07 04:45:35 -04:00
from utils.default_commands import set_default_commands
await set_default_commands(bot)
2022-08-13 08:25:52 -04:00
2023-01-04 15:14:32 -05:00
# Reloading users data
from utils import reload_users_data
2023-01-22 05:27:20 -05:00
scheduler.add_job(reload_users_data, "interval", seconds=config.update_interval)
2023-01-04 15:14:32 -05:00
scheduler.start()
2022-08-13 08:25:52 -04:00
from load import tgc
await tgc.client.start()
2023-01-04 15:14:32 -05:00
2022-08-07 04:45:35 -04:00
await bot.set_webhook(WEBHOOK_URL)
2023-01-22 05:27:20 -05:00
async def on_shutdown():
2022-08-07 04:45:35 -04:00
await bot.delete_webhook()
# Close Redis connection.
await dp.storage.close()
await bot.session.close()
2023-01-22 05:27:20 -05:00
2022-08-07 04:45:35 -04:00
def main() -> None:
dp.startup.register(on_startup)
dp.shutdown.register(on_shutdown)
2022-11-06 03:48:10 -05:00
if config.USE_WEBHOOK:
app = Application()
app["bot"] = bot
SimpleRequestHandler(
2022-08-07 04:45:35 -04:00
dispatcher=dp,
bot=bot,
).register(app, path=WEBHOOK_PATH)
setup_application(app, dp, bot=bot)
run_app(app, host=WEBAPP_HOST, port=WEBAPP_PORT)
2022-08-07 04:45:35 -04:00
else:
dp.run_polling()
2023-01-22 05:27:20 -05:00
2022-08-07 04:45:35 -04:00
if __name__ == '__main__':
main()